| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2009 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 17 | #define LOG_TAG "APM::AudioPolicyManager" | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 | 
 | 19 |  | 
 | 20 | //#define VERY_VERBOSE_LOGGING | 
 | 21 | #ifdef VERY_VERBOSE_LOGGING | 
 | 22 | #define ALOGVV ALOGV | 
 | 23 | #else | 
 | 24 | #define ALOGVV(a...) do { } while(0) | 
 | 25 | #endif | 
 | 26 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 27 | #include <inttypes.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 28 | #include <math.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 29 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 30 | #include <AudioPolicyManagerInterface.h> | 
 | 31 | #include <AudioPolicyEngineInstance.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 32 | #include <cutils/properties.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 33 | #include <utils/Log.h> | 
 | 34 | #include <hardware/audio.h> | 
 | 35 | #include <hardware/audio_effect.h> | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 36 | #include <media/AudioParameter.h> | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 37 | #include <media/AudioPolicyHelper.h> | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 38 | #include <soundtrigger/SoundTrigger.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 39 | #include "AudioPolicyManager.h" | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 40 | #include <ConfigParsingUtils.h> | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 41 | #include "TypeConverter.h" | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 42 | #include <policy.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 43 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 44 | namespace android { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 45 |  | 
 | 46 | // ---------------------------------------------------------------------------- | 
 | 47 | // AudioPolicyInterface implementation | 
 | 48 | // ---------------------------------------------------------------------------- | 
 | 49 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 50 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 51 |                                                       audio_policy_dev_state_t state, | 
 | 52 |                                                       const char *device_address, | 
 | 53 |                                                       const char *device_name) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 54 | { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 55 |     return setDeviceConnectionStateInt(device, state, device_address, device_name); | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 56 | } | 
 | 57 |  | 
 | 58 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 59 |                                                          audio_policy_dev_state_t state, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 60 |                                                          const char *device_address, | 
 | 61 |                                                          const char *device_name) | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 62 | { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 63 |     ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", | 
 | 64 | -            device, state, device_address, device_name); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 65 |  | 
 | 66 |     // connect/disconnect only 1 device at a time | 
 | 67 |     if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; | 
 | 68 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 69 |     sp<DeviceDescriptor> devDesc = | 
 | 70 |             mHwModules.getDeviceDescriptor(device, device_address, device_name); | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 71 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 72 |     // handle output devices | 
 | 73 |     if (audio_is_output_device(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 74 |         SortedVector <audio_io_handle_t> outputs; | 
 | 75 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 76 |         ssize_t index = mAvailableOutputDevices.indexOf(devDesc); | 
 | 77 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 78 |         // save a copy of the opened output descriptors before any output is opened or closed | 
 | 79 |         // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() | 
 | 80 |         mPreviousOutputs = mOutputs; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 81 |         switch (state) | 
 | 82 |         { | 
 | 83 |         // handle output device connection | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 84 |         case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 85 |             if (index >= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 86 |                 ALOGW("setDeviceConnectionState() device already connected: %x", device); | 
 | 87 |                 return INVALID_OPERATION; | 
 | 88 |             } | 
 | 89 |             ALOGV("setDeviceConnectionState() connecting device %x", device); | 
 | 90 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 91 |             // register new device as available | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 92 |             index = mAvailableOutputDevices.add(devDesc); | 
 | 93 |             if (index >= 0) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 94 |                 sp<HwModule> module = mHwModules.getModuleForDevice(device); | 
| Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 95 |                 if (module == 0) { | 
 | 96 |                     ALOGD("setDeviceConnectionState() could not find HW module for device %08x", | 
 | 97 |                           device); | 
 | 98 |                     mAvailableOutputDevices.remove(devDesc); | 
 | 99 |                     return INVALID_OPERATION; | 
 | 100 |                 } | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 101 |                 mAvailableOutputDevices[index]->attach(module); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 102 |             } else { | 
 | 103 |                 return NO_MEMORY; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 104 |             } | 
 | 105 |  | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 106 |             if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 107 |                 mAvailableOutputDevices.remove(devDesc); | 
 | 108 |                 return INVALID_OPERATION; | 
 | 109 |             } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 110 |             // Propagate device availability to Engine | 
 | 111 |             mEngine->setDeviceConnectionState(devDesc, state); | 
 | 112 |  | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 113 |             // outputs should never be empty here | 
 | 114 |             ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" | 
 | 115 |                     "checkOutputsForDevice() returned no outputs but status OK"); | 
 | 116 |             ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", | 
 | 117 |                   outputs.size()); | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 118 |  | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 119 |             // Send connect to HALs | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 120 |             AudioParameter param = AudioParameter(devDesc->mAddress); | 
 | 121 |             param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); | 
 | 122 |             mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
 | 123 |  | 
 | 124 |             } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 125 |         // handle output device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 126 |         case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 127 |             if (index < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 128 |                 ALOGW("setDeviceConnectionState() device not connected: %x", device); | 
 | 129 |                 return INVALID_OPERATION; | 
 | 130 |             } | 
 | 131 |  | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 132 |             ALOGV("setDeviceConnectionState() disconnecting output device %x", device); | 
 | 133 |  | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 134 |             // Send Disconnect to HALs | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 135 |             AudioParameter param = AudioParameter(devDesc->mAddress); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 136 |             param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); | 
 | 137 |             mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
 | 138 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 139 |             // remove device from available output devices | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 140 |             mAvailableOutputDevices.remove(devDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 141 |  | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 142 |             checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 143 |  | 
 | 144 |             // Propagate device availability to Engine | 
 | 145 |             mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 146 |             } break; | 
 | 147 |  | 
 | 148 |         default: | 
 | 149 |             ALOGE("setDeviceConnectionState() invalid state: %x", state); | 
 | 150 |             return BAD_VALUE; | 
 | 151 |         } | 
 | 152 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 153 |         // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP | 
 | 154 |         // output is suspended before any tracks are moved to it | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 155 |         checkA2dpSuspend(); | 
 | 156 |         checkOutputForAllStrategies(); | 
 | 157 |         // outputs must be closed after checkOutputForAllStrategies() is executed | 
 | 158 |         if (!outputs.isEmpty()) { | 
 | 159 |             for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 160 |                 sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 161 |                 // close unused outputs after device disconnection or direct outputs that have been | 
 | 162 |                 // opened by checkOutputsForDevice() to query dynamic parameters | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 163 |                 if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 164 |                         (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && | 
 | 165 |                          (desc->mDirectOpenCount == 0))) { | 
 | 166 |                     closeOutput(outputs[i]); | 
 | 167 |                 } | 
 | 168 |             } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 169 |             // check again after closing A2DP output to reset mA2dpSuspended if needed | 
 | 170 |             checkA2dpSuspend(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 171 |         } | 
 | 172 |  | 
 | 173 |         updateDevicesAndOutputs(); | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 174 |         if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 175 |             audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
 | 176 |             updateCallRouting(newDevice); | 
 | 177 |         } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 178 |         for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 179 |             sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
 | 180 |             if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { | 
 | 181 |                 audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 182 |                 // do not force device change on duplicated output because if device is 0, it will | 
 | 183 |                 // also force a device 0 for the two outputs it is duplicated to which may override | 
 | 184 |                 // a valid device selection on those outputs. | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 185 |                 bool force = !desc->isDuplicated() | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 186 |                         && (!device_distinguishes_on_address(device) | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 187 |                                 // always force when disconnecting (a non-duplicated device) | 
 | 188 |                                 || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 189 |                 setOutputDevice(desc, newDevice, force, 0); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 190 |             } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 191 |         } | 
 | 192 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 193 |         if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
 | 194 |             cleanUpForDevice(devDesc); | 
 | 195 |         } | 
 | 196 |  | 
| Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 197 |         mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 198 |         return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 199 |     }  // end if is output device | 
 | 200 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 201 |     // handle input devices | 
 | 202 |     if (audio_is_input_device(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 203 |         SortedVector <audio_io_handle_t> inputs; | 
 | 204 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 205 |         ssize_t index = mAvailableInputDevices.indexOf(devDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 206 |         switch (state) | 
 | 207 |         { | 
 | 208 |         // handle input device connection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 209 |         case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 210 |             if (index >= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 211 |                 ALOGW("setDeviceConnectionState() device already connected: %d", device); | 
 | 212 |                 return INVALID_OPERATION; | 
 | 213 |             } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 214 |             sp<HwModule> module = mHwModules.getModuleForDevice(device); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 215 |             if (module == NULL) { | 
 | 216 |                 ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", | 
 | 217 |                       device); | 
 | 218 |                 return INVALID_OPERATION; | 
 | 219 |             } | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 220 |             if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 221 |                 return INVALID_OPERATION; | 
 | 222 |             } | 
 | 223 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 224 |             index = mAvailableInputDevices.add(devDesc); | 
 | 225 |             if (index >= 0) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 226 |                 mAvailableInputDevices[index]->attach(module); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 227 |             } else { | 
 | 228 |                 return NO_MEMORY; | 
 | 229 |             } | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 230 |  | 
 | 231 |             // Set connect to HALs | 
 | 232 |             AudioParameter param = AudioParameter(devDesc->mAddress); | 
 | 233 |             param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); | 
 | 234 |             mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
 | 235 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 236 |             // Propagate device availability to Engine | 
 | 237 |             mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 238 |         } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 239 |  | 
 | 240 |         // handle input device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 241 |         case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 242 |             if (index < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 243 |                 ALOGW("setDeviceConnectionState() device not connected: %d", device); | 
 | 244 |                 return INVALID_OPERATION; | 
 | 245 |             } | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 246 |  | 
 | 247 |             ALOGV("setDeviceConnectionState() disconnecting input device %x", device); | 
 | 248 |  | 
 | 249 |             // Set Disconnect to HALs | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 250 |             AudioParameter param = AudioParameter(devDesc->mAddress); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 251 |             param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); | 
 | 252 |             mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
 | 253 |  | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 254 |             checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 255 |             mAvailableInputDevices.remove(devDesc); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 256 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 257 |             // Propagate device availability to Engine | 
 | 258 |             mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 259 |         } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 260 |  | 
 | 261 |         default: | 
 | 262 |             ALOGE("setDeviceConnectionState() invalid state: %x", state); | 
 | 263 |             return BAD_VALUE; | 
 | 264 |         } | 
 | 265 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 266 |         closeAllInputs(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 267 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 268 |         if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 269 |             audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
 | 270 |             updateCallRouting(newDevice); | 
 | 271 |         } | 
 | 272 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 273 |         if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
 | 274 |             cleanUpForDevice(devDesc); | 
 | 275 |         } | 
 | 276 |  | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 277 |         mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 278 |         return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 279 |     } // end if is input device | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 280 |  | 
 | 281 |     ALOGW("setDeviceConnectionState() invalid device: %x", device); | 
 | 282 |     return BAD_VALUE; | 
 | 283 | } | 
 | 284 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 285 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 286 |                                                                       const char *device_address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 287 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 288 |     sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, ""); | 
 | 289 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 290 |     DeviceVector *deviceVector; | 
 | 291 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 292 |     if (audio_is_output_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 293 |         deviceVector = &mAvailableOutputDevices; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 294 |     } else if (audio_is_input_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 295 |         deviceVector = &mAvailableInputDevices; | 
 | 296 |     } else { | 
 | 297 |         ALOGW("getDeviceConnectionState() invalid device type %08x", device); | 
 | 298 |         return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 299 |     } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 300 |     return deviceVector->getDeviceConnectionState(devDesc); | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 301 | } | 
 | 302 |  | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 303 | void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs) | 
 | 304 | { | 
 | 305 |     bool createTxPatch = false; | 
 | 306 |     struct audio_patch patch; | 
 | 307 |     patch.num_sources = 1; | 
 | 308 |     patch.num_sinks = 1; | 
 | 309 |     status_t status; | 
 | 310 |     audio_patch_handle_t afPatchHandle; | 
 | 311 |     DeviceVector deviceList; | 
 | 312 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 313 |     if(!hasPrimaryOutput()) { | 
 | 314 |         return; | 
 | 315 |     } | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 316 |     audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 317 |     ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); | 
 | 318 |  | 
 | 319 |     // release existing RX patch if any | 
 | 320 |     if (mCallRxPatch != 0) { | 
 | 321 |         mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); | 
 | 322 |         mCallRxPatch.clear(); | 
 | 323 |     } | 
 | 324 |     // release TX patch if any | 
 | 325 |     if (mCallTxPatch != 0) { | 
 | 326 |         mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); | 
 | 327 |         mCallTxPatch.clear(); | 
 | 328 |     } | 
 | 329 |  | 
 | 330 |     // If the RX device is on the primary HW module, then use legacy routing method for voice calls | 
 | 331 |     // via setOutputDevice() on primary output. | 
 | 332 |     // Otherwise, create two audio patches for TX and RX path. | 
 | 333 |     if (availablePrimaryOutputDevices() & rxDevice) { | 
 | 334 |         setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); | 
 | 335 |         // If the TX device is also on the primary HW module, setOutputDevice() will take care | 
 | 336 |         // of it due to legacy implementation. If not, create a patch. | 
 | 337 |         if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) | 
 | 338 |                 == AUDIO_DEVICE_NONE) { | 
 | 339 |             createTxPatch = true; | 
 | 340 |         } | 
 | 341 |     } else { | 
 | 342 |         // create RX path audio patch | 
 | 343 |         deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); | 
 | 344 |         ALOG_ASSERT(!deviceList.isEmpty(), | 
 | 345 |                     "updateCallRouting() selected device not in output device list"); | 
 | 346 |         sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); | 
 | 347 |         deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); | 
 | 348 |         ALOG_ASSERT(!deviceList.isEmpty(), | 
 | 349 |                     "updateCallRouting() no telephony RX device"); | 
 | 350 |         sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); | 
 | 351 |  | 
 | 352 |         rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); | 
 | 353 |         rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); | 
 | 354 |  | 
 | 355 |         // request to reuse existing output stream if one is already opened to reach the RX device | 
 | 356 |         SortedVector<audio_io_handle_t> outputs = | 
 | 357 |                                 getOutputsForDevice(rxDevice, mOutputs); | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 358 |         audio_io_handle_t output = selectOutput(outputs, | 
 | 359 |                                                 AUDIO_OUTPUT_FLAG_NONE, | 
 | 360 |                                                 AUDIO_FORMAT_INVALID); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 361 |         if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 362 |             sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 363 |             ALOG_ASSERT(!outputDesc->isDuplicated(), | 
 | 364 |                         "updateCallRouting() RX device output is duplicated"); | 
 | 365 |             outputDesc->toAudioPortConfig(&patch.sources[1]); | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 366 |             patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 367 |             patch.num_sources = 2; | 
 | 368 |         } | 
 | 369 |  | 
 | 370 |         afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
 | 371 |         status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); | 
 | 372 |         ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", | 
 | 373 |                                                status); | 
 | 374 |         if (status == NO_ERROR) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 375 |             mCallRxPatch = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 376 |             mCallRxPatch->mAfPatchHandle = afPatchHandle; | 
 | 377 |             mCallRxPatch->mUid = mUidCached; | 
 | 378 |         } | 
 | 379 |         createTxPatch = true; | 
 | 380 |     } | 
 | 381 |     if (createTxPatch) { | 
 | 382 |  | 
 | 383 |         struct audio_patch patch; | 
 | 384 |         patch.num_sources = 1; | 
 | 385 |         patch.num_sinks = 1; | 
 | 386 |         deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); | 
 | 387 |         ALOG_ASSERT(!deviceList.isEmpty(), | 
 | 388 |                     "updateCallRouting() selected device not in input device list"); | 
 | 389 |         sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); | 
 | 390 |         txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); | 
 | 391 |         deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); | 
 | 392 |         ALOG_ASSERT(!deviceList.isEmpty(), | 
 | 393 |                     "updateCallRouting() no telephony TX device"); | 
 | 394 |         sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); | 
 | 395 |         txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); | 
 | 396 |  | 
 | 397 |         SortedVector<audio_io_handle_t> outputs = | 
 | 398 |                                 getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 399 |         audio_io_handle_t output = selectOutput(outputs, | 
 | 400 |                                                 AUDIO_OUTPUT_FLAG_NONE, | 
 | 401 |                                                 AUDIO_FORMAT_INVALID); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 402 |         // request to reuse existing output stream if one is already opened to reach the TX | 
 | 403 |         // path output device | 
 | 404 |         if (output != AUDIO_IO_HANDLE_NONE) { | 
 | 405 |             sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
 | 406 |             ALOG_ASSERT(!outputDesc->isDuplicated(), | 
 | 407 |                         "updateCallRouting() RX device output is duplicated"); | 
 | 408 |             outputDesc->toAudioPortConfig(&patch.sources[1]); | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 409 |             patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 410 |             patch.num_sources = 2; | 
 | 411 |         } | 
 | 412 |  | 
| Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 413 |         // terminate active capture if on the same HW module as the call TX source device | 
 | 414 |         // FIXME: would be better to refine to only inputs whose profile connects to the | 
 | 415 |         // call TX device but this information is not in the audio patch and logic here must be | 
 | 416 |         // symmetric to the one in startInput() | 
 | 417 |         audio_io_handle_t activeInput = mInputs.getActiveInput(); | 
 | 418 |         if (activeInput != 0) { | 
 | 419 |             sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); | 
 | 420 |             if (activeDesc->getModuleHandle() == txSourceDeviceDesc->getModuleHandle()) { | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 421 |                 //FIXME: consider all active sessions | 
 | 422 |                 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions(); | 
 | 423 |                 audio_session_t activeSession = activeSessions.keyAt(0); | 
| Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 424 |                 stopInput(activeInput, activeSession); | 
 | 425 |                 releaseInput(activeInput, activeSession); | 
 | 426 |             } | 
 | 427 |         } | 
 | 428 |  | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 429 |         afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
 | 430 |         status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); | 
 | 431 |         ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", | 
 | 432 |                                                status); | 
 | 433 |         if (status == NO_ERROR) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 434 |             mCallTxPatch = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 435 |             mCallTxPatch->mAfPatchHandle = afPatchHandle; | 
 | 436 |             mCallTxPatch->mUid = mUidCached; | 
 | 437 |         } | 
 | 438 |     } | 
 | 439 | } | 
 | 440 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 441 | void AudioPolicyManager::setPhoneState(audio_mode_t state) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 442 | { | 
 | 443 |     ALOGV("setPhoneState() state %d", state); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 444 |     // store previous phone state for management of sonification strategy below | 
 | 445 |     int oldState = mEngine->getPhoneState(); | 
 | 446 |  | 
 | 447 |     if (mEngine->setPhoneState(state) != NO_ERROR) { | 
 | 448 |         ALOGW("setPhoneState() invalid or same state %d", state); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 449 |         return; | 
 | 450 |     } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 451 |     /// Opens: can these line be executed after the switch of volume curves??? | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 452 |     // if leaving call state, handle special case of active streams | 
 | 453 |     // pertaining to sonification strategy see handleIncallSonification() | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 454 |     if (isStateInCall(oldState)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 455 |         ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 456 |         for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 457 |             if (stream == AUDIO_STREAM_PATCH) { | 
 | 458 |                 continue; | 
 | 459 |             } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 460 |             handleIncallSonification((audio_stream_type_t)stream, false, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 461 |         } | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 462 |  | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 463 |         // force reevaluating accessibility routing when call stops | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 464 |         mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 465 |     } | 
 | 466 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 467 |     /** | 
 | 468 |      * Switching to or from incall state or switching between telephony and VoIP lead to force | 
 | 469 |      * routing command. | 
 | 470 |      */ | 
 | 471 |     bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) | 
 | 472 |                   || (is_state_in_call(state) && (state != oldState))); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 473 |  | 
 | 474 |     // check for device and output changes triggered by new phone state | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 475 |     checkA2dpSuspend(); | 
 | 476 |     checkOutputForAllStrategies(); | 
 | 477 |     updateDevicesAndOutputs(); | 
 | 478 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 479 |     int delayMs = 0; | 
 | 480 |     if (isStateInCall(state)) { | 
 | 481 |         nsecs_t sysTime = systemTime(); | 
 | 482 |         for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 483 |             sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 484 |             // mute media and sonification strategies and delay device switch by the largest | 
 | 485 |             // latency of any output where either strategy is active. | 
 | 486 |             // 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] | 487 |             if ((isStrategyActive(desc, STRATEGY_MEDIA, | 
 | 488 |                                   SONIFICATION_HEADSET_MUSIC_DELAY, | 
 | 489 |                                   sysTime) || | 
 | 490 |                  isStrategyActive(desc, STRATEGY_SONIFICATION, | 
 | 491 |                                   SONIFICATION_HEADSET_MUSIC_DELAY, | 
 | 492 |                                   sysTime)) && | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 493 |                     (delayMs < (int)desc->latency()*2)) { | 
 | 494 |                 delayMs = desc->latency()*2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 495 |             } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 496 |             setStrategyMute(STRATEGY_MEDIA, true, desc); | 
 | 497 |             setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 498 |                 getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 499 |             setStrategyMute(STRATEGY_SONIFICATION, true, desc); | 
 | 500 |             setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 501 |                 getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); | 
 | 502 |         } | 
 | 503 |     } | 
 | 504 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 505 |     if (hasPrimaryOutput()) { | 
 | 506 |         // Note that despite the fact that getNewOutputDevice() is called on the primary output, | 
 | 507 |         // the device returned is not necessarily reachable via this output | 
 | 508 |         audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
 | 509 |         // force routing command to audio hardware when ending call | 
 | 510 |         // even if no device change is needed | 
 | 511 |         if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { | 
 | 512 |             rxDevice = mPrimaryOutput->device(); | 
 | 513 |         } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 514 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 515 |         if (state == AUDIO_MODE_IN_CALL) { | 
 | 516 |             updateCallRouting(rxDevice, delayMs); | 
 | 517 |         } else if (oldState == AUDIO_MODE_IN_CALL) { | 
 | 518 |             if (mCallRxPatch != 0) { | 
 | 519 |                 mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); | 
 | 520 |                 mCallRxPatch.clear(); | 
 | 521 |             } | 
 | 522 |             if (mCallTxPatch != 0) { | 
 | 523 |                 mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); | 
 | 524 |                 mCallTxPatch.clear(); | 
 | 525 |             } | 
 | 526 |             setOutputDevice(mPrimaryOutput, rxDevice, force, 0); | 
 | 527 |         } else { | 
 | 528 |             setOutputDevice(mPrimaryOutput, rxDevice, force, 0); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 529 |         } | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 530 |     } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 531 |     // if entering in call state, handle special case of active streams | 
 | 532 |     // pertaining to sonification strategy see handleIncallSonification() | 
 | 533 |     if (isStateInCall(state)) { | 
 | 534 |         ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 535 |         for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 536 |             if (stream == AUDIO_STREAM_PATCH) { | 
 | 537 |                 continue; | 
 | 538 |             } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 539 |             handleIncallSonification((audio_stream_type_t)stream, true, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 540 |         } | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 541 |  | 
 | 542 |         // force reevaluating accessibility routing when call starts | 
 | 543 |         mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 544 |     } | 
 | 545 |  | 
 | 546 |     // 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] | 547 |     if (state == AUDIO_MODE_RINGTONE && | 
 | 548 |         isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 549 |         mLimitRingtoneVolume = true; | 
 | 550 |     } else { | 
 | 551 |         mLimitRingtoneVolume = false; | 
 | 552 |     } | 
 | 553 | } | 
 | 554 |  | 
| Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 555 | audio_mode_t AudioPolicyManager::getPhoneState() { | 
 | 556 |     return mEngine->getPhoneState(); | 
 | 557 | } | 
 | 558 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 559 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 560 |                                          audio_policy_forced_cfg_t config) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 561 | { | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 562 |     ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 563 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 564 |     if (mEngine->setForceUse(usage, config) != NO_ERROR) { | 
 | 565 |         ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); | 
 | 566 |         return; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 567 |     } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 568 |     bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || | 
 | 569 |             (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || | 
 | 570 |             (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 571 |  | 
 | 572 |     // check for device and output changes triggered by new force usage | 
 | 573 |     checkA2dpSuspend(); | 
 | 574 |     checkOutputForAllStrategies(); | 
 | 575 |     updateDevicesAndOutputs(); | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 576 |     if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 577 |         audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); | 
 | 578 |         updateCallRouting(newDevice); | 
 | 579 |     } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 580 |     for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 581 |         sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); | 
 | 582 |         audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); | 
 | 583 |         if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { | 
 | 584 |             setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE)); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 585 |         } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 586 |         if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 587 |             applyStreamVolumes(outputDesc, newDevice, 0, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 588 |         } | 
 | 589 |     } | 
 | 590 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 591 |     audio_io_handle_t activeInput = mInputs.getActiveInput(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 592 |     if (activeInput != 0) { | 
| Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 593 |         sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); | 
 | 594 |         audio_devices_t newDevice = getNewInputDevice(activeInput); | 
 | 595 |         // Force new input selection if the new device can not be reached via current input | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 596 |         if (activeDesc->mProfile->getSupportedDevices().types() & (newDevice & ~AUDIO_DEVICE_BIT_IN)) { | 
| Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 597 |             setInputDevice(activeInput, newDevice); | 
 | 598 |         } else { | 
 | 599 |             closeInput(activeInput); | 
 | 600 |         } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 601 |     } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 602 | } | 
 | 603 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 604 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 605 | { | 
 | 606 |     ALOGV("setSystemProperty() property %s, value %s", property, value); | 
 | 607 | } | 
 | 608 |  | 
 | 609 | // Find a direct output profile compatible with the parameters passed, even if the input flags do | 
 | 610 | // not explicitly request a direct output | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 611 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 612 |                                                                audio_devices_t device, | 
 | 613 |                                                                uint32_t samplingRate, | 
 | 614 |                                                                audio_format_t format, | 
 | 615 |                                                                audio_channel_mask_t channelMask, | 
 | 616 |                                                                audio_output_flags_t flags) | 
 | 617 | { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 618 |     // only retain flags that will drive the direct output profile selection | 
 | 619 |     // if explicitly requested | 
 | 620 |     static const uint32_t kRelevantFlags = | 
 | 621 |             (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
 | 622 |     flags = | 
 | 623 |         (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); | 
 | 624 |  | 
 | 625 |     sp<IOProfile> profile; | 
 | 626 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 627 |     for (size_t i = 0; i < mHwModules.size(); i++) { | 
 | 628 |         if (mHwModules[i]->mHandle == 0) { | 
 | 629 |             continue; | 
 | 630 |         } | 
 | 631 |         for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 632 |             sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j]; | 
 | 633 |             if (!curProfile->isCompatibleProfile(device, String8(""), | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 634 |                     samplingRate, NULL /*updatedSamplingRate*/, | 
 | 635 |                     format, NULL /*updatedFormat*/, | 
 | 636 |                     channelMask, NULL /*updatedChannelMask*/, | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 637 |                     flags)) { | 
 | 638 |                 continue; | 
 | 639 |             } | 
 | 640 |             // reject profiles not corresponding to a device currently available | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 641 |             if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 642 |                 continue; | 
 | 643 |             } | 
 | 644 |             // 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] | 645 |             if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 646 |                 continue; | 
 | 647 |             } | 
 | 648 |             profile = curProfile; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 649 |             if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 650 |                 break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 651 |             } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 652 |         } | 
 | 653 |     } | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 654 |     return profile; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 655 | } | 
 | 656 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 657 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 658 |                                                 uint32_t samplingRate, | 
 | 659 |                                                 audio_format_t format, | 
 | 660 |                                                 audio_channel_mask_t channelMask, | 
 | 661 |                                                 audio_output_flags_t flags, | 
 | 662 |                                                 const audio_offload_info_t *offloadInfo) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 663 | { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 664 |     routing_strategy strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 665 |     audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
 | 666 |     ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", | 
 | 667 |           device, stream, samplingRate, format, channelMask, flags); | 
 | 668 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 669 |     return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, | 
 | 670 |                               stream, samplingRate,format, channelMask, | 
 | 671 |                               flags, offloadInfo); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 672 | } | 
 | 673 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 674 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, | 
 | 675 |                                               audio_io_handle_t *output, | 
 | 676 |                                               audio_session_t session, | 
 | 677 |                                               audio_stream_type_t *stream, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 678 |                                               uid_t uid, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 679 |                                               uint32_t samplingRate, | 
 | 680 |                                               audio_format_t format, | 
 | 681 |                                               audio_channel_mask_t channelMask, | 
 | 682 |                                               audio_output_flags_t flags, | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 683 |                                               audio_port_handle_t selectedDeviceId, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 684 |                                               const audio_offload_info_t *offloadInfo) | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 685 | { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 686 |     audio_attributes_t attributes; | 
 | 687 |     if (attr != NULL) { | 
 | 688 |         if (!isValidAttributes(attr)) { | 
 | 689 |             ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", | 
 | 690 |                   attr->usage, attr->content_type, attr->flags, | 
 | 691 |                   attr->tags); | 
 | 692 |             return BAD_VALUE; | 
 | 693 |         } | 
 | 694 |         attributes = *attr; | 
 | 695 |     } else { | 
 | 696 |         if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { | 
 | 697 |             ALOGE("getOutputForAttr():  invalid stream type"); | 
 | 698 |             return BAD_VALUE; | 
 | 699 |         } | 
 | 700 |         stream_type_to_audio_attributes(*stream, &attributes); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 701 |     } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 702 |     sp<SwAudioOutputDescriptor> desc; | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 703 |     if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) { | 
 | 704 |         ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); | 
 | 705 |         if (!audio_is_linear_pcm(format)) { | 
 | 706 |             return BAD_VALUE; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 707 |         } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 708 |         *stream = streamTypefromAttributesInt(&attributes); | 
 | 709 |         *output = desc->mIoHandle; | 
 | 710 |         ALOGV("getOutputForAttr() returns output %d", *output); | 
 | 711 |         return NO_ERROR; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 712 |     } | 
 | 713 |     if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { | 
 | 714 |         ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); | 
 | 715 |         return BAD_VALUE; | 
 | 716 |     } | 
 | 717 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 718 |     ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" | 
 | 719 |             " session %d selectedDeviceId %d", | 
 | 720 |             attributes.usage, attributes.content_type, attributes.tags, attributes.flags, | 
 | 721 |             session, selectedDeviceId); | 
 | 722 |  | 
 | 723 |     *stream = streamTypefromAttributesInt(&attributes); | 
 | 724 |  | 
 | 725 |     // Explicit routing? | 
 | 726 |     sp<DeviceDescriptor> deviceDesc; | 
 | 727 |     for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { | 
 | 728 |         if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) { | 
 | 729 |             deviceDesc = mAvailableOutputDevices[i]; | 
 | 730 |             break; | 
 | 731 |         } | 
 | 732 |     } | 
 | 733 |     mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 734 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 735 |     routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 736 |     audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 737 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 738 |     if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 739 |         flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); | 
 | 740 |     } | 
 | 741 |  | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 742 |     ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x", | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 743 |           device, samplingRate, format, channelMask, flags); | 
 | 744 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 745 |     *output = getOutputForDevice(device, session, *stream, | 
 | 746 |                                  samplingRate, format, channelMask, | 
 | 747 |                                  flags, offloadInfo); | 
 | 748 |     if (*output == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 749 |         mOutputRoutes.removeRoute(session); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 750 |         return INVALID_OPERATION; | 
 | 751 |     } | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 752 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 753 |     return NO_ERROR; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 754 | } | 
 | 755 |  | 
 | 756 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( | 
 | 757 |         audio_devices_t device, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 758 |         audio_session_t session __unused, | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 759 |         audio_stream_type_t stream, | 
 | 760 |         uint32_t samplingRate, | 
 | 761 |         audio_format_t format, | 
 | 762 |         audio_channel_mask_t channelMask, | 
 | 763 |         audio_output_flags_t flags, | 
 | 764 |         const audio_offload_info_t *offloadInfo) | 
 | 765 | { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 766 |     audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 767 |     uint32_t latency = 0; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 768 |     status_t status; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 769 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 770 | #ifdef AUDIO_POLICY_TEST | 
 | 771 |     if (mCurOutput != 0) { | 
 | 772 |         ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", | 
 | 773 |                 mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); | 
 | 774 |  | 
 | 775 |         if (mTestOutputs[mCurOutput] == 0) { | 
 | 776 |             ALOGV("getOutput() opening test output"); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 777 |             sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL, | 
 | 778 |                                                                                mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 779 |             outputDesc->mDevice = mTestDevice; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 780 |             outputDesc->mLatency = mTestLatencyMs; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 781 |             outputDesc->mFlags = | 
 | 782 |                     (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 783 |             outputDesc->mRefCount[stream] = 0; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 784 |             audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
 | 785 |             config.sample_rate = mTestSamplingRate; | 
 | 786 |             config.channel_mask = mTestChannels; | 
 | 787 |             config.format = mTestFormat; | 
| Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 788 |             if (offloadInfo != NULL) { | 
 | 789 |                 config.offload_info = *offloadInfo; | 
 | 790 |             } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 791 |             status = mpClientInterface->openOutput(0, | 
 | 792 |                                                   &mTestOutputs[mCurOutput], | 
 | 793 |                                                   &config, | 
 | 794 |                                                   &outputDesc->mDevice, | 
 | 795 |                                                   String8(""), | 
 | 796 |                                                   &outputDesc->mLatency, | 
 | 797 |                                                   outputDesc->mFlags); | 
 | 798 |             if (status == NO_ERROR) { | 
 | 799 |                 outputDesc->mSamplingRate = config.sample_rate; | 
 | 800 |                 outputDesc->mFormat = config.format; | 
 | 801 |                 outputDesc->mChannelMask = config.channel_mask; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 802 |                 AudioParameter outputCmd = AudioParameter(); | 
 | 803 |                 outputCmd.addInt(String8("set_id"),mCurOutput); | 
 | 804 |                 mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); | 
 | 805 |                 addOutput(mTestOutputs[mCurOutput], outputDesc); | 
 | 806 |             } | 
 | 807 |         } | 
 | 808 |         return mTestOutputs[mCurOutput]; | 
 | 809 |     } | 
 | 810 | #endif //AUDIO_POLICY_TEST | 
 | 811 |  | 
 | 812 |     // open a direct output if required by specified parameters | 
 | 813 |     //force direct flag if offload flag is set: offloading implies a direct output stream | 
 | 814 |     // and all common behaviors are driven by checking only the direct flag | 
 | 815 |     // this should normally be set appropriately in the policy configuration file | 
 | 816 |     if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 817 |         flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 818 |     } | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 819 |     if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { | 
 | 820 |         flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
 | 821 |     } | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 822 |     // only allow deep buffering for music stream type | 
 | 823 |     if (stream != AUDIO_STREAM_MUSIC) { | 
 | 824 |         flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); | 
| Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 825 |     } else if (/* stream == AUDIO_STREAM_MUSIC && */ | 
 | 826 |             flags == AUDIO_OUTPUT_FLAG_NONE && | 
 | 827 |             property_get_bool("audio.deep_buffer.media", false /* default_value */)) { | 
 | 828 |         // use DEEP_BUFFER as default output for music stream type | 
 | 829 |         flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 830 |     } | 
| Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 831 |     if (stream == AUDIO_STREAM_TTS) { | 
 | 832 |         flags = AUDIO_OUTPUT_FLAG_TTS; | 
 | 833 |     } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 834 |  | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 835 |     sp<IOProfile> profile; | 
 | 836 |  | 
 | 837 |     // 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] | 838 |     // and not explicitly requested | 
 | 839 |     if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && | 
 | 840 |             audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE && | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 841 |             audio_channel_count_from_out_mask(channelMask) <= 2) { | 
 | 842 |         goto non_direct_output; | 
 | 843 |     } | 
 | 844 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame^] | 845 |     // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. | 
 | 846 |     // This prevents creating an offloaded track and tearing it down immediately after start | 
 | 847 |     // when audioflinger detects there is an active non offloadable effect. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 848 |     // FIXME: We should check the audio session here but we do not have it in this context. | 
 | 849 |     // This may prevent offloading in rare situations where effects are left active by apps | 
 | 850 |     // in the background. | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 851 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 852 |     if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame^] | 853 |             !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 854 |         profile = getProfileForDirectOutput(device, | 
 | 855 |                                            samplingRate, | 
 | 856 |                                            format, | 
 | 857 |                                            channelMask, | 
 | 858 |                                            (audio_output_flags_t)flags); | 
 | 859 |     } | 
 | 860 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 861 |     if (profile != 0) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 862 |         sp<SwAudioOutputDescriptor> outputDesc = NULL; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 863 |  | 
 | 864 |         for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 865 |             sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 866 |             if (!desc->isDuplicated() && (profile == desc->mProfile)) { | 
 | 867 |                 outputDesc = desc; | 
 | 868 |                 // reuse direct output if currently open and configured with same parameters | 
 | 869 |                 if ((samplingRate == outputDesc->mSamplingRate) && | 
 | 870 |                         (format == outputDesc->mFormat) && | 
 | 871 |                         (channelMask == outputDesc->mChannelMask)) { | 
 | 872 |                     outputDesc->mDirectOpenCount++; | 
 | 873 |                     ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); | 
 | 874 |                     return mOutputs.keyAt(i); | 
 | 875 |                 } | 
 | 876 |             } | 
 | 877 |         } | 
 | 878 |         // close direct output if currently open and configured with different parameters | 
 | 879 |         if (outputDesc != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 880 |             closeOutput(outputDesc->mIoHandle); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 881 |         } | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 882 |  | 
 | 883 |         // if the selected profile is offloaded and no offload info was specified, | 
 | 884 |         // create a default one | 
 | 885 |         audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 886 |         if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 887 |             flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
 | 888 |             defaultOffloadInfo.sample_rate = samplingRate; | 
 | 889 |             defaultOffloadInfo.channel_mask = channelMask; | 
 | 890 |             defaultOffloadInfo.format = format; | 
 | 891 |             defaultOffloadInfo.stream_type = stream; | 
 | 892 |             defaultOffloadInfo.bit_rate = 0; | 
 | 893 |             defaultOffloadInfo.duration_us = -1; | 
 | 894 |             defaultOffloadInfo.has_video = true; // conservative | 
 | 895 |             defaultOffloadInfo.is_streaming = true; // likely | 
 | 896 |             offloadInfo = &defaultOffloadInfo; | 
 | 897 |         } | 
 | 898 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 899 |         outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 900 |         outputDesc->mDevice = device; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 901 |         outputDesc->mLatency = 0; | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 902 |         outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 903 |         audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
 | 904 |         config.sample_rate = samplingRate; | 
 | 905 |         config.channel_mask = channelMask; | 
 | 906 |         config.format = format; | 
| Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 907 |         if (offloadInfo != NULL) { | 
 | 908 |             config.offload_info = *offloadInfo; | 
 | 909 |         } | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 910 |         status = mpClientInterface->openOutput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 911 |                                                &output, | 
 | 912 |                                                &config, | 
 | 913 |                                                &outputDesc->mDevice, | 
 | 914 |                                                String8(""), | 
 | 915 |                                                &outputDesc->mLatency, | 
 | 916 |                                                outputDesc->mFlags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 917 |  | 
 | 918 |         // only accept an output with the requested parameters | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 919 |         if (status != NO_ERROR || | 
 | 920 |             (samplingRate != 0 && samplingRate != config.sample_rate) || | 
 | 921 |             (format != AUDIO_FORMAT_DEFAULT && format != config.format) || | 
 | 922 |             (channelMask != 0 && channelMask != config.channel_mask)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 923 |             ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," | 
 | 924 |                     "format %d %d, channelMask %04x %04x", output, samplingRate, | 
 | 925 |                     outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, | 
 | 926 |                     outputDesc->mChannelMask); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 927 |             if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 928 |                 mpClientInterface->closeOutput(output); | 
 | 929 |             } | 
| Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 930 |             // fall back to mixer output if possible when the direct output could not be open | 
 | 931 |             if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) { | 
 | 932 |                 goto non_direct_output; | 
 | 933 |             } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 934 |             return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 935 |         } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 936 |         outputDesc->mSamplingRate = config.sample_rate; | 
 | 937 |         outputDesc->mChannelMask = config.channel_mask; | 
 | 938 |         outputDesc->mFormat = config.format; | 
 | 939 |         outputDesc->mRefCount[stream] = 0; | 
 | 940 |         outputDesc->mStopTime[stream] = 0; | 
 | 941 |         outputDesc->mDirectOpenCount = 1; | 
 | 942 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 943 |         audio_io_handle_t srcOutput = getOutputForEffect(); | 
 | 944 |         addOutput(output, outputDesc); | 
 | 945 |         audio_io_handle_t dstOutput = getOutputForEffect(); | 
 | 946 |         if (dstOutput == output) { | 
 | 947 |             mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); | 
 | 948 |         } | 
 | 949 |         mPreviousOutputs = mOutputs; | 
 | 950 |         ALOGV("getOutput() returns new direct output %d", output); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 951 |         mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 952 |         return output; | 
 | 953 |     } | 
 | 954 |  | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 955 | non_direct_output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 956 |     // ignoring channel mask due to downmix capability in mixer | 
 | 957 |  | 
 | 958 |     // open a non direct output | 
 | 959 |  | 
 | 960 |     // for non direct outputs, only PCM is supported | 
 | 961 |     if (audio_is_linear_pcm(format)) { | 
 | 962 |         // get which output is suitable for the specified stream. The actual | 
 | 963 |         // routing change will happen when startOutput() will be called | 
 | 964 |         SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
 | 965 |  | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 966 |         // at this stage we should ignore the DIRECT flag as no direct output could be found earlier | 
 | 967 |         flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); | 
 | 968 |         output = selectOutput(outputs, flags, format); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 969 |     } | 
 | 970 |     ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," | 
 | 971 |             "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); | 
 | 972 |  | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 973 |     ALOGV("  getOutputForDevice() returns output %d", output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 974 |  | 
 | 975 |     return output; | 
 | 976 | } | 
 | 977 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 978 | 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] | 979 |                                                        audio_output_flags_t flags, | 
 | 980 |                                                        audio_format_t format) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 981 | { | 
 | 982 |     // select one output among several that provide a path to a particular device or set of | 
 | 983 |     // devices (the list was previously build by getOutputsForDevice()). | 
 | 984 |     // The priority is as follows: | 
 | 985 |     // 1: the output with the highest number of requested policy flags | 
 | 986 |     // 2: the primary output | 
 | 987 |     // 3: the first output in the list | 
 | 988 |  | 
 | 989 |     if (outputs.size() == 0) { | 
 | 990 |         return 0; | 
 | 991 |     } | 
 | 992 |     if (outputs.size() == 1) { | 
 | 993 |         return outputs[0]; | 
 | 994 |     } | 
 | 995 |  | 
 | 996 |     int maxCommonFlags = 0; | 
 | 997 |     audio_io_handle_t outputFlags = 0; | 
 | 998 |     audio_io_handle_t outputPrimary = 0; | 
 | 999 |  | 
 | 1000 |     for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1001 |         sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1002 |         if (!outputDesc->isDuplicated()) { | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1003 |             // if a valid format is specified, skip output if not compatible | 
 | 1004 |             if (format != AUDIO_FORMAT_INVALID) { | 
 | 1005 |                 if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
 | 1006 |                     if (format != outputDesc->mFormat) { | 
 | 1007 |                         continue; | 
 | 1008 |                     } | 
 | 1009 |                 } else if (!audio_is_linear_pcm(format)) { | 
 | 1010 |                     continue; | 
 | 1011 |                 } | 
 | 1012 |             } | 
 | 1013 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1014 |             int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1015 |             if (commonFlags > maxCommonFlags) { | 
 | 1016 |                 outputFlags = outputs[i]; | 
 | 1017 |                 maxCommonFlags = commonFlags; | 
 | 1018 |                 ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); | 
 | 1019 |             } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1020 |             if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1021 |                 outputPrimary = outputs[i]; | 
 | 1022 |             } | 
 | 1023 |         } | 
 | 1024 |     } | 
 | 1025 |  | 
 | 1026 |     if (outputFlags != 0) { | 
 | 1027 |         return outputFlags; | 
 | 1028 |     } | 
 | 1029 |     if (outputPrimary != 0) { | 
 | 1030 |         return outputPrimary; | 
 | 1031 |     } | 
 | 1032 |  | 
 | 1033 |     return outputs[0]; | 
 | 1034 | } | 
 | 1035 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1036 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1037 |                                              audio_stream_type_t stream, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1038 |                                              audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1039 | { | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1040 |     ALOGV("startOutput() output %d, stream %d, session %d", | 
 | 1041 |           output, stream, session); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1042 |     ssize_t index = mOutputs.indexOfKey(output); | 
 | 1043 |     if (index < 0) { | 
 | 1044 |         ALOGW("startOutput() unknown output %d", output); | 
 | 1045 |         return BAD_VALUE; | 
 | 1046 |     } | 
 | 1047 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1048 |     sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
 | 1049 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1050 |     // Routing? | 
 | 1051 |     mOutputRoutes.incRouteActivity(session); | 
 | 1052 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1053 |     audio_devices_t newDevice; | 
 | 1054 |     if (outputDesc->mPolicyMix != NULL) { | 
 | 1055 |         newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; | 
| Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1056 |     } else if (mOutputRoutes.hasRouteChanged(session)) { | 
 | 1057 |         newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1058 |         checkStrategyRoute(getStrategy(stream), output); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1059 |     } else { | 
 | 1060 |         newDevice = AUDIO_DEVICE_NONE; | 
 | 1061 |     } | 
 | 1062 |  | 
 | 1063 |     uint32_t delayMs = 0; | 
 | 1064 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1065 |     status_t status = startSource(outputDesc, stream, newDevice, &delayMs); | 
 | 1066 |  | 
 | 1067 |     if (status != NO_ERROR) { | 
 | 1068 |         mOutputRoutes.decRouteActivity(session); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1069 |         return status; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1070 |     } | 
 | 1071 |     // Automatically enable the remote submix input when output is started on a re routing mix | 
 | 1072 |     // of type MIX_TYPE_RECORDERS | 
 | 1073 |     if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL && | 
 | 1074 |             outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { | 
 | 1075 |             setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
 | 1076 |                     AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
 | 1077 |                     outputDesc->mPolicyMix->mRegistrationId, | 
 | 1078 |                     "remote-submix"); | 
 | 1079 |     } | 
 | 1080 |  | 
 | 1081 |     if (delayMs != 0) { | 
 | 1082 |         usleep(delayMs * 1000); | 
 | 1083 |     } | 
 | 1084 |  | 
 | 1085 |     return status; | 
 | 1086 | } | 
 | 1087 |  | 
 | 1088 | status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc, | 
 | 1089 |                                              audio_stream_type_t stream, | 
 | 1090 |                                              audio_devices_t device, | 
 | 1091 |                                              uint32_t *delayMs) | 
 | 1092 | { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1093 |     // cannot start playback of STREAM_TTS if any other output is being used | 
 | 1094 |     uint32_t beaconMuteLatency = 0; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1095 |  | 
 | 1096 |     *delayMs = 0; | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1097 |     if (stream == AUDIO_STREAM_TTS) { | 
 | 1098 |         ALOGV("\t found BEACON stream"); | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1099 |         if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1100 |             return INVALID_OPERATION; | 
 | 1101 |         } else { | 
 | 1102 |             beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); | 
 | 1103 |         } | 
 | 1104 |     } else { | 
 | 1105 |         // some playback other than beacon starts | 
 | 1106 |         beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); | 
 | 1107 |     } | 
 | 1108 |  | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1109 |     // check active before incrementing usage count | 
 | 1110 |     bool force = !outputDesc->isActive(); | 
 | 1111 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1112 |     // increment usage count for this stream on the requested output: | 
 | 1113 |     // NOTE that the usage count is the same for duplicated output and hardware output which is | 
 | 1114 |     // necessary for a correct control of hardware output routing by startOutput() and stopOutput() | 
 | 1115 |     outputDesc->changeRefCount(stream, 1); | 
 | 1116 |  | 
| Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1117 |     if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1118 |         // starting an output being rerouted? | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1119 |         if (device == AUDIO_DEVICE_NONE) { | 
 | 1120 |             device = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1121 |         } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1122 |         routing_strategy strategy = getStrategy(stream); | 
 | 1123 |         bool shouldWait = (strategy == STRATEGY_SONIFICATION) || | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1124 |                             (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || | 
 | 1125 |                             (beaconMuteLatency > 0); | 
 | 1126 |         uint32_t waitMs = beaconMuteLatency; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1127 |         for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1128 |             sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1129 |             if (desc != outputDesc) { | 
 | 1130 |                 // force a device change if any other output is managed by the same hw | 
 | 1131 |                 // module and has a current device selection that differs from selected device. | 
 | 1132 |                 // In this case, the audio HAL must receive the new device selection so that it can | 
 | 1133 |                 // change the device currently selected by the other active output. | 
 | 1134 |                 if (outputDesc->sharesHwModuleWith(desc) && | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1135 |                     desc->device() != device) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1136 |                     force = true; | 
 | 1137 |                 } | 
 | 1138 |                 // 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] | 1139 |                 // a notification so that audio focus effect can propagate, or that a mute/unmute | 
 | 1140 |                 // event occurred for beacon | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1141 |                 uint32_t latency = desc->latency(); | 
 | 1142 |                 if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { | 
 | 1143 |                     waitMs = latency; | 
 | 1144 |                 } | 
 | 1145 |             } | 
 | 1146 |         } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1147 |         uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1148 |  | 
 | 1149 |         // handle special case for sonification while in call | 
 | 1150 |         if (isInCall()) { | 
 | 1151 |             handleIncallSonification(stream, true, false); | 
 | 1152 |         } | 
 | 1153 |  | 
 | 1154 |         // apply volume rules for current stream and device if necessary | 
 | 1155 |         checkAndSetVolume(stream, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1156 |                           mStreams.valueFor(stream).getVolumeIndex(device), | 
 | 1157 |                           outputDesc, | 
 | 1158 |                           device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1159 |  | 
 | 1160 |         // update the outputs if starting an output with a stream that can affect notification | 
 | 1161 |         // routing | 
 | 1162 |         handleNotificationRoutingForStream(stream); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1163 |  | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1164 |         // force reevaluating accessibility routing when ringtone or alarm starts | 
 | 1165 |         if (strategy == STRATEGY_SONIFICATION) { | 
 | 1166 |             mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
 | 1167 |         } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1168 |     } | 
 | 1169 |     return NO_ERROR; | 
 | 1170 | } | 
 | 1171 |  | 
 | 1172 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1173 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1174 |                                             audio_stream_type_t stream, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1175 |                                             audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1176 | { | 
 | 1177 |     ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); | 
 | 1178 |     ssize_t index = mOutputs.indexOfKey(output); | 
 | 1179 |     if (index < 0) { | 
 | 1180 |         ALOGW("stopOutput() unknown output %d", output); | 
 | 1181 |         return BAD_VALUE; | 
 | 1182 |     } | 
 | 1183 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1184 |     sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1185 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1186 |     if (outputDesc->mRefCount[stream] == 1) { | 
 | 1187 |         // Automatically disable the remote submix input when output is stopped on a | 
 | 1188 |         // re routing mix of type MIX_TYPE_RECORDERS | 
 | 1189 |         if (audio_is_remote_submix_device(outputDesc->mDevice) && | 
 | 1190 |                 outputDesc->mPolicyMix != NULL && | 
 | 1191 |                 outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { | 
 | 1192 |             setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
 | 1193 |                     AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
 | 1194 |                     outputDesc->mPolicyMix->mRegistrationId, | 
 | 1195 |                     "remote-submix"); | 
 | 1196 |         } | 
 | 1197 |     } | 
 | 1198 |  | 
 | 1199 |     // Routing? | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1200 |     bool forceDeviceUpdate = false; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1201 |     if (outputDesc->mRefCount[stream] > 0) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1202 |         int activityCount = mOutputRoutes.decRouteActivity(session); | 
 | 1203 |         forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); | 
 | 1204 |  | 
 | 1205 |         if (forceDeviceUpdate) { | 
 | 1206 |             checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); | 
 | 1207 |         } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1208 |     } | 
 | 1209 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1210 |     return stopSource(outputDesc, stream, forceDeviceUpdate); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1211 | } | 
 | 1212 |  | 
 | 1213 | status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1214 |                                             audio_stream_type_t stream, | 
 | 1215 |                                             bool forceDeviceUpdate) | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1216 | { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1217 |     // always handle stream stop, check which stream type is stopping | 
 | 1218 |     handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); | 
 | 1219 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1220 |     // handle special case for sonification while in call | 
 | 1221 |     if (isInCall()) { | 
 | 1222 |         handleIncallSonification(stream, false, false); | 
 | 1223 |     } | 
 | 1224 |  | 
 | 1225 |     if (outputDesc->mRefCount[stream] > 0) { | 
 | 1226 |         // decrement usage count of this stream on the output | 
 | 1227 |         outputDesc->changeRefCount(stream, -1); | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1228 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1229 |         // store time at which the stream was stopped - see isStreamActive() | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1230 |         if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1231 |             outputDesc->mStopTime[stream] = systemTime(); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1232 |             audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1233 |             // delay the device switch by twice the latency because stopOutput() is executed when | 
 | 1234 |             // the track stop() command is received and at that time the audio track buffer can | 
 | 1235 |             // still contain data that needs to be drained. The latency only covers the audio HAL | 
 | 1236 |             // and kernel buffers. Also the latency does not always include additional delay in the | 
 | 1237 |             // audio path (audio DSP, CODEC ...) | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1238 |             setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1239 |  | 
 | 1240 |             // force restoring the device selection on other active outputs if it differs from the | 
 | 1241 |             // one being selected for this output | 
 | 1242 |             for (size_t i = 0; i < mOutputs.size(); i++) { | 
 | 1243 |                 audio_io_handle_t curOutput = mOutputs.keyAt(i); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1244 |                 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1245 |                 if (desc != outputDesc && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1246 |                         desc->isActive() && | 
 | 1247 |                         outputDesc->sharesHwModuleWith(desc) && | 
 | 1248 |                         (newDevice != desc->device())) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1249 |                     setOutputDevice(desc, | 
 | 1250 |                                     getNewOutputDevice(desc, false /*fromCache*/), | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1251 |                                     true, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1252 |                                     outputDesc->latency()*2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1253 |                 } | 
 | 1254 |             } | 
 | 1255 |             // update the outputs if stopping one with a stream that can affect notification routing | 
 | 1256 |             handleNotificationRoutingForStream(stream); | 
 | 1257 |         } | 
 | 1258 |         return NO_ERROR; | 
 | 1259 |     } else { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1260 |         ALOGW("stopOutput() refcount is already 0"); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1261 |         return INVALID_OPERATION; | 
 | 1262 |     } | 
 | 1263 | } | 
 | 1264 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1265 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1266 |                                        audio_stream_type_t stream __unused, | 
 | 1267 |                                        audio_session_t session __unused) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1268 | { | 
 | 1269 |     ALOGV("releaseOutput() %d", output); | 
 | 1270 |     ssize_t index = mOutputs.indexOfKey(output); | 
 | 1271 |     if (index < 0) { | 
 | 1272 |         ALOGW("releaseOutput() releasing unknown output %d", output); | 
 | 1273 |         return; | 
 | 1274 |     } | 
 | 1275 |  | 
 | 1276 | #ifdef AUDIO_POLICY_TEST | 
 | 1277 |     int testIndex = testOutputIndex(output); | 
 | 1278 |     if (testIndex != 0) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1279 |         sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1280 |         if (outputDesc->isActive()) { | 
 | 1281 |             mpClientInterface->closeOutput(output); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1282 |             removeOutput(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1283 |             mTestOutputs[testIndex] = 0; | 
 | 1284 |         } | 
 | 1285 |         return; | 
 | 1286 |     } | 
 | 1287 | #endif //AUDIO_POLICY_TEST | 
 | 1288 |  | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1289 |     // Routing | 
 | 1290 |     mOutputRoutes.removeRoute(session); | 
 | 1291 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1292 |     sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1293 |     if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1294 |         if (desc->mDirectOpenCount <= 0) { | 
 | 1295 |             ALOGW("releaseOutput() invalid open count %d for output %d", | 
 | 1296 |                                                               desc->mDirectOpenCount, output); | 
 | 1297 |             return; | 
 | 1298 |         } | 
 | 1299 |         if (--desc->mDirectOpenCount == 0) { | 
 | 1300 |             closeOutput(output); | 
 | 1301 |             // If effects where present on the output, audioflinger moved them to the primary | 
 | 1302 |             // output by default: move them back to the appropriate output. | 
 | 1303 |             audio_io_handle_t dstOutput = getOutputForEffect(); | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 1304 |             if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1305 |                 mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, | 
 | 1306 |                                                mPrimaryOutput->mIoHandle, dstOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1307 |             } | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1308 |             mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1309 |         } | 
 | 1310 |     } | 
 | 1311 | } | 
 | 1312 |  | 
 | 1313 |  | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1314 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, | 
 | 1315 |                                              audio_io_handle_t *input, | 
 | 1316 |                                              audio_session_t session, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1317 |                                              uid_t uid, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1318 |                                              uint32_t samplingRate, | 
 | 1319 |                                              audio_format_t format, | 
 | 1320 |                                              audio_channel_mask_t channelMask, | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1321 |                                              audio_input_flags_t flags, | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1322 |                                              audio_port_handle_t selectedDeviceId, | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1323 |                                              input_type_t *inputType) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1324 | { | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1325 |     ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," | 
 | 1326 |             "session %d, flags %#x", | 
 | 1327 |           attr->source, samplingRate, format, channelMask, session, flags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1328 |  | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1329 |     *input = AUDIO_IO_HANDLE_NONE; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1330 |     *inputType = API_INPUT_INVALID; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1331 |     audio_devices_t device; | 
 | 1332 |     // handle legacy remote submix case where the address was not always specified | 
 | 1333 |     String8 address = String8(""); | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1334 |     audio_source_t inputSource = attr->source; | 
 | 1335 |     audio_source_t halInputSource; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1336 |     AudioMix *policyMix = NULL; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1337 |  | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1338 |     if (inputSource == AUDIO_SOURCE_DEFAULT) { | 
 | 1339 |         inputSource = AUDIO_SOURCE_MIC; | 
 | 1340 |     } | 
 | 1341 |     halInputSource = inputSource; | 
 | 1342 |  | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1343 |     // Explicit routing? | 
 | 1344 |     sp<DeviceDescriptor> deviceDesc; | 
 | 1345 |     for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { | 
 | 1346 |         if (mAvailableInputDevices[i]->getId() == selectedDeviceId) { | 
 | 1347 |             deviceDesc = mAvailableInputDevices[i]; | 
 | 1348 |             break; | 
 | 1349 |         } | 
 | 1350 |     } | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1351 |     mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1352 |  | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1353 |     if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1354 |             strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { | 
| Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 1355 |         status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1356 |         if (ret != NO_ERROR) { | 
 | 1357 |             return ret; | 
 | 1358 |         } | 
 | 1359 |         *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1360 |         device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; | 
 | 1361 |         address = String8(attr->tags + strlen("addr=")); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1362 |     } else { | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1363 |         device = getDeviceAndMixForInputSource(inputSource, &policyMix); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1364 |         if (device == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1365 |             ALOGW("getInputForAttr() could not find device for source %d", inputSource); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1366 |             return BAD_VALUE; | 
 | 1367 |         } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1368 |         if (policyMix != NULL) { | 
 | 1369 |             address = policyMix->mRegistrationId; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1370 |             if (policyMix->mMixType == MIX_TYPE_RECORDERS) { | 
 | 1371 |                 // there is an external policy, but this input is attached to a mix of recorders, | 
 | 1372 |                 // meaning it receives audio injected into the framework, so the recorder doesn't | 
 | 1373 |                 // know about it and is therefore considered "legacy" | 
 | 1374 |                 *inputType = API_INPUT_LEGACY; | 
 | 1375 |             } else { | 
 | 1376 |                 // recording a mix of players defined by an external policy, we're rerouting for | 
 | 1377 |                 // an external policy | 
 | 1378 |                 *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; | 
 | 1379 |             } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1380 |         } else if (audio_is_remote_submix_device(device)) { | 
 | 1381 |             address = String8("0"); | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1382 |             *inputType = API_INPUT_MIX_CAPTURE; | 
| Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1383 |         } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { | 
 | 1384 |             *inputType = API_INPUT_TELEPHONY_RX; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1385 |         } else { | 
 | 1386 |             *inputType = API_INPUT_LEGACY; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1387 |         } | 
| Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1388 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1389 |     } | 
 | 1390 |  | 
 | 1391 |     *input = getInputForDevice(device, address, session, uid, inputSource, | 
 | 1392 |                                samplingRate, format, channelMask, flags, | 
 | 1393 |                                policyMix); | 
 | 1394 |     if (*input == AUDIO_IO_HANDLE_NONE) { | 
 | 1395 |         mInputRoutes.removeRoute(session); | 
 | 1396 |         return INVALID_OPERATION; | 
 | 1397 |     } | 
 | 1398 |     ALOGV("getInputForAttr() returns input type = %d", *inputType); | 
 | 1399 |     return NO_ERROR; | 
 | 1400 | } | 
 | 1401 |  | 
 | 1402 |  | 
 | 1403 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, | 
 | 1404 |                                                         String8 address, | 
 | 1405 |                                                         audio_session_t session, | 
 | 1406 |                                                         uid_t uid, | 
 | 1407 |                                                         audio_source_t inputSource, | 
 | 1408 |                                                         uint32_t samplingRate, | 
 | 1409 |                                                         audio_format_t format, | 
 | 1410 |                                                         audio_channel_mask_t channelMask, | 
 | 1411 |                                                         audio_input_flags_t flags, | 
 | 1412 |                                                         AudioMix *policyMix) | 
 | 1413 | { | 
 | 1414 |     audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
 | 1415 |     audio_source_t halInputSource = inputSource; | 
 | 1416 |     bool isSoundTrigger = false; | 
 | 1417 |  | 
 | 1418 |     if (inputSource == AUDIO_SOURCE_HOTWORD) { | 
 | 1419 |         ssize_t index = mSoundTriggerSessions.indexOfKey(session); | 
 | 1420 |         if (index >= 0) { | 
 | 1421 |             input = mSoundTriggerSessions.valueFor(session); | 
 | 1422 |             isSoundTrigger = true; | 
 | 1423 |             flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); | 
 | 1424 |             ALOGV("SoundTrigger capture on session %d input %d", session, input); | 
 | 1425 |         } else { | 
 | 1426 |             halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; | 
| Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1427 |         } | 
 | 1428 |     } | 
 | 1429 |  | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1430 |     // find a compatible input profile (not necessarily identical in parameters) | 
 | 1431 |     sp<IOProfile> profile; | 
 | 1432 |     // samplingRate and flags may be updated by getInputProfile | 
 | 1433 |     uint32_t profileSamplingRate = samplingRate; | 
 | 1434 |     audio_format_t profileFormat = format; | 
 | 1435 |     audio_channel_mask_t profileChannelMask = channelMask; | 
 | 1436 |     audio_input_flags_t profileFlags = flags; | 
 | 1437 |     for (;;) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1438 |         profile = getInputProfile(device, address, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1439 |                                   profileSamplingRate, profileFormat, profileChannelMask, | 
 | 1440 |                                   profileFlags); | 
 | 1441 |         if (profile != 0) { | 
 | 1442 |             break; // success | 
 | 1443 |         } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { | 
 | 1444 |             profileFlags = AUDIO_INPUT_FLAG_NONE; // retry | 
 | 1445 |         } else { // fail | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1446 |             ALOGW("getInputForDevice() could not find profile for device 0x%X," | 
 | 1447 |                   "samplingRate %u, format %#x, channelMask 0x%X, flags %#x", | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1448 |                     device, samplingRate, format, channelMask, flags); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1449 |             return input; | 
| Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1450 |         } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1451 |     } | 
 | 1452 |  | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1453 |     if (profile->getModuleHandle() == 0) { | 
 | 1454 |         ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1455 |         return input; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1456 |     } | 
 | 1457 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1458 |     sp<AudioSession> audioSession = new AudioSession(session, | 
 | 1459 |                                                               inputSource, | 
 | 1460 |                                                               format, | 
 | 1461 |                                                               samplingRate, | 
 | 1462 |                                                               channelMask, | 
 | 1463 |                                                               flags, | 
 | 1464 |                                                               uid, | 
 | 1465 |                                                               isSoundTrigger); | 
 | 1466 |  | 
 | 1467 | // TODO enable input reuse | 
 | 1468 | #if 0 | 
 | 1469 |     // reuse an open input if possible | 
 | 1470 |     for (size_t i = 0; i < mInputs.size(); i++) { | 
 | 1471 |         sp<AudioInputDescriptor> desc = mInputs.valueAt(i); | 
 | 1472 |         // reuse input if it shares the same profile and same sound trigger attribute | 
 | 1473 |         if (profile == desc->mProfile && | 
 | 1474 |             isSoundTrigger == desc->isSoundTrigger()) { | 
 | 1475 |  | 
 | 1476 |             sp<AudioSession> as = desc->getAudioSession(session); | 
 | 1477 |             if (as != 0) { | 
 | 1478 |                 // do not allow unmatching properties on same session | 
 | 1479 |                 if (as->matches(audioSession)) { | 
 | 1480 |                     as->changeOpenCount(1); | 
 | 1481 |                 } else { | 
 | 1482 |                     ALOGW("getInputForDevice() record with different attributes" | 
 | 1483 |                           " exists for session %d", session); | 
 | 1484 |                     return input; | 
 | 1485 |                 } | 
 | 1486 |             } else { | 
 | 1487 |                 desc->addAudioSession(session, audioSession); | 
 | 1488 |             } | 
 | 1489 |             ALOGV("getInputForDevice() reusing input %d", mInputs.keyAt(i)); | 
 | 1490 |             return mInputs.keyAt(i); | 
 | 1491 |         } | 
 | 1492 |     } | 
 | 1493 | #endif | 
 | 1494 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1495 |     audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1496 |     config.sample_rate = profileSamplingRate; | 
 | 1497 |     config.channel_mask = profileChannelMask; | 
 | 1498 |     config.format = profileFormat; | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1499 |  | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1500 |     status_t status = mpClientInterface->openInput(profile->getModuleHandle(), | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1501 |                                                    &input, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1502 |                                                    &config, | 
 | 1503 |                                                    &device, | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1504 |                                                    address, | 
| Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1505 |                                                    halInputSource, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1506 |                                                    profileFlags); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1507 |  | 
 | 1508 |     // only accept input with the exact requested set of parameters | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1509 |     if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1510 |         (profileSamplingRate != config.sample_rate) || | 
 | 1511 |         (profileFormat != config.format) || | 
 | 1512 |         (profileChannelMask != config.channel_mask)) { | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1513 |         ALOGW("getInputForAttr() failed opening input: samplingRate %d" | 
 | 1514 |               ", format %d, channelMask %x", | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1515 |                 samplingRate, format, channelMask); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1516 |         if (input != AUDIO_IO_HANDLE_NONE) { | 
 | 1517 |             mpClientInterface->closeInput(input); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1518 |         } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1519 |         return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1520 |     } | 
 | 1521 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1522 |     sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1523 |     inputDesc->mSamplingRate = profileSamplingRate; | 
 | 1524 |     inputDesc->mFormat = profileFormat; | 
 | 1525 |     inputDesc->mChannelMask = profileChannelMask; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1526 |     inputDesc->mDevice = device; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1527 |     inputDesc->mPolicyMix = policyMix; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1528 |     inputDesc->addAudioSession(session, audioSession); | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1529 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1530 |     addInput(input, inputDesc); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1531 |     mpClientInterface->onAudioPortListUpdate(); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1532 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1533 |     return input; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1534 | } | 
 | 1535 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1536 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, | 
 | 1537 |                                         audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1538 | { | 
 | 1539 |     ALOGV("startInput() input %d", input); | 
 | 1540 |     ssize_t index = mInputs.indexOfKey(input); | 
 | 1541 |     if (index < 0) { | 
 | 1542 |         ALOGW("startInput() unknown input %d", input); | 
 | 1543 |         return BAD_VALUE; | 
 | 1544 |     } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1545 |     sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1546 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1547 |     sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
 | 1548 |     if (audioSession == 0) { | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1549 |         ALOGW("startInput() unknown session %d on input %d", session, input); | 
 | 1550 |         return BAD_VALUE; | 
 | 1551 |     } | 
 | 1552 |  | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1553 |     // virtual input devices are compatible with other input devices | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1554 |     if (!is_virtual_input_device(inputDesc->mDevice)) { | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1555 |  | 
 | 1556 |         // for a non-virtual input device, check if there is another (non-virtual) active input | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1557 |         audio_io_handle_t activeInput = mInputs.getActiveInput(); | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1558 |         if (activeInput != 0 && activeInput != input) { | 
 | 1559 |  | 
 | 1560 |             // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed, | 
 | 1561 |             // otherwise the active input continues and the new input cannot be started. | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1562 |             sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1563 |             if ((activeDesc->inputSource() == AUDIO_SOURCE_HOTWORD) && | 
| Eric Laurent | 64265b2 | 2015-09-18 18:32:07 -0700 | [diff] [blame] | 1564 |                     !activeDesc->hasPreemptedSession(session)) { | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1565 |                 ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1566 |                 //FIXME: consider all active sessions | 
 | 1567 |                 AudioSessionCollection activeSessions = activeDesc->getActiveAudioSessions(); | 
 | 1568 |                 audio_session_t activeSession = activeSessions.keyAt(0); | 
 | 1569 |                 SortedVector<audio_session_t> sessions = | 
 | 1570 |                                            activeDesc->getPreemptedSessions(); | 
| Eric Laurent | 64265b2 | 2015-09-18 18:32:07 -0700 | [diff] [blame] | 1571 |                 sessions.add(activeSession); | 
 | 1572 |                 inputDesc->setPreemptedSessions(sessions); | 
 | 1573 |                 stopInput(activeInput, activeSession); | 
 | 1574 |                 releaseInput(activeInput, activeSession); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1575 |             } else { | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1576 |                 ALOGE("startInput(%d) failed: other input %d already started", input, activeInput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1577 |                 return INVALID_OPERATION; | 
 | 1578 |             } | 
 | 1579 |         } | 
| Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 1580 |  | 
 | 1581 |         // Do not allow capture if an active voice call is using a software patch and | 
 | 1582 |         // the call TX source device is on the same HW module. | 
 | 1583 |         // FIXME: would be better to refine to only inputs whose profile connects to the | 
 | 1584 |         // call TX device but this information is not in the audio patch | 
 | 1585 |         if (mCallTxPatch != 0 && | 
 | 1586 |             inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { | 
 | 1587 |             return INVALID_OPERATION; | 
 | 1588 |         } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1589 |     } | 
 | 1590 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1591 |     // Routing? | 
 | 1592 |     mInputRoutes.incRouteActivity(session); | 
 | 1593 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1594 |     if (!inputDesc->isActive() || mInputRoutes.hasRouteChanged(session)) { | 
| Jean-Michel Trivi | 2b0c1fc | 2015-04-15 17:29:28 -0700 | [diff] [blame] | 1595 |         // if input maps to a dynamic policy with an activity listener, notify of state change | 
 | 1596 |         if ((inputDesc->mPolicyMix != NULL) | 
| Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 1597 |                 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { | 
| Jean-Michel Trivi | 2b0c1fc | 2015-04-15 17:29:28 -0700 | [diff] [blame] | 1598 |             mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId, | 
 | 1599 |                     MIX_STATE_MIXING); | 
 | 1600 |         } | 
 | 1601 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1602 |         if (mInputs.activeInputsCount() == 0) { | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1603 |             SoundTrigger::setCaptureState(true); | 
 | 1604 |         } | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1605 |         setInputDevice(input, getNewInputDevice(input), true /* force */); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1606 |  | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1607 |         // automatically enable the remote submix output when input is started if not | 
 | 1608 |         // used by a policy mix of type MIX_TYPE_RECORDERS | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1609 |         // For remote submix (a virtual device), we open only one input per capture request. | 
 | 1610 |         if (audio_is_remote_submix_device(inputDesc->mDevice)) { | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1611 |             String8 address = String8(""); | 
 | 1612 |             if (inputDesc->mPolicyMix == NULL) { | 
 | 1613 |                 address = String8("0"); | 
 | 1614 |             } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { | 
 | 1615 |                 address = inputDesc->mPolicyMix->mRegistrationId; | 
 | 1616 |             } | 
 | 1617 |             if (address != "") { | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1618 |                 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1619 |                         AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1620 |                         address, "remote-submix"); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1621 |             } | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1622 |         } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1623 |     } | 
 | 1624 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1625 |     ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1626 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1627 |     audioSession->changeActiveCount(1); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1628 |     return NO_ERROR; | 
 | 1629 | } | 
 | 1630 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1631 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, | 
 | 1632 |                                        audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1633 | { | 
 | 1634 |     ALOGV("stopInput() input %d", input); | 
 | 1635 |     ssize_t index = mInputs.indexOfKey(input); | 
 | 1636 |     if (index < 0) { | 
 | 1637 |         ALOGW("stopInput() unknown input %d", input); | 
 | 1638 |         return BAD_VALUE; | 
 | 1639 |     } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1640 |     sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1641 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1642 |     sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1643 |     if (index < 0) { | 
 | 1644 |         ALOGW("stopInput() unknown session %d on input %d", session, input); | 
 | 1645 |         return BAD_VALUE; | 
 | 1646 |     } | 
 | 1647 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1648 |     if (audioSession->activeCount() == 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1649 |         ALOGW("stopInput() input %d already stopped", input); | 
 | 1650 |         return INVALID_OPERATION; | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1651 |     } | 
 | 1652 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1653 |     audioSession->changeActiveCount(-1); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1654 |  | 
 | 1655 |     // Routing? | 
 | 1656 |     mInputRoutes.decRouteActivity(session); | 
 | 1657 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1658 |     if (!inputDesc->isActive()) { | 
| Jean-Michel Trivi | 2b0c1fc | 2015-04-15 17:29:28 -0700 | [diff] [blame] | 1659 |         // if input maps to a dynamic policy with an activity listener, notify of state change | 
 | 1660 |         if ((inputDesc->mPolicyMix != NULL) | 
| Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 1661 |                 && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { | 
| Jean-Michel Trivi | 2b0c1fc | 2015-04-15 17:29:28 -0700 | [diff] [blame] | 1662 |             mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId, | 
 | 1663 |                     MIX_STATE_IDLE); | 
 | 1664 |         } | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1665 |  | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1666 |         // automatically disable the remote submix output when input is stopped if not | 
 | 1667 |         // used by a policy mix of type MIX_TYPE_RECORDERS | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1668 |         if (audio_is_remote_submix_device(inputDesc->mDevice)) { | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1669 |             String8 address = String8(""); | 
 | 1670 |             if (inputDesc->mPolicyMix == NULL) { | 
 | 1671 |                 address = String8("0"); | 
 | 1672 |             } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { | 
 | 1673 |                 address = inputDesc->mPolicyMix->mRegistrationId; | 
 | 1674 |             } | 
 | 1675 |             if (address != "") { | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1676 |                 setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1677 |                                          AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1678 |                                          address, "remote-submix"); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1679 |             } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1680 |         } | 
 | 1681 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1682 |         resetInputDevice(input); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1683 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1684 |         if (mInputs.activeInputsCount() == 0) { | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1685 |             SoundTrigger::setCaptureState(false); | 
 | 1686 |         } | 
| Eric Laurent | 64265b2 | 2015-09-18 18:32:07 -0700 | [diff] [blame] | 1687 |         inputDesc->clearPreemptedSessions(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1688 |     } | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1689 |     return NO_ERROR; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1690 | } | 
 | 1691 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1692 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, | 
 | 1693 |                                       audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1694 | { | 
 | 1695 |     ALOGV("releaseInput() %d", input); | 
 | 1696 |     ssize_t index = mInputs.indexOfKey(input); | 
 | 1697 |     if (index < 0) { | 
 | 1698 |         ALOGW("releaseInput() releasing unknown input %d", input); | 
 | 1699 |         return; | 
 | 1700 |     } | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1701 |  | 
 | 1702 |     // Routing | 
 | 1703 |     mInputRoutes.removeRoute(session); | 
 | 1704 |  | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1705 |     sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
 | 1706 |     ALOG_ASSERT(inputDesc != 0); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1707 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1708 |     sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1709 |     if (index < 0) { | 
 | 1710 |         ALOGW("releaseInput() unknown session %d on input %d", session, input); | 
 | 1711 |         return; | 
 | 1712 |     } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1713 |  | 
 | 1714 |     if (audioSession->openCount() == 0) { | 
 | 1715 |         ALOGW("releaseInput() invalid open count %d on session %d", | 
 | 1716 |               audioSession->openCount(), session); | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1717 |         return; | 
 | 1718 |     } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1719 |  | 
 | 1720 |     if (audioSession->changeOpenCount(-1) == 0) { | 
 | 1721 |         inputDesc->removeAudioSession(session); | 
 | 1722 |     } | 
 | 1723 |  | 
| Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 1724 |     if (inputDesc->getOpenRefCount() > 0) { | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1725 |         ALOGV("releaseInput() exit > 0"); | 
 | 1726 |         return; | 
 | 1727 |     } | 
 | 1728 |  | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1729 |     closeInput(input); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1730 |     mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1731 |     ALOGV("releaseInput() exit"); | 
 | 1732 | } | 
 | 1733 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1734 | void AudioPolicyManager::closeAllInputs() { | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1735 |     bool patchRemoved = false; | 
 | 1736 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1737 |     for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1738 |         sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); | 
 | 1739 |         ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); | 
 | 1740 |         if (patch_index >= 0) { | 
 | 1741 |             sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); | 
 | 1742 |             status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
 | 1743 |             mAudioPatches.removeItemsAt(patch_index); | 
 | 1744 |             patchRemoved = true; | 
 | 1745 |         } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1746 |         mpClientInterface->closeInput(mInputs.keyAt(input_index)); | 
 | 1747 |     } | 
 | 1748 |     mInputs.clear(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1749 |     nextAudioPortGeneration(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1750 |  | 
 | 1751 |     if (patchRemoved) { | 
 | 1752 |         mpClientInterface->onAudioPatchListUpdate(); | 
 | 1753 |     } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1754 | } | 
 | 1755 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1756 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1757 |                                             int indexMin, | 
 | 1758 |                                             int indexMax) | 
 | 1759 | { | 
 | 1760 |     ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1761 |     mEngine->initStreamVolume(stream, indexMin, indexMax); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1762 |     //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now | 
 | 1763 |     if (stream == AUDIO_STREAM_MUSIC) { | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1764 |         mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1765 |     } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1766 | } | 
 | 1767 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1768 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1769 |                                                   int index, | 
 | 1770 |                                                   audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1771 | { | 
 | 1772 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1773 |     if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) || | 
 | 1774 |             (index > mStreams.valueFor(stream).getVolumeIndexMax())) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1775 |         return BAD_VALUE; | 
 | 1776 |     } | 
 | 1777 |     if (!audio_is_output_device(device)) { | 
 | 1778 |         return BAD_VALUE; | 
 | 1779 |     } | 
 | 1780 |  | 
 | 1781 |     // Force max volume if stream cannot be muted | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1782 |     if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1783 |  | 
 | 1784 |     ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d", | 
 | 1785 |           stream, device, index); | 
 | 1786 |  | 
 | 1787 |     // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and | 
 | 1788 |     // clear all device specific values | 
 | 1789 |     if (device == AUDIO_DEVICE_OUT_DEFAULT) { | 
| François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1790 |         mStreams.clearCurrentVolumeIndex(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1791 |     } | 
| François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1792 |     mStreams.addCurrentVolumeIndex(stream, device, index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1793 |  | 
| Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1794 |     // update volume on all outputs whose current device is also selected by the same | 
 | 1795 |     // strategy as the device specified by the caller | 
 | 1796 |     audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1797 |  | 
 | 1798 |  | 
 | 1799 |     //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now | 
 | 1800 |     audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE; | 
 | 1801 |     if (stream == AUDIO_STREAM_MUSIC) { | 
| François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1802 |         mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1803 |         accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/); | 
 | 1804 |     } | 
 | 1805 |     if ((device != AUDIO_DEVICE_OUT_DEFAULT) && | 
 | 1806 |             (device & (strategyDevice | accessibilityDevice)) == 0) { | 
| Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1807 |         return NO_ERROR; | 
 | 1808 |     } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1809 |     status_t status = NO_ERROR; | 
 | 1810 |     for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1811 |         sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
 | 1812 |         audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); | 
| Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1813 |         if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1814 |             status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1815 |             if (volStatus != NO_ERROR) { | 
 | 1816 |                 status = volStatus; | 
 | 1817 |             } | 
 | 1818 |         } | 
| Jean-Michel Trivi | af20bc2 | 2015-09-14 16:37:07 -0700 | [diff] [blame] | 1819 |         if ((accessibilityDevice != AUDIO_DEVICE_NONE) && | 
 | 1820 |                 ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0))) | 
 | 1821 |         { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1822 |             status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1823 |                                                    index, desc, curDevice); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1824 |         } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1825 |     } | 
 | 1826 |     return status; | 
 | 1827 | } | 
 | 1828 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1829 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1830 |                                                       int *index, | 
 | 1831 |                                                       audio_devices_t device) | 
 | 1832 | { | 
 | 1833 |     if (index == NULL) { | 
 | 1834 |         return BAD_VALUE; | 
 | 1835 |     } | 
 | 1836 |     if (!audio_is_output_device(device)) { | 
 | 1837 |         return BAD_VALUE; | 
 | 1838 |     } | 
 | 1839 |     // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to | 
 | 1840 |     // the strategy the stream belongs to. | 
 | 1841 |     if (device == AUDIO_DEVICE_OUT_DEFAULT) { | 
 | 1842 |         device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); | 
 | 1843 |     } | 
| François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1844 |     device = Volume::getDeviceForVolume(device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1845 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1846 |     *index =  mStreams.valueFor(stream).getVolumeIndex(device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1847 |     ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); | 
 | 1848 |     return NO_ERROR; | 
 | 1849 | } | 
 | 1850 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1851 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1852 |                                             const SortedVector<audio_io_handle_t>& outputs) | 
 | 1853 | { | 
 | 1854 |     // select one output among several suitable for global effects. | 
 | 1855 |     // The priority is as follows: | 
 | 1856 |     // 1: An offloaded output. If the effect ends up not being offloadable, | 
 | 1857 |     //    AudioFlinger will invalidate the track and the offloaded output | 
 | 1858 |     //    will be closed causing the effect to be moved to a PCM output. | 
 | 1859 |     // 2: A deep buffer output | 
 | 1860 |     // 3: the first output in the list | 
 | 1861 |  | 
 | 1862 |     if (outputs.size() == 0) { | 
 | 1863 |         return 0; | 
 | 1864 |     } | 
 | 1865 |  | 
 | 1866 |     audio_io_handle_t outputOffloaded = 0; | 
 | 1867 |     audio_io_handle_t outputDeepBuffer = 0; | 
 | 1868 |  | 
 | 1869 |     for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1870 |         sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1871 |         ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1872 |         if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
 | 1873 |             outputOffloaded = outputs[i]; | 
 | 1874 |         } | 
 | 1875 |         if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { | 
 | 1876 |             outputDeepBuffer = outputs[i]; | 
 | 1877 |         } | 
 | 1878 |     } | 
 | 1879 |  | 
 | 1880 |     ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", | 
 | 1881 |           outputOffloaded, outputDeepBuffer); | 
 | 1882 |     if (outputOffloaded != 0) { | 
 | 1883 |         return outputOffloaded; | 
 | 1884 |     } | 
 | 1885 |     if (outputDeepBuffer != 0) { | 
 | 1886 |         return outputDeepBuffer; | 
 | 1887 |     } | 
 | 1888 |  | 
 | 1889 |     return outputs[0]; | 
 | 1890 | } | 
 | 1891 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1892 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1893 | { | 
 | 1894 |     // apply simple rule where global effects are attached to the same output as MUSIC streams | 
 | 1895 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1896 |     routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1897 |     audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
 | 1898 |     SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); | 
 | 1899 |  | 
 | 1900 |     audio_io_handle_t output = selectOutputForEffects(dstOutputs); | 
 | 1901 |     ALOGV("getOutputForEffect() got output %d for fx %s flags %x", | 
 | 1902 |           output, (desc == NULL) ? "unspecified" : desc->name,  (desc == NULL) ? 0 : desc->flags); | 
 | 1903 |  | 
 | 1904 |     return output; | 
 | 1905 | } | 
 | 1906 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1907 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1908 |                                 audio_io_handle_t io, | 
 | 1909 |                                 uint32_t strategy, | 
 | 1910 |                                 int session, | 
 | 1911 |                                 int id) | 
 | 1912 | { | 
 | 1913 |     ssize_t index = mOutputs.indexOfKey(io); | 
 | 1914 |     if (index < 0) { | 
 | 1915 |         index = mInputs.indexOfKey(io); | 
 | 1916 |         if (index < 0) { | 
 | 1917 |             ALOGW("registerEffect() unknown io %d", io); | 
 | 1918 |             return INVALID_OPERATION; | 
 | 1919 |         } | 
 | 1920 |     } | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 1921 |     return mEffects.registerEffect(desc, io, strategy, session, id); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1922 | } | 
 | 1923 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1924 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const | 
 | 1925 | { | 
 | 1926 |     return mOutputs.isStreamActive(stream, inPastMs); | 
 | 1927 | } | 
 | 1928 |  | 
 | 1929 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const | 
 | 1930 | { | 
 | 1931 |     return mOutputs.isStreamActiveRemotely(stream, inPastMs); | 
 | 1932 | } | 
 | 1933 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1934 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1935 | { | 
 | 1936 |     for (size_t i = 0; i < mInputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1937 |         const sp<AudioInputDescriptor>  inputDescriptor = mInputs.valueAt(i); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1938 |         if (inputDescriptor->isSourceActive(source)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1939 |             return true; | 
 | 1940 |         } | 
 | 1941 |     } | 
 | 1942 |     return false; | 
 | 1943 | } | 
 | 1944 |  | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1945 | // Register a list of custom mixes with their attributes and format. | 
 | 1946 | // When a mix is registered, corresponding input and output profiles are | 
 | 1947 | // added to the remote submix hw module. The profile contains only the | 
 | 1948 | // parameters (sampling rate, format...) specified by the mix. | 
 | 1949 | // The corresponding input remote submix device is also connected. | 
 | 1950 | // | 
 | 1951 | // When a remote submix device is connected, the address is checked to select the | 
 | 1952 | // appropriate profile and the corresponding input or output stream is opened. | 
 | 1953 | // | 
 | 1954 | // When capture starts, getInputForAttr() will: | 
 | 1955 | //  - 1 look for a mix matching the address passed in attribtutes tags if any | 
 | 1956 | //  - 2 if none found, getDeviceForInputSource() will: | 
 | 1957 | //     - 2.1 look for a mix matching the attributes source | 
 | 1958 | //     - 2.2 if none found, default to device selection by policy rules | 
 | 1959 | // At this time, the corresponding output remote submix device is also connected | 
 | 1960 | // and active playback use cases can be transferred to this mix if needed when reconnecting | 
 | 1961 | // after AudioTracks are invalidated | 
 | 1962 | // | 
 | 1963 | // When playback starts, getOutputForAttr() will: | 
 | 1964 | //  - 1 look for a mix matching the address passed in attribtutes tags if any | 
 | 1965 | //  - 2 if none found, look for a mix matching the attributes usage | 
 | 1966 | //  - 3 if none found, default to device and output selection by policy rules. | 
 | 1967 |  | 
 | 1968 | status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes) | 
 | 1969 | { | 
 | 1970 |     sp<HwModule> module; | 
 | 1971 |     for (size_t i = 0; i < mHwModules.size(); i++) { | 
 | 1972 |         if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 && | 
 | 1973 |                 mHwModules[i]->mHandle != 0) { | 
 | 1974 |             module = mHwModules[i]; | 
 | 1975 |             break; | 
 | 1976 |         } | 
 | 1977 |     } | 
 | 1978 |  | 
 | 1979 |     if (module == 0) { | 
 | 1980 |         return INVALID_OPERATION; | 
 | 1981 |     } | 
 | 1982 |  | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 1983 |     ALOGV("registerPolicyMixes() num mixes %zu", mixes.size()); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1984 |  | 
 | 1985 |     for (size_t i = 0; i < mixes.size(); i++) { | 
 | 1986 |         String8 address = mixes[i].mRegistrationId; | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1987 |  | 
 | 1988 |         if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1989 |             continue; | 
 | 1990 |         } | 
 | 1991 |         audio_config_t outputConfig = mixes[i].mFormat; | 
 | 1992 |         audio_config_t inputConfig = mixes[i].mFormat; | 
 | 1993 |         // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in | 
 | 1994 |         // stereo and let audio flinger do the channel conversion if needed. | 
 | 1995 |         outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; | 
 | 1996 |         inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; | 
 | 1997 |         module->addOutputProfile(address, &outputConfig, | 
 | 1998 |                                  AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); | 
 | 1999 |         module->addInputProfile(address, &inputConfig, | 
 | 2000 |                                  AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2001 |  | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2002 |         if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2003 |             setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2004 |                                      AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2005 |                                      address.string(), "remote-submix"); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2006 |         } else { | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2007 |             setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2008 |                                      AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2009 |                                      address.string(), "remote-submix"); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2010 |         } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2011 |     } | 
 | 2012 |     return NO_ERROR; | 
 | 2013 | } | 
 | 2014 |  | 
 | 2015 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) | 
 | 2016 | { | 
 | 2017 |     sp<HwModule> module; | 
 | 2018 |     for (size_t i = 0; i < mHwModules.size(); i++) { | 
 | 2019 |         if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 && | 
 | 2020 |                 mHwModules[i]->mHandle != 0) { | 
 | 2021 |             module = mHwModules[i]; | 
 | 2022 |             break; | 
 | 2023 |         } | 
 | 2024 |     } | 
 | 2025 |  | 
 | 2026 |     if (module == 0) { | 
 | 2027 |         return INVALID_OPERATION; | 
 | 2028 |     } | 
 | 2029 |  | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2030 |     ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2031 |  | 
 | 2032 |     for (size_t i = 0; i < mixes.size(); i++) { | 
 | 2033 |         String8 address = mixes[i].mRegistrationId; | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2034 |  | 
 | 2035 |         if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2036 |             continue; | 
 | 2037 |         } | 
 | 2038 |  | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2039 |         if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == | 
 | 2040 |                                              AUDIO_POLICY_DEVICE_STATE_AVAILABLE) | 
 | 2041 |         { | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2042 |             setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2043 |                                      AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2044 |                                      address.string(), "remote-submix"); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2045 |         } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2046 |  | 
 | 2047 |         if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == | 
 | 2048 |                                              AUDIO_POLICY_DEVICE_STATE_AVAILABLE) | 
 | 2049 |         { | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2050 |             setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2051 |                                      AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2052 |                                      address.string(), "remote-submix"); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2053 |         } | 
 | 2054 |         module->removeOutputProfile(address); | 
 | 2055 |         module->removeInputProfile(address); | 
 | 2056 |     } | 
 | 2057 |     return NO_ERROR; | 
 | 2058 | } | 
 | 2059 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2060 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2061 | status_t AudioPolicyManager::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2062 | { | 
 | 2063 |     const size_t SIZE = 256; | 
 | 2064 |     char buffer[SIZE]; | 
 | 2065 |     String8 result; | 
 | 2066 |  | 
 | 2067 |     snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); | 
 | 2068 |     result.append(buffer); | 
 | 2069 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2070 |     snprintf(buffer, SIZE, " Primary Output: %d\n", | 
 | 2071 |              hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2072 |     result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2073 |     snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2074 |     result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2075 |     snprintf(buffer, SIZE, " Force use for communications %d\n", | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2076 |              mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2077 |     result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2078 |     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] | 2079 |     result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2080 |     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] | 2081 |     result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2082 |     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] | 2083 |     result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2084 |     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] | 2085 |     result.append(buffer); | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2086 |     snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2087 |             mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2088 |     result.append(buffer); | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2089 |     snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available"); | 
 | 2090 |     result.append(buffer); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame^] | 2091 |     snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off"); | 
 | 2092 |     result.append(buffer); | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2093 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2094 |     write(fd, result.string(), result.size()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2095 |  | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2096 |     mAvailableOutputDevices.dump(fd, String8("Available output")); | 
 | 2097 |     mAvailableInputDevices.dump(fd, String8("Available input")); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2098 |     mHwModules.dump(fd); | 
 | 2099 |     mOutputs.dump(fd); | 
 | 2100 |     mInputs.dump(fd); | 
| François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2101 |     mStreams.dump(fd); | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2102 |     mEffects.dump(fd); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2103 |     mAudioPatches.dump(fd); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2104 |  | 
 | 2105 |     return NO_ERROR; | 
 | 2106 | } | 
 | 2107 |  | 
 | 2108 | // This function checks for the parameters which can be offloaded. | 
 | 2109 | // This can be enhanced depending on the capability of the DSP and policy | 
 | 2110 | // of the system. | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2111 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2112 | { | 
 | 2113 |     ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2114 |      " BitRate=%u, duration=%" PRId64 " us, has_video=%d", | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2115 |      offloadInfo.sample_rate, offloadInfo.channel_mask, | 
 | 2116 |      offloadInfo.format, | 
 | 2117 |      offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, | 
 | 2118 |      offloadInfo.has_video); | 
 | 2119 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame^] | 2120 |     if (mMasterMono) { | 
 | 2121 |         return false; // no offloading if mono is set. | 
 | 2122 |     } | 
 | 2123 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2124 |     // Check if offload has been disabled | 
 | 2125 |     char propValue[PROPERTY_VALUE_MAX]; | 
 | 2126 |     if (property_get("audio.offload.disable", propValue, "0")) { | 
 | 2127 |         if (atoi(propValue) != 0) { | 
 | 2128 |             ALOGV("offload disabled by audio.offload.disable=%s", propValue ); | 
 | 2129 |             return false; | 
 | 2130 |         } | 
 | 2131 |     } | 
 | 2132 |  | 
 | 2133 |     // Check if stream type is music, then only allow offload as of now. | 
 | 2134 |     if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) | 
 | 2135 |     { | 
 | 2136 |         ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); | 
 | 2137 |         return false; | 
 | 2138 |     } | 
 | 2139 |  | 
 | 2140 |     //TODO: enable audio offloading with video when ready | 
| Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2141 |     const bool allowOffloadWithVideo = | 
 | 2142 |             property_get_bool("audio.offload.video", false /* default_value */); | 
 | 2143 |     if (offloadInfo.has_video && !allowOffloadWithVideo) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2144 |         ALOGV("isOffloadSupported: has_video == true, returning false"); | 
 | 2145 |         return false; | 
 | 2146 |     } | 
 | 2147 |  | 
 | 2148 |     //If duration is less than minimum value defined in property, return false | 
 | 2149 |     if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { | 
 | 2150 |         if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { | 
 | 2151 |             ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); | 
 | 2152 |             return false; | 
 | 2153 |         } | 
 | 2154 |     } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { | 
 | 2155 |         ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); | 
 | 2156 |         return false; | 
 | 2157 |     } | 
 | 2158 |  | 
 | 2159 |     // Do not allow offloading if one non offloadable effect is enabled. This prevents from | 
 | 2160 |     // creating an offloaded track and tearing it down immediately after start when audioflinger | 
 | 2161 |     // detects there is an active non offloadable effect. | 
 | 2162 |     // FIXME: We should check the audio session here but we do not have it in this context. | 
 | 2163 |     // This may prevent offloading in rare situations where effects are left active by apps | 
 | 2164 |     // in the background. | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2165 |     if (mEffects.isNonOffloadableEffectEnabled()) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2166 |         return false; | 
 | 2167 |     } | 
 | 2168 |  | 
 | 2169 |     // See if there is a profile to support this. | 
 | 2170 |     // AUDIO_DEVICE_NONE | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2171 |     sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2172 |                                             offloadInfo.sample_rate, | 
 | 2173 |                                             offloadInfo.format, | 
 | 2174 |                                             offloadInfo.channel_mask, | 
 | 2175 |                                             AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2176 |     ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); | 
 | 2177 |     return (profile != 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2178 | } | 
 | 2179 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2180 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, | 
 | 2181 |                                             audio_port_type_t type, | 
 | 2182 |                                             unsigned int *num_ports, | 
 | 2183 |                                             struct audio_port *ports, | 
 | 2184 |                                             unsigned int *generation) | 
 | 2185 | { | 
 | 2186 |     if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || | 
 | 2187 |             generation == NULL) { | 
 | 2188 |         return BAD_VALUE; | 
 | 2189 |     } | 
 | 2190 |     ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); | 
 | 2191 |     if (ports == NULL) { | 
 | 2192 |         *num_ports = 0; | 
 | 2193 |     } | 
 | 2194 |  | 
 | 2195 |     size_t portsWritten = 0; | 
 | 2196 |     size_t portsMax = *num_ports; | 
 | 2197 |     *num_ports = 0; | 
 | 2198 |     if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { | 
 | 2199 |         if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
 | 2200 |             for (size_t i = 0; | 
 | 2201 |                     i  < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) { | 
 | 2202 |                 mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); | 
 | 2203 |             } | 
 | 2204 |             *num_ports += mAvailableOutputDevices.size(); | 
 | 2205 |         } | 
 | 2206 |         if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
 | 2207 |             for (size_t i = 0; | 
 | 2208 |                     i  < mAvailableInputDevices.size() && portsWritten < portsMax; i++) { | 
 | 2209 |                 mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); | 
 | 2210 |             } | 
 | 2211 |             *num_ports += mAvailableInputDevices.size(); | 
 | 2212 |         } | 
 | 2213 |     } | 
 | 2214 |     if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { | 
 | 2215 |         if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
 | 2216 |             for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { | 
 | 2217 |                 mInputs[i]->toAudioPort(&ports[portsWritten++]); | 
 | 2218 |             } | 
 | 2219 |             *num_ports += mInputs.size(); | 
 | 2220 |         } | 
 | 2221 |         if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2222 |             size_t numOutputs = 0; | 
 | 2223 |             for (size_t i = 0; i < mOutputs.size(); i++) { | 
 | 2224 |                 if (!mOutputs[i]->isDuplicated()) { | 
 | 2225 |                     numOutputs++; | 
 | 2226 |                     if (portsWritten < portsMax) { | 
 | 2227 |                         mOutputs[i]->toAudioPort(&ports[portsWritten++]); | 
 | 2228 |                     } | 
 | 2229 |                 } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2230 |             } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2231 |             *num_ports += numOutputs; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2232 |         } | 
 | 2233 |     } | 
 | 2234 |     *generation = curAudioPortGeneration(); | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2235 |     ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2236 |     return NO_ERROR; | 
 | 2237 | } | 
 | 2238 |  | 
 | 2239 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) | 
 | 2240 | { | 
 | 2241 |     return NO_ERROR; | 
 | 2242 | } | 
 | 2243 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2244 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, | 
 | 2245 |                                                audio_patch_handle_t *handle, | 
 | 2246 |                                                uid_t uid) | 
 | 2247 | { | 
 | 2248 |     ALOGV("createAudioPatch()"); | 
 | 2249 |  | 
 | 2250 |     if (handle == NULL || patch == NULL) { | 
 | 2251 |         return BAD_VALUE; | 
 | 2252 |     } | 
 | 2253 |     ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); | 
 | 2254 |  | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2255 |     if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || | 
 | 2256 |             patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { | 
 | 2257 |         return BAD_VALUE; | 
 | 2258 |     } | 
 | 2259 |     // only one source per audio patch supported for now | 
 | 2260 |     if (patch->num_sources > 1) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2261 |         return INVALID_OPERATION; | 
 | 2262 |     } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2263 |  | 
 | 2264 |     if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2265 |         return INVALID_OPERATION; | 
 | 2266 |     } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2267 |     for (size_t i = 0; i < patch->num_sinks; i++) { | 
 | 2268 |         if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { | 
 | 2269 |             return INVALID_OPERATION; | 
 | 2270 |         } | 
 | 2271 |     } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2272 |  | 
 | 2273 |     sp<AudioPatch> patchDesc; | 
 | 2274 |     ssize_t index = mAudioPatches.indexOfKey(*handle); | 
 | 2275 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2276 |     ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, | 
 | 2277 |                                                            patch->sources[0].role, | 
 | 2278 |                                                            patch->sources[0].type); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2279 | #if LOG_NDEBUG == 0 | 
 | 2280 |     for (size_t i = 0; i < patch->num_sinks; i++) { | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2281 |         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] | 2282 |                                                              patch->sinks[i].role, | 
 | 2283 |                                                              patch->sinks[i].type); | 
 | 2284 |     } | 
 | 2285 | #endif | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2286 |  | 
 | 2287 |     if (index >= 0) { | 
 | 2288 |         patchDesc = mAudioPatches.valueAt(index); | 
 | 2289 |         ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", | 
 | 2290 |                                                                   mUidCached, patchDesc->mUid, uid); | 
 | 2291 |         if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { | 
 | 2292 |             return INVALID_OPERATION; | 
 | 2293 |         } | 
 | 2294 |     } else { | 
 | 2295 |         *handle = 0; | 
 | 2296 |     } | 
 | 2297 |  | 
 | 2298 |     if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2299 |         sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2300 |         if (outputDesc == NULL) { | 
 | 2301 |             ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); | 
 | 2302 |             return BAD_VALUE; | 
 | 2303 |         } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2304 |         ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", | 
 | 2305 |                                                 outputDesc->mIoHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2306 |         if (patchDesc != 0) { | 
 | 2307 |             if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
 | 2308 |                 ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", | 
 | 2309 |                                           patchDesc->mPatch.sources[0].id, patch->sources[0].id); | 
 | 2310 |                 return BAD_VALUE; | 
 | 2311 |             } | 
 | 2312 |         } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2313 |         DeviceVector devices; | 
 | 2314 |         for (size_t i = 0; i < patch->num_sinks; i++) { | 
 | 2315 |             // Only support mix to devices connection | 
 | 2316 |             // TODO add support for mix to mix connection | 
 | 2317 |             if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { | 
 | 2318 |                 ALOGV("createAudioPatch() source mix but sink is not a device"); | 
 | 2319 |                 return INVALID_OPERATION; | 
 | 2320 |             } | 
 | 2321 |             sp<DeviceDescriptor> devDesc = | 
 | 2322 |                     mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); | 
 | 2323 |             if (devDesc == 0) { | 
 | 2324 |                 ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); | 
 | 2325 |                 return BAD_VALUE; | 
 | 2326 |             } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2327 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2328 |             if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2329 |                                                            devDesc->mAddress, | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2330 |                                                            patch->sources[0].sample_rate, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2331 |                                                            NULL,  // updatedSamplingRate | 
 | 2332 |                                                            patch->sources[0].format, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2333 |                                                            NULL,  // updatedFormat | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2334 |                                                            patch->sources[0].channel_mask, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2335 |                                                            NULL,  // updatedChannelMask | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2336 |                                                            AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2337 |                 ALOGV("createAudioPatch() profile not supported for device %08x", | 
 | 2338 |                         devDesc->type()); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2339 |                 return INVALID_OPERATION; | 
 | 2340 |             } | 
 | 2341 |             devices.add(devDesc); | 
 | 2342 |         } | 
 | 2343 |         if (devices.size() == 0) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2344 |             return INVALID_OPERATION; | 
 | 2345 |         } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2346 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2347 |         // TODO: reconfigure output format and channels here | 
 | 2348 |         ALOGV("createAudioPatch() setting device %08x on output %d", | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2349 |               devices.types(), outputDesc->mIoHandle); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2350 |         setOutputDevice(outputDesc, devices.types(), true, 0, handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2351 |         index = mAudioPatches.indexOfKey(*handle); | 
 | 2352 |         if (index >= 0) { | 
 | 2353 |             if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
 | 2354 |                 ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); | 
 | 2355 |             } | 
 | 2356 |             patchDesc = mAudioPatches.valueAt(index); | 
 | 2357 |             patchDesc->mUid = uid; | 
 | 2358 |             ALOGV("createAudioPatch() success"); | 
 | 2359 |         } else { | 
 | 2360 |             ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); | 
 | 2361 |             return INVALID_OPERATION; | 
 | 2362 |         } | 
 | 2363 |     } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
 | 2364 |         if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
 | 2365 |             // input device to input mix connection | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2366 |             // only one sink supported when connecting an input device to a mix | 
 | 2367 |             if (patch->num_sinks > 1) { | 
 | 2368 |                 return INVALID_OPERATION; | 
 | 2369 |             } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2370 |             sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2371 |             if (inputDesc == NULL) { | 
 | 2372 |                 return BAD_VALUE; | 
 | 2373 |             } | 
 | 2374 |             if (patchDesc != 0) { | 
 | 2375 |                 if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { | 
 | 2376 |                     return BAD_VALUE; | 
 | 2377 |                 } | 
 | 2378 |             } | 
 | 2379 |             sp<DeviceDescriptor> devDesc = | 
 | 2380 |                     mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
 | 2381 |             if (devDesc == 0) { | 
 | 2382 |                 return BAD_VALUE; | 
 | 2383 |             } | 
 | 2384 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2385 |             if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2386 |                                                           devDesc->mAddress, | 
 | 2387 |                                                           patch->sinks[0].sample_rate, | 
 | 2388 |                                                           NULL, /*updatedSampleRate*/ | 
 | 2389 |                                                           patch->sinks[0].format, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2390 |                                                           NULL, /*updatedFormat*/ | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2391 |                                                           patch->sinks[0].channel_mask, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2392 |                                                           NULL, /*updatedChannelMask*/ | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2393 |                                                           // FIXME for the parameter type, | 
 | 2394 |                                                           // and the NONE | 
 | 2395 |                                                           (audio_output_flags_t) | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2396 |                                                             AUDIO_INPUT_FLAG_NONE)) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2397 |                 return INVALID_OPERATION; | 
 | 2398 |             } | 
 | 2399 |             // TODO: reconfigure output format and channels here | 
 | 2400 |             ALOGV("createAudioPatch() setting device %08x on output %d", | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2401 |                                                   devDesc->type(), inputDesc->mIoHandle); | 
 | 2402 |             setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2403 |             index = mAudioPatches.indexOfKey(*handle); | 
 | 2404 |             if (index >= 0) { | 
 | 2405 |                 if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
 | 2406 |                     ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); | 
 | 2407 |                 } | 
 | 2408 |                 patchDesc = mAudioPatches.valueAt(index); | 
 | 2409 |                 patchDesc->mUid = uid; | 
 | 2410 |                 ALOGV("createAudioPatch() success"); | 
 | 2411 |             } else { | 
 | 2412 |                 ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); | 
 | 2413 |                 return INVALID_OPERATION; | 
 | 2414 |             } | 
 | 2415 |         } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
 | 2416 |             // device to device connection | 
 | 2417 |             if (patchDesc != 0) { | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2418 |                 if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2419 |                     return BAD_VALUE; | 
 | 2420 |                 } | 
 | 2421 |             } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2422 |             sp<DeviceDescriptor> srcDeviceDesc = | 
 | 2423 |                     mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
| Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2424 |             if (srcDeviceDesc == 0) { | 
 | 2425 |                 return BAD_VALUE; | 
 | 2426 |             } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2427 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2428 |             //update source and sink with our own data as the data passed in the patch may | 
 | 2429 |             // be incomplete. | 
 | 2430 |             struct audio_patch newPatch = *patch; | 
 | 2431 |             srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2432 |  | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2433 |             for (size_t i = 0; i < patch->num_sinks; i++) { | 
 | 2434 |                 if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { | 
 | 2435 |                     ALOGV("createAudioPatch() source device but one sink is not a device"); | 
 | 2436 |                     return INVALID_OPERATION; | 
 | 2437 |                 } | 
 | 2438 |  | 
 | 2439 |                 sp<DeviceDescriptor> sinkDeviceDesc = | 
 | 2440 |                         mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); | 
 | 2441 |                 if (sinkDeviceDesc == 0) { | 
 | 2442 |                     return BAD_VALUE; | 
 | 2443 |                 } | 
 | 2444 |                 sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); | 
 | 2445 |  | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2446 |                 // create a software bridge in PatchPanel if: | 
 | 2447 |                 // - source and sink devices are on differnt HW modules OR | 
 | 2448 |                 // - audio HAL version is < 3.0 | 
 | 2449 |                 if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) || | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 2450 |                         (srcDeviceDesc->mModule->getHalVersion() < AUDIO_DEVICE_API_VERSION_3_0)) { | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2451 |                     // support only one sink device for now to simplify output selection logic | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2452 |                     if (patch->num_sinks > 1) { | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2453 |                         return INVALID_OPERATION; | 
 | 2454 |                     } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2455 |                     SortedVector<audio_io_handle_t> outputs = | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2456 |                                             getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2457 |                     // if the sink device is reachable via an opened output stream, request to go via | 
 | 2458 |                     // this output stream by adding a second source to the patch description | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2459 |                     audio_io_handle_t output = selectOutput(outputs, | 
 | 2460 |                                                             AUDIO_OUTPUT_FLAG_NONE, | 
 | 2461 |                                                             AUDIO_FORMAT_INVALID); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2462 |                     if (output != AUDIO_IO_HANDLE_NONE) { | 
 | 2463 |                         sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
 | 2464 |                         if (outputDesc->isDuplicated()) { | 
 | 2465 |                             return INVALID_OPERATION; | 
 | 2466 |                         } | 
 | 2467 |                         outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2468 |                         newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2469 |                         newPatch.num_sources = 2; | 
 | 2470 |                     } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2471 |                 } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2472 |             } | 
 | 2473 |             // TODO: check from routing capabilities in config file and other conflicting patches | 
 | 2474 |  | 
 | 2475 |             audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
 | 2476 |             if (index >= 0) { | 
 | 2477 |                 afPatchHandle = patchDesc->mAfPatchHandle; | 
 | 2478 |             } | 
 | 2479 |  | 
 | 2480 |             status_t status = mpClientInterface->createAudioPatch(&newPatch, | 
 | 2481 |                                                                   &afPatchHandle, | 
 | 2482 |                                                                   0); | 
 | 2483 |             ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", | 
 | 2484 |                                                                   status, afPatchHandle); | 
 | 2485 |             if (status == NO_ERROR) { | 
 | 2486 |                 if (index < 0) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 2487 |                     patchDesc = new AudioPatch(&newPatch, uid); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2488 |                     addAudioPatch(patchDesc->mHandle, patchDesc); | 
 | 2489 |                 } else { | 
 | 2490 |                     patchDesc->mPatch = newPatch; | 
 | 2491 |                 } | 
 | 2492 |                 patchDesc->mAfPatchHandle = afPatchHandle; | 
 | 2493 |                 *handle = patchDesc->mHandle; | 
 | 2494 |                 nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2495 |                 mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2496 |             } else { | 
 | 2497 |                 ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", | 
 | 2498 |                 status); | 
 | 2499 |                 return INVALID_OPERATION; | 
 | 2500 |             } | 
 | 2501 |         } else { | 
 | 2502 |             return BAD_VALUE; | 
 | 2503 |         } | 
 | 2504 |     } else { | 
 | 2505 |         return BAD_VALUE; | 
 | 2506 |     } | 
 | 2507 |     return NO_ERROR; | 
 | 2508 | } | 
 | 2509 |  | 
 | 2510 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, | 
 | 2511 |                                                   uid_t uid) | 
 | 2512 | { | 
 | 2513 |     ALOGV("releaseAudioPatch() patch %d", handle); | 
 | 2514 |  | 
 | 2515 |     ssize_t index = mAudioPatches.indexOfKey(handle); | 
 | 2516 |  | 
 | 2517 |     if (index < 0) { | 
 | 2518 |         return BAD_VALUE; | 
 | 2519 |     } | 
 | 2520 |     sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
 | 2521 |     ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", | 
 | 2522 |           mUidCached, patchDesc->mUid, uid); | 
 | 2523 |     if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { | 
 | 2524 |         return INVALID_OPERATION; | 
 | 2525 |     } | 
 | 2526 |  | 
 | 2527 |     struct audio_patch *patch = &patchDesc->mPatch; | 
 | 2528 |     patchDesc->mUid = mUidCached; | 
 | 2529 |     if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2530 |         sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2531 |         if (outputDesc == NULL) { | 
 | 2532 |             ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); | 
 | 2533 |             return BAD_VALUE; | 
 | 2534 |         } | 
 | 2535 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2536 |         setOutputDevice(outputDesc, | 
 | 2537 |                         getNewOutputDevice(outputDesc, true /*fromCache*/), | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2538 |                        true, | 
 | 2539 |                        0, | 
 | 2540 |                        NULL); | 
 | 2541 |     } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
 | 2542 |         if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2543 |             sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2544 |             if (inputDesc == NULL) { | 
 | 2545 |                 ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); | 
 | 2546 |                 return BAD_VALUE; | 
 | 2547 |             } | 
 | 2548 |             setInputDevice(inputDesc->mIoHandle, | 
 | 2549 |                            getNewInputDevice(inputDesc->mIoHandle), | 
 | 2550 |                            true, | 
 | 2551 |                            NULL); | 
 | 2552 |         } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
 | 2553 |             audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle; | 
 | 2554 |             status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
 | 2555 |             ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", | 
 | 2556 |                                                               status, patchDesc->mAfPatchHandle); | 
 | 2557 |             removeAudioPatch(patchDesc->mHandle); | 
 | 2558 |             nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2559 |             mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2560 |         } else { | 
 | 2561 |             return BAD_VALUE; | 
 | 2562 |         } | 
 | 2563 |     } else { | 
 | 2564 |         return BAD_VALUE; | 
 | 2565 |     } | 
 | 2566 |     return NO_ERROR; | 
 | 2567 | } | 
 | 2568 |  | 
 | 2569 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, | 
 | 2570 |                                               struct audio_patch *patches, | 
 | 2571 |                                               unsigned int *generation) | 
 | 2572 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2573 |     if (generation == NULL) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2574 |         return BAD_VALUE; | 
 | 2575 |     } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2576 |     *generation = curAudioPortGeneration(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2577 |     return mAudioPatches.listAudioPatches(num_patches, patches); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2578 | } | 
 | 2579 |  | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2580 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2581 | { | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2582 |     ALOGV("setAudioPortConfig()"); | 
 | 2583 |  | 
 | 2584 |     if (config == NULL) { | 
 | 2585 |         return BAD_VALUE; | 
 | 2586 |     } | 
 | 2587 |     ALOGV("setAudioPortConfig() on port handle %d", config->id); | 
 | 2588 |     // Only support gain configuration for now | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2589 |     if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { | 
 | 2590 |         return INVALID_OPERATION; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2591 |     } | 
 | 2592 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2593 |     sp<AudioPortConfig> audioPortConfig; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2594 |     if (config->type == AUDIO_PORT_TYPE_MIX) { | 
 | 2595 |         if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2596 |             sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2597 |             if (outputDesc == NULL) { | 
 | 2598 |                 return BAD_VALUE; | 
 | 2599 |             } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2600 |             ALOG_ASSERT(!outputDesc->isDuplicated(), | 
 | 2601 |                         "setAudioPortConfig() called on duplicated output %d", | 
 | 2602 |                         outputDesc->mIoHandle); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2603 |             audioPortConfig = outputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2604 |         } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2605 |             sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2606 |             if (inputDesc == NULL) { | 
 | 2607 |                 return BAD_VALUE; | 
 | 2608 |             } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2609 |             audioPortConfig = inputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2610 |         } else { | 
 | 2611 |             return BAD_VALUE; | 
 | 2612 |         } | 
 | 2613 |     } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { | 
 | 2614 |         sp<DeviceDescriptor> deviceDesc; | 
 | 2615 |         if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
 | 2616 |             deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); | 
 | 2617 |         } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
 | 2618 |             deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); | 
 | 2619 |         } else { | 
 | 2620 |             return BAD_VALUE; | 
 | 2621 |         } | 
 | 2622 |         if (deviceDesc == NULL) { | 
 | 2623 |             return BAD_VALUE; | 
 | 2624 |         } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2625 |         audioPortConfig = deviceDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2626 |     } else { | 
 | 2627 |         return BAD_VALUE; | 
 | 2628 |     } | 
 | 2629 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2630 |     struct audio_port_config backupConfig; | 
 | 2631 |     status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); | 
 | 2632 |     if (status == NO_ERROR) { | 
 | 2633 |         struct audio_port_config newConfig; | 
 | 2634 |         audioPortConfig->toAudioPortConfig(&newConfig, config); | 
 | 2635 |         status = mpClientInterface->setAudioPortConfig(&newConfig, 0); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2636 |     } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2637 |     if (status != NO_ERROR) { | 
 | 2638 |         audioPortConfig->applyAudioPortConfig(&backupConfig); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2639 |     } | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2640 |  | 
 | 2641 |     return status; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2642 | } | 
 | 2643 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2644 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) | 
 | 2645 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2646 |     clearAudioSources(uid); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2647 |     clearAudioPatches(uid); | 
 | 2648 |     clearSessionRoutes(uid); | 
 | 2649 | } | 
 | 2650 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2651 | void AudioPolicyManager::clearAudioPatches(uid_t uid) | 
 | 2652 | { | 
| Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2653 |     for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--)  { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2654 |         sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); | 
 | 2655 |         if (patchDesc->mUid == uid) { | 
| Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2656 |             releaseAudioPatch(mAudioPatches.keyAt(i), uid); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2657 |         } | 
 | 2658 |     } | 
 | 2659 | } | 
 | 2660 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2661 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, | 
 | 2662 |                                             audio_io_handle_t ouptutToSkip) | 
 | 2663 | { | 
 | 2664 |     audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
 | 2665 |     SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
 | 2666 |     for (size_t j = 0; j < mOutputs.size(); j++) { | 
 | 2667 |         if (mOutputs.keyAt(j) == ouptutToSkip) { | 
 | 2668 |             continue; | 
 | 2669 |         } | 
 | 2670 |         sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); | 
 | 2671 |         if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { | 
 | 2672 |             continue; | 
 | 2673 |         } | 
 | 2674 |         // If the default device for this strategy is on another output mix, | 
 | 2675 |         // invalidate all tracks in this strategy to force re connection. | 
 | 2676 |         // Otherwise select new device on the output mix. | 
 | 2677 |         if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { | 
 | 2678 |             for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { | 
 | 2679 |                 if (stream == AUDIO_STREAM_PATCH) { | 
 | 2680 |                     continue; | 
 | 2681 |                 } | 
 | 2682 |                 if (getStrategy((audio_stream_type_t)stream) == strategy) { | 
 | 2683 |                     mpClientInterface->invalidateStream((audio_stream_type_t)stream); | 
 | 2684 |                 } | 
 | 2685 |             } | 
 | 2686 |         } else { | 
 | 2687 |             audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
 | 2688 |             setOutputDevice(outputDesc, newDevice, false); | 
 | 2689 |         } | 
 | 2690 |     } | 
 | 2691 | } | 
 | 2692 |  | 
 | 2693 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) | 
 | 2694 | { | 
 | 2695 |     // remove output routes associated with this uid | 
 | 2696 |     SortedVector<routing_strategy> affectedStrategies; | 
 | 2697 |     for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--)  { | 
 | 2698 |         sp<SessionRoute> route = mOutputRoutes.valueAt(i); | 
 | 2699 |         if (route->mUid == uid) { | 
 | 2700 |             mOutputRoutes.removeItemsAt(i); | 
 | 2701 |             if (route->mDeviceDescriptor != 0) { | 
 | 2702 |                 affectedStrategies.add(getStrategy(route->mStreamType)); | 
 | 2703 |             } | 
 | 2704 |         } | 
 | 2705 |     } | 
 | 2706 |     // reroute outputs if necessary | 
 | 2707 |     for (size_t i = 0; i < affectedStrategies.size(); i++) { | 
 | 2708 |         checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE); | 
 | 2709 |     } | 
 | 2710 |  | 
 | 2711 |     // remove input routes associated with this uid | 
 | 2712 |     SortedVector<audio_source_t> affectedSources; | 
 | 2713 |     for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--)  { | 
 | 2714 |         sp<SessionRoute> route = mInputRoutes.valueAt(i); | 
 | 2715 |         if (route->mUid == uid) { | 
 | 2716 |             mInputRoutes.removeItemsAt(i); | 
 | 2717 |             if (route->mDeviceDescriptor != 0) { | 
 | 2718 |                 affectedSources.add(route->mSource); | 
 | 2719 |             } | 
 | 2720 |         } | 
 | 2721 |     } | 
 | 2722 |     // reroute inputs if necessary | 
 | 2723 |     SortedVector<audio_io_handle_t> inputsToClose; | 
 | 2724 |     for (size_t i = 0; i < mInputs.size(); i++) { | 
 | 2725 |         sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2726 |         if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2727 |             inputsToClose.add(inputDesc->mIoHandle); | 
 | 2728 |         } | 
 | 2729 |     } | 
 | 2730 |     for (size_t i = 0; i < inputsToClose.size(); i++) { | 
 | 2731 |         closeInput(inputsToClose[i]); | 
 | 2732 |     } | 
 | 2733 | } | 
 | 2734 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2735 | void AudioPolicyManager::clearAudioSources(uid_t uid) | 
 | 2736 | { | 
 | 2737 |     for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--)  { | 
 | 2738 |         sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
 | 2739 |         if (sourceDesc->mUid == uid) { | 
 | 2740 |             stopAudioSource(mAudioSources.keyAt(i)); | 
 | 2741 |         } | 
 | 2742 |     } | 
 | 2743 | } | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2744 |  | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2745 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, | 
 | 2746 |                                        audio_io_handle_t *ioHandle, | 
 | 2747 |                                        audio_devices_t *device) | 
 | 2748 | { | 
 | 2749 |     *session = (audio_session_t)mpClientInterface->newAudioUniqueId(); | 
 | 2750 |     *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(); | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2751 |     *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2752 |  | 
| François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 2753 |     return mSoundTriggerSessions.acquireSession(*session, *ioHandle); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2754 | } | 
 | 2755 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2756 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, | 
 | 2757 |                                   const audio_attributes_t *attributes, | 
 | 2758 |                                   audio_io_handle_t *handle, | 
 | 2759 |                                   uid_t uid) | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 2760 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2761 |     ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); | 
 | 2762 |     if (source == NULL || attributes == NULL || handle == NULL) { | 
 | 2763 |         return BAD_VALUE; | 
 | 2764 |     } | 
 | 2765 |  | 
 | 2766 |     *handle = AUDIO_IO_HANDLE_NONE; | 
 | 2767 |  | 
 | 2768 |     if (source->role != AUDIO_PORT_ROLE_SOURCE || | 
 | 2769 |             source->type != AUDIO_PORT_TYPE_DEVICE) { | 
 | 2770 |         ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); | 
 | 2771 |         return INVALID_OPERATION; | 
 | 2772 |     } | 
 | 2773 |  | 
 | 2774 |     sp<DeviceDescriptor> srcDeviceDesc = | 
 | 2775 |             mAvailableInputDevices.getDevice(source->ext.device.type, | 
 | 2776 |                                               String8(source->ext.device.address)); | 
 | 2777 |     if (srcDeviceDesc == 0) { | 
 | 2778 |         ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); | 
 | 2779 |         return BAD_VALUE; | 
 | 2780 |     } | 
 | 2781 |     sp<AudioSourceDescriptor> sourceDesc = | 
 | 2782 |             new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); | 
 | 2783 |  | 
 | 2784 |     struct audio_patch dummyPatch; | 
 | 2785 |     sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); | 
 | 2786 |     sourceDesc->mPatchDesc = patchDesc; | 
 | 2787 |  | 
 | 2788 |     status_t status = connectAudioSource(sourceDesc); | 
 | 2789 |     if (status == NO_ERROR) { | 
 | 2790 |         mAudioSources.add(sourceDesc->getHandle(), sourceDesc); | 
 | 2791 |         *handle = sourceDesc->getHandle(); | 
 | 2792 |     } | 
 | 2793 |     return status; | 
 | 2794 | } | 
 | 2795 |  | 
 | 2796 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) | 
 | 2797 | { | 
 | 2798 |     ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); | 
 | 2799 |  | 
 | 2800 |     // make sure we only have one patch per source. | 
 | 2801 |     disconnectAudioSource(sourceDesc); | 
 | 2802 |  | 
 | 2803 |     routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); | 
 | 2804 |     audio_stream_type_t stream = audio_attributes_to_stream_type(&sourceDesc->mAttributes); | 
 | 2805 |     sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; | 
 | 2806 |  | 
 | 2807 |     audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); | 
 | 2808 |     sp<DeviceDescriptor> sinkDeviceDesc = | 
 | 2809 |             mAvailableOutputDevices.getDevice(sinkDevice, String8("")); | 
 | 2810 |  | 
 | 2811 |     audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
 | 2812 |     struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch; | 
 | 2813 |  | 
 | 2814 |     if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == | 
 | 2815 |             sinkDeviceDesc->getAudioPort()->mModule->getHandle() && | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 2816 |             srcDeviceDesc->getAudioPort()->mModule->getHalVersion() >= AUDIO_DEVICE_API_VERSION_3_0 && | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2817 |             srcDeviceDesc->getAudioPort()->mGains.size() > 0) { | 
 | 2818 |         ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); | 
 | 2819 |         //   create patch between src device and output device | 
 | 2820 |         //   create Hwoutput and add to mHwOutputs | 
 | 2821 |     } else { | 
 | 2822 |         SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); | 
 | 2823 |         audio_io_handle_t output = | 
 | 2824 |                 selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); | 
 | 2825 |         if (output == AUDIO_IO_HANDLE_NONE) { | 
 | 2826 |             ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); | 
 | 2827 |             return INVALID_OPERATION; | 
 | 2828 |         } | 
 | 2829 |         sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
 | 2830 |         if (outputDesc->isDuplicated()) { | 
 | 2831 |             ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); | 
 | 2832 |             return INVALID_OPERATION; | 
 | 2833 |         } | 
 | 2834 |         // create a special patch with no sink and two sources: | 
 | 2835 |         // - the second source indicates to PatchPanel through which output mix this patch should | 
 | 2836 |         // be connected as well as the stream type for volume control | 
 | 2837 |         // - the sink is defined by whatever output device is currently selected for the output | 
 | 2838 |         // though which this patch is routed. | 
 | 2839 |         patch->num_sinks = 0; | 
 | 2840 |         patch->num_sources = 2; | 
 | 2841 |         srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL); | 
 | 2842 |         outputDesc->toAudioPortConfig(&patch->sources[1], NULL); | 
 | 2843 |         patch->sources[1].ext.mix.usecase.stream = stream; | 
 | 2844 |         status_t status = mpClientInterface->createAudioPatch(patch, | 
 | 2845 |                                                               &afPatchHandle, | 
 | 2846 |                                                               0); | 
 | 2847 |         ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, | 
 | 2848 |                                                               status, afPatchHandle); | 
 | 2849 |         if (status != NO_ERROR) { | 
 | 2850 |             ALOGW("%s patch panel could not connect device patch, error %d", | 
 | 2851 |                   __FUNCTION__, status); | 
 | 2852 |             return INVALID_OPERATION; | 
 | 2853 |         } | 
 | 2854 |         uint32_t delayMs = 0; | 
 | 2855 |         status = startSource(outputDesc, stream, sinkDevice, &delayMs); | 
 | 2856 |  | 
 | 2857 |         if (status != NO_ERROR) { | 
 | 2858 |             mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); | 
 | 2859 |             return status; | 
 | 2860 |         } | 
 | 2861 |         sourceDesc->mSwOutput = outputDesc; | 
 | 2862 |         if (delayMs != 0) { | 
 | 2863 |             usleep(delayMs * 1000); | 
 | 2864 |         } | 
 | 2865 |     } | 
 | 2866 |  | 
 | 2867 |     sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; | 
 | 2868 |     addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); | 
 | 2869 |  | 
 | 2870 |     return NO_ERROR; | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 2871 | } | 
 | 2872 |  | 
| Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 2873 | status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused) | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 2874 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2875 |     sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); | 
 | 2876 |     ALOGV("%s handle %d", __FUNCTION__, handle); | 
 | 2877 |     if (sourceDesc == 0) { | 
 | 2878 |         ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); | 
 | 2879 |         return BAD_VALUE; | 
 | 2880 |     } | 
 | 2881 |     status_t status = disconnectAudioSource(sourceDesc); | 
 | 2882 |  | 
 | 2883 |     mAudioSources.removeItem(handle); | 
 | 2884 |     return status; | 
 | 2885 | } | 
 | 2886 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame^] | 2887 | status_t AudioPolicyManager::setMasterMono(bool mono) | 
 | 2888 | { | 
 | 2889 |     if (mMasterMono == mono) { | 
 | 2890 |         return NO_ERROR; | 
 | 2891 |     } | 
 | 2892 |     mMasterMono = mono; | 
 | 2893 |     // if enabling mono we close all offloaded devices, which will invalidate the | 
 | 2894 |     // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible | 
 | 2895 |     // for recreating the new AudioTrack as non-offloaded PCM. | 
 | 2896 |     // | 
 | 2897 |     // If disabling mono, we leave all tracks as is: we don't know which clients | 
 | 2898 |     // and tracks are able to be recreated as offloaded. The next "song" should | 
 | 2899 |     // play back offloaded. | 
 | 2900 |     if (mMasterMono) { | 
 | 2901 |         Vector<audio_io_handle_t> offloaded; | 
 | 2902 |         for (size_t i = 0; i < mOutputs.size(); ++i) { | 
 | 2903 |             sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
 | 2904 |             if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { | 
 | 2905 |                 offloaded.push(desc->mIoHandle); | 
 | 2906 |             } | 
 | 2907 |         } | 
 | 2908 |         for (size_t i = 0; i < offloaded.size(); ++i) { | 
 | 2909 |             closeOutput(offloaded[i]); | 
 | 2910 |         } | 
 | 2911 |     } | 
 | 2912 |     // update master mono for all remaining outputs | 
 | 2913 |     for (size_t i = 0; i < mOutputs.size(); ++i) { | 
 | 2914 |         updateMono(mOutputs.keyAt(i)); | 
 | 2915 |     } | 
 | 2916 |     return NO_ERROR; | 
 | 2917 | } | 
 | 2918 |  | 
 | 2919 | status_t AudioPolicyManager::getMasterMono(bool *mono) | 
 | 2920 | { | 
 | 2921 |     *mono = mMasterMono; | 
 | 2922 |     return NO_ERROR; | 
 | 2923 | } | 
 | 2924 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2925 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) | 
 | 2926 | { | 
 | 2927 |     ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); | 
 | 2928 |  | 
 | 2929 |     sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); | 
 | 2930 |     if (patchDesc == 0) { | 
 | 2931 |         ALOGW("%s source has no patch with handle %d", __FUNCTION__, | 
 | 2932 |               sourceDesc->mPatchDesc->mHandle); | 
 | 2933 |         return BAD_VALUE; | 
 | 2934 |     } | 
 | 2935 |     removeAudioPatch(sourceDesc->mPatchDesc->mHandle); | 
 | 2936 |  | 
 | 2937 |     audio_stream_type_t stream = audio_attributes_to_stream_type(&sourceDesc->mAttributes); | 
 | 2938 |     sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); | 
 | 2939 |     if (swOutputDesc != 0) { | 
 | 2940 |         stopSource(swOutputDesc, stream, false); | 
 | 2941 |         mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
 | 2942 |     } else { | 
 | 2943 |         sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); | 
 | 2944 |         if (hwOutputDesc != 0) { | 
 | 2945 |           //   release patch between src device and output device | 
 | 2946 |           //   close Hwoutput and remove from mHwOutputs | 
 | 2947 |         } else { | 
 | 2948 |             ALOGW("%s source has neither SW nor HW output", __FUNCTION__); | 
 | 2949 |         } | 
 | 2950 |     } | 
 | 2951 |     return NO_ERROR; | 
 | 2952 | } | 
 | 2953 |  | 
 | 2954 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( | 
 | 2955 |         audio_io_handle_t output, routing_strategy strategy) | 
 | 2956 | { | 
 | 2957 |     sp<AudioSourceDescriptor> source; | 
 | 2958 |     for (size_t i = 0; i < mAudioSources.size(); i++)  { | 
 | 2959 |         sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
 | 2960 |         routing_strategy sourceStrategy = | 
 | 2961 |                 (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); | 
 | 2962 |         sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); | 
 | 2963 |         if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { | 
 | 2964 |             source = sourceDesc; | 
 | 2965 |             break; | 
 | 2966 |         } | 
 | 2967 |     } | 
 | 2968 |     return source; | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 2969 | } | 
 | 2970 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2971 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2972 | // AudioPolicyManager | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2973 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2974 | uint32_t AudioPolicyManager::nextAudioPortGeneration() | 
 | 2975 | { | 
 | 2976 |     return android_atomic_inc(&mAudioPortGeneration); | 
 | 2977 | } | 
 | 2978 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2979 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2980 |     : | 
 | 2981 | #ifdef AUDIO_POLICY_TEST | 
 | 2982 |     Thread(false), | 
 | 2983 | #endif //AUDIO_POLICY_TEST | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2984 |     mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2985 |     mA2dpSuspended(false), | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2986 |     mSpeakerDrcEnabled(false), | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2987 |     mAudioPortGeneration(1), | 
 | 2988 |     mBeaconMuteRefCount(0), | 
 | 2989 |     mBeaconPlayingRefCount(0), | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2990 |     mBeaconMuted(false), | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame^] | 2991 |     mTtsOutputAvailable(false), | 
 | 2992 |     mMasterMono(false) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2993 | { | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2994 |     audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); | 
 | 2995 |     if (!engineInstance) { | 
 | 2996 |         ALOGE("%s:  Could not get an instance of policy engine", __FUNCTION__); | 
 | 2997 |         return; | 
 | 2998 |     } | 
 | 2999 |     // Retrieve the Policy Manager Interface | 
 | 3000 |     mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); | 
 | 3001 |     if (mEngine == NULL) { | 
 | 3002 |         ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); | 
 | 3003 |         return; | 
 | 3004 |     } | 
 | 3005 |     mEngine->setObserver(this); | 
 | 3006 |     status_t status = mEngine->initCheck(); | 
 | 3007 |     ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status); | 
 | 3008 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3009 |     mUidCached = getuid(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3010 |     mpClientInterface = clientInterface; | 
 | 3011 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3012 |     AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, | 
 | 3013 |                              mDefaultOutputDevice, mSpeakerDrcEnabled); | 
 | 3014 |  | 
 | 3015 |     if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) && | 
 | 3016 |         (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) { | 
 | 3017 |  | 
 | 3018 |         ALOGE("could not load audio policy configuration file, setting defaults"); | 
 | 3019 |         config.setDefault(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3020 |     } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3021 |     // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3022 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3023 |     // must be done after reading the policy (since conditionned by Speaker Drc Enabling) | 
 | 3024 |     mEngine->initializeVolumeCurves(mSpeakerDrcEnabled); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3025 |  | 
 | 3026 |     // open all output streams needed to access attached devices | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3027 |     audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); | 
 | 3028 |     audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3029 |     for (size_t i = 0; i < mHwModules.size(); i++) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3030 |         mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3031 |         if (mHwModules[i]->mHandle == 0) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3032 |             ALOGW("could not open HW module %s", mHwModules[i]->getName()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3033 |             continue; | 
 | 3034 |         } | 
 | 3035 |         // open all output streams needed to access attached devices | 
 | 3036 |         // except for direct output streams that are only opened when they are actually | 
 | 3037 |         // required by an app. | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3038 |         // This also validates mAvailableOutputDevices list | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3039 |         for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
 | 3040 |         { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3041 |             const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3042 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3043 |             if (!outProfile->hasSupportedDevices()) { | 
 | 3044 |                 ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3045 |                 continue; | 
 | 3046 |             } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3047 |             if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3048 |                 mTtsOutputAvailable = true; | 
 | 3049 |             } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3050 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3051 |             if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3052 |                 continue; | 
 | 3053 |             } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3054 |             audio_devices_t profileType = outProfile->getSupportedDevicesType(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3055 |             if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { | 
 | 3056 |                 profileType = mDefaultOutputDevice->type(); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3057 |             } else { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3058 |                 // chose first device present in profile's SupportedDevices also part of | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3059 |                 // outputDeviceTypes | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3060 |                 profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3061 |             } | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3062 |             if ((profileType & outputDeviceTypes) == 0) { | 
 | 3063 |                 continue; | 
 | 3064 |             } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3065 |             sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, | 
 | 3066 |                                                                                  mpClientInterface); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3067 |  | 
 | 3068 |             outputDesc->mDevice = profileType; | 
 | 3069 |             audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
 | 3070 |             config.sample_rate = outputDesc->mSamplingRate; | 
 | 3071 |             config.channel_mask = outputDesc->mChannelMask; | 
 | 3072 |             config.format = outputDesc->mFormat; | 
 | 3073 |             audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3074 |             status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(), | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3075 |                                                             &output, | 
 | 3076 |                                                             &config, | 
 | 3077 |                                                             &outputDesc->mDevice, | 
 | 3078 |                                                             String8(""), | 
 | 3079 |                                                             &outputDesc->mLatency, | 
 | 3080 |                                                             outputDesc->mFlags); | 
 | 3081 |  | 
 | 3082 |             if (status != NO_ERROR) { | 
 | 3083 |                 ALOGW("Cannot open output stream for device %08x on hw module %s", | 
 | 3084 |                       outputDesc->mDevice, | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3085 |                       mHwModules[i]->getName()); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3086 |             } else { | 
 | 3087 |                 outputDesc->mSamplingRate = config.sample_rate; | 
 | 3088 |                 outputDesc->mChannelMask = config.channel_mask; | 
 | 3089 |                 outputDesc->mFormat = config.format; | 
 | 3090 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3091 |                 const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); | 
 | 3092 |                 for (size_t k = 0; k  < supportedDevices.size(); k++) { | 
 | 3093 |                     ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3094 |                     // 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] | 3095 |                     if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { | 
 | 3096 |                         mAvailableOutputDevices[index]->attach(mHwModules[i]); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3097 |                     } | 
 | 3098 |                 } | 
 | 3099 |                 if (mPrimaryOutput == 0 && | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3100 |                         outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3101 |                     mPrimaryOutput = outputDesc; | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3102 |                 } | 
 | 3103 |                 addOutput(output, outputDesc); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3104 |                 setOutputDevice(outputDesc, | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3105 |                                 outputDesc->mDevice, | 
 | 3106 |                                 true); | 
 | 3107 |             } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3108 |         } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3109 |         // open input streams needed to access attached devices to validate | 
 | 3110 |         // mAvailableInputDevices list | 
 | 3111 |         for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) | 
 | 3112 |         { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3113 |             const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3114 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3115 |             if (!inProfile->hasSupportedDevices()) { | 
 | 3116 |                 ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3117 |                 continue; | 
 | 3118 |             } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3119 |             // chose first device present in profile's SupportedDevices also part of | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3120 |             // inputDeviceTypes | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3121 |             audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); | 
 | 3122 |  | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3123 |             if ((profileType & inputDeviceTypes) == 0) { | 
 | 3124 |                 continue; | 
 | 3125 |             } | 
 | 3126 |             sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile); | 
 | 3127 |  | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3128 |             inputDesc->mDevice = profileType; | 
 | 3129 |  | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3130 |             // find the address | 
 | 3131 |             DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); | 
 | 3132 |             //   the inputs vector must be of size 1, but we don't want to crash here | 
 | 3133 |             String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress | 
 | 3134 |                     : String8(""); | 
 | 3135 |             ALOGV("  for input device 0x%x using address %s", profileType, address.string()); | 
 | 3136 |             ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); | 
 | 3137 |  | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3138 |             audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
 | 3139 |             config.sample_rate = inputDesc->mSamplingRate; | 
 | 3140 |             config.channel_mask = inputDesc->mChannelMask; | 
 | 3141 |             config.format = inputDesc->mFormat; | 
 | 3142 |             audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3143 |             status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(), | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3144 |                                                            &input, | 
 | 3145 |                                                            &config, | 
 | 3146 |                                                            &inputDesc->mDevice, | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3147 |                                                            address, | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3148 |                                                            AUDIO_SOURCE_MIC, | 
 | 3149 |                                                            AUDIO_INPUT_FLAG_NONE); | 
 | 3150 |  | 
 | 3151 |             if (status == NO_ERROR) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3152 |                 const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); | 
 | 3153 |                 for (size_t k = 0; k  < supportedDevices.size(); k++) { | 
 | 3154 |                     ssize_t index =  mAvailableInputDevices.indexOf(supportedDevices[k]); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3155 |                     // 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] | 3156 |                     if (index >= 0) { | 
 | 3157 |                         sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; | 
 | 3158 |                         if (!devDesc->isAttached()) { | 
 | 3159 |                             devDesc->attach(mHwModules[i]); | 
 | 3160 |                             devDesc->importAudioPort(inProfile); | 
 | 3161 |                         } | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3162 |                     } | 
 | 3163 |                 } | 
 | 3164 |                 mpClientInterface->closeInput(input); | 
 | 3165 |             } else { | 
 | 3166 |                 ALOGW("Cannot open input stream for device %08x on hw module %s", | 
 | 3167 |                       inputDesc->mDevice, | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3168 |                       mHwModules[i]->getName()); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3169 |             } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3170 |         } | 
 | 3171 |     } | 
 | 3172 |     // make sure all attached devices have been allocated a unique ID | 
 | 3173 |     for (size_t i = 0; i  < mAvailableOutputDevices.size();) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3174 |         if (!mAvailableOutputDevices[i]->isAttached()) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3175 |             ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3176 |             mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); | 
 | 3177 |             continue; | 
 | 3178 |         } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3179 |         // The device is now validated and can be appended to the available devices of the engine | 
 | 3180 |         mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], | 
 | 3181 |                                           AUDIO_POLICY_DEVICE_STATE_AVAILABLE); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3182 |         i++; | 
 | 3183 |     } | 
 | 3184 |     for (size_t i = 0; i  < mAvailableInputDevices.size();) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3185 |         if (!mAvailableInputDevices[i]->isAttached()) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3186 |             ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3187 |             mAvailableInputDevices.remove(mAvailableInputDevices[i]); | 
 | 3188 |             continue; | 
 | 3189 |         } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3190 |         // The device is now validated and can be appended to the available devices of the engine | 
 | 3191 |         mEngine->setDeviceConnectionState(mAvailableInputDevices[i], | 
 | 3192 |                                           AUDIO_POLICY_DEVICE_STATE_AVAILABLE); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3193 |         i++; | 
 | 3194 |     } | 
 | 3195 |     // make sure default device is reachable | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3196 |     if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3197 |         ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3198 |     } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3199 |  | 
 | 3200 |     ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); | 
 | 3201 |  | 
 | 3202 |     updateDevicesAndOutputs(); | 
 | 3203 |  | 
 | 3204 | #ifdef AUDIO_POLICY_TEST | 
 | 3205 |     if (mPrimaryOutput != 0) { | 
 | 3206 |         AudioParameter outputCmd = AudioParameter(); | 
 | 3207 |         outputCmd.addInt(String8("set_id"), 0); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3208 |         mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3209 |  | 
 | 3210 |         mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; | 
 | 3211 |         mTestSamplingRate = 44100; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3212 |         mTestFormat = AUDIO_FORMAT_PCM_16_BIT; | 
 | 3213 |         mTestChannels =  AUDIO_CHANNEL_OUT_STEREO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3214 |         mTestLatencyMs = 0; | 
 | 3215 |         mCurOutput = 0; | 
 | 3216 |         mDirectOutput = false; | 
 | 3217 |         for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { | 
 | 3218 |             mTestOutputs[i] = 0; | 
 | 3219 |         } | 
 | 3220 |  | 
 | 3221 |         const size_t SIZE = 256; | 
 | 3222 |         char buffer[SIZE]; | 
 | 3223 |         snprintf(buffer, SIZE, "AudioPolicyManagerTest"); | 
 | 3224 |         run(buffer, ANDROID_PRIORITY_AUDIO); | 
 | 3225 |     } | 
 | 3226 | #endif //AUDIO_POLICY_TEST | 
 | 3227 | } | 
 | 3228 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3229 | AudioPolicyManager::~AudioPolicyManager() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3230 | { | 
 | 3231 | #ifdef AUDIO_POLICY_TEST | 
 | 3232 |     exit(); | 
 | 3233 | #endif //AUDIO_POLICY_TEST | 
 | 3234 |    for (size_t i = 0; i < mOutputs.size(); i++) { | 
 | 3235 |         mpClientInterface->closeOutput(mOutputs.keyAt(i)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3236 |    } | 
 | 3237 |    for (size_t i = 0; i < mInputs.size(); i++) { | 
 | 3238 |         mpClientInterface->closeInput(mInputs.keyAt(i)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3239 |    } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3240 |    mAvailableOutputDevices.clear(); | 
 | 3241 |    mAvailableInputDevices.clear(); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3242 |    mOutputs.clear(); | 
 | 3243 |    mInputs.clear(); | 
 | 3244 |    mHwModules.clear(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3245 | } | 
 | 3246 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3247 | status_t AudioPolicyManager::initCheck() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3248 | { | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3249 |     return hasPrimaryOutput() ? NO_ERROR : NO_INIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3250 | } | 
 | 3251 |  | 
 | 3252 | #ifdef AUDIO_POLICY_TEST | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3253 | bool AudioPolicyManager::threadLoop() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3254 | { | 
 | 3255 |     ALOGV("entering threadLoop()"); | 
 | 3256 |     while (!exitPending()) | 
 | 3257 |     { | 
 | 3258 |         String8 command; | 
 | 3259 |         int valueInt; | 
 | 3260 |         String8 value; | 
 | 3261 |  | 
 | 3262 |         Mutex::Autolock _l(mLock); | 
 | 3263 |         mWaitWorkCV.waitRelative(mLock, milliseconds(50)); | 
 | 3264 |  | 
 | 3265 |         command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); | 
 | 3266 |         AudioParameter param = AudioParameter(command); | 
 | 3267 |  | 
 | 3268 |         if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && | 
 | 3269 |             valueInt != 0) { | 
 | 3270 |             ALOGV("Test command %s received", command.string()); | 
 | 3271 |             String8 target; | 
 | 3272 |             if (param.get(String8("target"), target) != NO_ERROR) { | 
 | 3273 |                 target = "Manager"; | 
 | 3274 |             } | 
 | 3275 |             if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { | 
 | 3276 |                 param.remove(String8("test_cmd_policy_output")); | 
 | 3277 |                 mCurOutput = valueInt; | 
 | 3278 |             } | 
 | 3279 |             if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { | 
 | 3280 |                 param.remove(String8("test_cmd_policy_direct")); | 
 | 3281 |                 if (value == "false") { | 
 | 3282 |                     mDirectOutput = false; | 
 | 3283 |                 } else if (value == "true") { | 
 | 3284 |                     mDirectOutput = true; | 
 | 3285 |                 } | 
 | 3286 |             } | 
 | 3287 |             if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { | 
 | 3288 |                 param.remove(String8("test_cmd_policy_input")); | 
 | 3289 |                 mTestInput = valueInt; | 
 | 3290 |             } | 
 | 3291 |  | 
 | 3292 |             if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { | 
 | 3293 |                 param.remove(String8("test_cmd_policy_format")); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3294 |                 int format = AUDIO_FORMAT_INVALID; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3295 |                 if (value == "PCM 16 bits") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3296 |                     format = AUDIO_FORMAT_PCM_16_BIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3297 |                 } else if (value == "PCM 8 bits") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3298 |                     format = AUDIO_FORMAT_PCM_8_BIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3299 |                 } else if (value == "Compressed MP3") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3300 |                     format = AUDIO_FORMAT_MP3; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3301 |                 } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3302 |                 if (format != AUDIO_FORMAT_INVALID) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3303 |                     if (target == "Manager") { | 
 | 3304 |                         mTestFormat = format; | 
 | 3305 |                     } else if (mTestOutputs[mCurOutput] != 0) { | 
 | 3306 |                         AudioParameter outputParam = AudioParameter(); | 
 | 3307 |                         outputParam.addInt(String8("format"), format); | 
 | 3308 |                         mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
 | 3309 |                     } | 
 | 3310 |                 } | 
 | 3311 |             } | 
 | 3312 |             if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { | 
 | 3313 |                 param.remove(String8("test_cmd_policy_channels")); | 
 | 3314 |                 int channels = 0; | 
 | 3315 |  | 
 | 3316 |                 if (value == "Channels Stereo") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3317 |                     channels =  AUDIO_CHANNEL_OUT_STEREO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3318 |                 } else if (value == "Channels Mono") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3319 |                     channels =  AUDIO_CHANNEL_OUT_MONO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3320 |                 } | 
 | 3321 |                 if (channels != 0) { | 
 | 3322 |                     if (target == "Manager") { | 
 | 3323 |                         mTestChannels = channels; | 
 | 3324 |                     } else if (mTestOutputs[mCurOutput] != 0) { | 
 | 3325 |                         AudioParameter outputParam = AudioParameter(); | 
 | 3326 |                         outputParam.addInt(String8("channels"), channels); | 
 | 3327 |                         mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
 | 3328 |                     } | 
 | 3329 |                 } | 
 | 3330 |             } | 
 | 3331 |             if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { | 
 | 3332 |                 param.remove(String8("test_cmd_policy_sampleRate")); | 
 | 3333 |                 if (valueInt >= 0 && valueInt <= 96000) { | 
 | 3334 |                     int samplingRate = valueInt; | 
 | 3335 |                     if (target == "Manager") { | 
 | 3336 |                         mTestSamplingRate = samplingRate; | 
 | 3337 |                     } else if (mTestOutputs[mCurOutput] != 0) { | 
 | 3338 |                         AudioParameter outputParam = AudioParameter(); | 
 | 3339 |                         outputParam.addInt(String8("sampling_rate"), samplingRate); | 
 | 3340 |                         mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
 | 3341 |                     } | 
 | 3342 |                 } | 
 | 3343 |             } | 
 | 3344 |  | 
 | 3345 |             if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { | 
 | 3346 |                 param.remove(String8("test_cmd_policy_reopen")); | 
 | 3347 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3348 |                 mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput);); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3349 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3350 |                 audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3351 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3352 |                 removeOutput(mPrimaryOutput->mIoHandle); | 
 | 3353 |                 sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL, | 
 | 3354 |                                                                                mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3355 |                 outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3356 |                 audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
 | 3357 |                 config.sample_rate = outputDesc->mSamplingRate; | 
 | 3358 |                 config.channel_mask = outputDesc->mChannelMask; | 
 | 3359 |                 config.format = outputDesc->mFormat; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3360 |                 audio_io_handle_t handle; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3361 |                 status_t status = mpClientInterface->openOutput(moduleHandle, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3362 |                                                                 &handle, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3363 |                                                                 &config, | 
 | 3364 |                                                                 &outputDesc->mDevice, | 
 | 3365 |                                                                 String8(""), | 
 | 3366 |                                                                 &outputDesc->mLatency, | 
 | 3367 |                                                                 outputDesc->mFlags); | 
 | 3368 |                 if (status != NO_ERROR) { | 
 | 3369 |                     ALOGE("Failed to reopen hardware output stream, " | 
 | 3370 |                         "samplingRate: %d, format %d, channels %d", | 
 | 3371 |                         outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3372 |                 } else { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3373 |                     outputDesc->mSamplingRate = config.sample_rate; | 
 | 3374 |                     outputDesc->mChannelMask = config.channel_mask; | 
 | 3375 |                     outputDesc->mFormat = config.format; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3376 |                     mPrimaryOutput = outputDesc; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3377 |                     AudioParameter outputCmd = AudioParameter(); | 
 | 3378 |                     outputCmd.addInt(String8("set_id"), 0); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3379 |                     mpClientInterface->setParameters(handle, outputCmd.toString()); | 
 | 3380 |                     addOutput(handle, outputDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3381 |                 } | 
 | 3382 |             } | 
 | 3383 |  | 
 | 3384 |  | 
 | 3385 |             mpClientInterface->setParameters(0, String8("test_cmd_policy=")); | 
 | 3386 |         } | 
 | 3387 |     } | 
 | 3388 |     return false; | 
 | 3389 | } | 
 | 3390 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3391 | void AudioPolicyManager::exit() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3392 | { | 
 | 3393 |     { | 
 | 3394 |         AutoMutex _l(mLock); | 
 | 3395 |         requestExit(); | 
 | 3396 |         mWaitWorkCV.signal(); | 
 | 3397 |     } | 
 | 3398 |     requestExitAndWait(); | 
 | 3399 | } | 
 | 3400 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3401 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3402 | { | 
 | 3403 |     for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { | 
 | 3404 |         if (output == mTestOutputs[i]) return i; | 
 | 3405 |     } | 
 | 3406 |     return 0; | 
 | 3407 | } | 
 | 3408 | #endif //AUDIO_POLICY_TEST | 
 | 3409 |  | 
 | 3410 | // --- | 
 | 3411 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3412 | void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3413 | { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3414 |     outputDesc->setIoHandle(output); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3415 |     mOutputs.add(output, outputDesc); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame^] | 3416 |     updateMono(output); // update mono status when adding to output list | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3417 |     nextAudioPortGeneration(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3418 | } | 
 | 3419 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3420 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) | 
 | 3421 | { | 
 | 3422 |     mOutputs.removeItem(output); | 
 | 3423 | } | 
 | 3424 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3425 | void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc) | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3426 | { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3427 |     inputDesc->setIoHandle(input); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3428 |     mInputs.add(input, inputDesc); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3429 |     nextAudioPortGeneration(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3430 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3431 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3432 | void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/, | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3433 |         const audio_devices_t device /*in*/, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3434 |         const String8 address /*in*/, | 
 | 3435 |         SortedVector<audio_io_handle_t>& outputs /*out*/) { | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3436 |     sp<DeviceDescriptor> devDesc = | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3437 |         desc->mProfile->getSupportedDeviceByAddress(device, address); | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3438 |     if (devDesc != 0) { | 
 | 3439 |         ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", | 
 | 3440 |               desc->mIoHandle, address.string()); | 
 | 3441 |         outputs.add(desc->mIoHandle); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3442 |     } | 
 | 3443 | } | 
 | 3444 |  | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3445 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3446 |                                                    audio_policy_dev_state_t state, | 
 | 3447 |                                                    SortedVector<audio_io_handle_t>& outputs, | 
 | 3448 |                                                    const String8 address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3449 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3450 |     audio_devices_t device = devDesc->type(); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3451 |     sp<SwAudioOutputDescriptor> desc; | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3452 |  | 
 | 3453 |     if (audio_device_is_digital(device)) { | 
 | 3454 |         // erase all current sample rates, formats and channel masks | 
 | 3455 |         devDesc->clearCapabilities(); | 
 | 3456 |     } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3457 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3458 |     if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3459 |         // first list already open outputs that can be routed to this device | 
 | 3460 |         for (size_t i = 0; i < mOutputs.size(); i++) { | 
 | 3461 |             desc = mOutputs.valueAt(i); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3462 |             if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3463 |                 if (!device_distinguishes_on_address(device)) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3464 |                     ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); | 
 | 3465 |                     outputs.add(mOutputs.keyAt(i)); | 
 | 3466 |                 } else { | 
 | 3467 |                     ALOGV("  checking address match due to device 0x%x", device); | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3468 |                     findIoHandlesByAddress(desc, device, address, outputs); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3469 |                 } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3470 |             } | 
 | 3471 |         } | 
 | 3472 |         // then look for output profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3473 |         SortedVector< sp<IOProfile> > profiles; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3474 |         for (size_t i = 0; i < mHwModules.size(); i++) | 
 | 3475 |         { | 
 | 3476 |             if (mHwModules[i]->mHandle == 0) { | 
 | 3477 |                 continue; | 
 | 3478 |             } | 
 | 3479 |             for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
 | 3480 |             { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3481 |                 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3482 |                 if (profile->supportDevice(device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3483 |                     if (!device_distinguishes_on_address(device) || | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3484 |                             profile->supportDeviceAddress(address)) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3485 |                         profiles.add(profile); | 
 | 3486 |                         ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); | 
 | 3487 |                     } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3488 |                 } | 
 | 3489 |             } | 
 | 3490 |         } | 
 | 3491 |  | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3492 |         ALOGV("  found %zu profiles, %zu outputs", profiles.size(), outputs.size()); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3493 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3494 |         if (profiles.isEmpty() && outputs.isEmpty()) { | 
 | 3495 |             ALOGW("checkOutputsForDevice(): No output available for device %04x", device); | 
 | 3496 |             return BAD_VALUE; | 
 | 3497 |         } | 
 | 3498 |  | 
 | 3499 |         // open outputs for matching profiles if needed. Direct outputs are also opened to | 
 | 3500 |         // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
 | 3501 |         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] | 3502 |             sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3503 |  | 
 | 3504 |             // nothing to do if one output is already opened for this profile | 
 | 3505 |             size_t j; | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3506 |             for (j = 0; j < outputs.size(); j++) { | 
 | 3507 |                 desc = mOutputs.valueFor(outputs.itemAt(j)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3508 |                 if (!desc->isDuplicated() && desc->mProfile == profile) { | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3509 |                     // matching profile: save the sample rates, format and channel masks supported | 
 | 3510 |                     // by the profile in our device descriptor | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3511 |                     if (audio_device_is_digital(device)) { | 
 | 3512 |                         devDesc->importAudioPort(profile); | 
 | 3513 |                     } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3514 |                     break; | 
 | 3515 |                 } | 
 | 3516 |             } | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3517 |             if (j != outputs.size()) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3518 |                 continue; | 
 | 3519 |             } | 
 | 3520 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3521 |             ALOGV("opening output for device %08x with params %s profile %p", | 
 | 3522 |                                                       device, address.string(), profile.get()); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3523 |             desc = new SwAudioOutputDescriptor(profile, mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3524 |             desc->mDevice = device; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3525 |             audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
 | 3526 |             config.sample_rate = desc->mSamplingRate; | 
 | 3527 |             config.channel_mask = desc->mChannelMask; | 
 | 3528 |             config.format = desc->mFormat; | 
 | 3529 |             config.offload_info.sample_rate = desc->mSamplingRate; | 
 | 3530 |             config.offload_info.channel_mask = desc->mChannelMask; | 
 | 3531 |             config.offload_info.format = desc->mFormat; | 
 | 3532 |             audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3533 |             status_t status = mpClientInterface->openOutput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3534 |                                                             &output, | 
 | 3535 |                                                             &config, | 
 | 3536 |                                                             &desc->mDevice, | 
 | 3537 |                                                             address, | 
 | 3538 |                                                             &desc->mLatency, | 
 | 3539 |                                                             desc->mFlags); | 
 | 3540 |             if (status == NO_ERROR) { | 
 | 3541 |                 desc->mSamplingRate = config.sample_rate; | 
 | 3542 |                 desc->mChannelMask = config.channel_mask; | 
 | 3543 |                 desc->mFormat = config.format; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3544 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3545 |                 // Here is where the out_set_parameters() for card & device gets called | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3546 |                 if (!address.isEmpty()) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3547 |                     char *param = audio_device_address_to_parameter(device, address); | 
 | 3548 |                     mpClientInterface->setParameters(output, String8(param)); | 
 | 3549 |                     free(param); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3550 |                 } | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3551 |                 updateAudioProfiles(output, profile->getAudioProfiles()); | 
 | 3552 |                 if (!profile->hasValidAudioProfile()) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3553 |                     ALOGW("checkOutputsForDevice() missing param"); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3554 |                     mpClientInterface->closeOutput(output); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3555 |                     output = AUDIO_IO_HANDLE_NONE; | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3556 |                 } else if (profile->hasDynamicAudioProfile()) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3557 |                     mpClientInterface->closeOutput(output); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3558 |                     profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3559 |                     config.offload_info.sample_rate = config.sample_rate; | 
 | 3560 |                     config.offload_info.channel_mask = config.channel_mask; | 
 | 3561 |                     config.offload_info.format = config.format; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3562 |                     status = mpClientInterface->openOutput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3563 |                                                            &output, | 
 | 3564 |                                                            &config, | 
 | 3565 |                                                            &desc->mDevice, | 
 | 3566 |                                                            address, | 
 | 3567 |                                                            &desc->mLatency, | 
 | 3568 |                                                            desc->mFlags); | 
 | 3569 |                     if (status == NO_ERROR) { | 
 | 3570 |                         desc->mSamplingRate = config.sample_rate; | 
 | 3571 |                         desc->mChannelMask = config.channel_mask; | 
 | 3572 |                         desc->mFormat = config.format; | 
 | 3573 |                     } else { | 
 | 3574 |                         output = AUDIO_IO_HANDLE_NONE; | 
 | 3575 |                     } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3576 |                 } | 
 | 3577 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3578 |                 if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3579 |                     addOutput(output, desc); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3580 |                     if (device_distinguishes_on_address(device) && address != "0") { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3581 |                         sp<AudioPolicyMix> policyMix; | 
 | 3582 |                         if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3583 |                             ALOGE("checkOutputsForDevice() cannot find policy for address %s", | 
 | 3584 |                                   address.string()); | 
 | 3585 |                         } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3586 |                         policyMix->setOutput(desc); | 
| Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 3587 |                         desc->mPolicyMix = policyMix->getMix(); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3588 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3589 |                     } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && | 
 | 3590 |                                     hasPrimaryOutput()) { | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3591 |                         // no duplicated output for direct outputs and | 
 | 3592 |                         // outputs used by dynamic policy mixes | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3593 |                         audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3594 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3595 |                         // set initial stream volume for device | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3596 |                         applyStreamVolumes(desc, device, 0, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3597 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3598 |                         //TODO: configure audio effect output stage here | 
 | 3599 |  | 
 | 3600 |                         // 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] | 3601 |                         duplicatedOutput = | 
 | 3602 |                                 mpClientInterface->openDuplicateOutput(output, | 
 | 3603 |                                                                        mPrimaryOutput->mIoHandle); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3604 |                         if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3605 |                             // add duplicated output descriptor | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3606 |                             sp<SwAudioOutputDescriptor> dupOutputDesc = | 
 | 3607 |                                     new SwAudioOutputDescriptor(NULL, mpClientInterface); | 
 | 3608 |                             dupOutputDesc->mOutput1 = mPrimaryOutput; | 
 | 3609 |                             dupOutputDesc->mOutput2 = desc; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3610 |                             dupOutputDesc->mSamplingRate = desc->mSamplingRate; | 
 | 3611 |                             dupOutputDesc->mFormat = desc->mFormat; | 
 | 3612 |                             dupOutputDesc->mChannelMask = desc->mChannelMask; | 
 | 3613 |                             dupOutputDesc->mLatency = desc->mLatency; | 
 | 3614 |                             addOutput(duplicatedOutput, dupOutputDesc); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3615 |                             applyStreamVolumes(dupOutputDesc, device, 0, true); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3616 |                         } else { | 
 | 3617 |                             ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3618 |                                     mPrimaryOutput->mIoHandle, output); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3619 |                             mpClientInterface->closeOutput(output); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3620 |                             removeOutput(output); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3621 |                             nextAudioPortGeneration(); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3622 |                             output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3623 |                         } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3624 |                     } | 
 | 3625 |                 } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3626 |             } else { | 
 | 3627 |                 output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3628 |             } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3629 |             if (output == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3630 |                 ALOGW("checkOutputsForDevice() could not open output for device %x", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3631 |                 profiles.removeAt(profile_index); | 
 | 3632 |                 profile_index--; | 
 | 3633 |             } else { | 
 | 3634 |                 outputs.add(output); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3635 |                 // Load digital format info only for digital devices | 
 | 3636 |                 if (audio_device_is_digital(device)) { | 
 | 3637 |                     devDesc->importAudioPort(profile); | 
 | 3638 |                 } | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3639 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3640 |                 if (device_distinguishes_on_address(device)) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3641 |                     ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", | 
 | 3642 |                             device, address.string()); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3643 |                     setOutputDevice(desc, device, true/*force*/, 0/*delay*/, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3644 |                             NULL/*patch handle*/, address.string()); | 
 | 3645 |                 } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3646 |                 ALOGV("checkOutputsForDevice(): adding output %d", output); | 
 | 3647 |             } | 
 | 3648 |         } | 
 | 3649 |  | 
 | 3650 |         if (profiles.isEmpty()) { | 
 | 3651 |             ALOGW("checkOutputsForDevice(): No output available for device %04x", device); | 
 | 3652 |             return BAD_VALUE; | 
 | 3653 |         } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3654 |     } else { // Disconnect | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3655 |         // check if one opened output is not needed any more after disconnecting one device | 
 | 3656 |         for (size_t i = 0; i < mOutputs.size(); i++) { | 
 | 3657 |             desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3658 |             if (!desc->isDuplicated()) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3659 |                 // exact match on device | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3660 |                 if (device_distinguishes_on_address(device) && | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3661 |                         (desc->supportedDevices() == device)) { | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3662 |                     findIoHandlesByAddress(desc, device, address, outputs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3663 |                 } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3664 |                     ALOGV("checkOutputsForDevice(): disconnecting adding output %d", | 
 | 3665 |                             mOutputs.keyAt(i)); | 
 | 3666 |                     outputs.add(mOutputs.keyAt(i)); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3667 |                 } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3668 |             } | 
 | 3669 |         } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3670 |         // Clear any profiles associated with the disconnected device. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3671 |         for (size_t i = 0; i < mHwModules.size(); i++) | 
 | 3672 |         { | 
 | 3673 |             if (mHwModules[i]->mHandle == 0) { | 
 | 3674 |                 continue; | 
 | 3675 |             } | 
 | 3676 |             for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
 | 3677 |             { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3678 |                 sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3679 |                 if (profile->supportDevice(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3680 |                     ALOGV("checkOutputsForDevice(): " | 
 | 3681 |                             "clearing direct output profile %zu on module %zu", j, i); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3682 |                     profile->clearAudioProfiles(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3683 |                 } | 
 | 3684 |             } | 
 | 3685 |         } | 
 | 3686 |     } | 
 | 3687 |     return NO_ERROR; | 
 | 3688 | } | 
 | 3689 |  | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3690 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor> devDesc, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3691 |                                                   audio_policy_dev_state_t state, | 
 | 3692 |                                                   SortedVector<audio_io_handle_t>& inputs, | 
 | 3693 |                                                   const String8 address) | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3694 | { | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3695 |     audio_devices_t device = devDesc->type(); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3696 |     sp<AudioInputDescriptor> desc; | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3697 |  | 
 | 3698 |     if (audio_device_is_digital(device)) { | 
 | 3699 |         // erase all current sample rates, formats and channel masks | 
 | 3700 |         devDesc->clearCapabilities(); | 
 | 3701 |     } | 
 | 3702 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3703 |     if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
 | 3704 |         // first list already open inputs that can be routed to this device | 
 | 3705 |         for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
 | 3706 |             desc = mInputs.valueAt(input_index); | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3707 |             if (desc->mProfile->supportDevice(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3708 |                 ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); | 
 | 3709 |                inputs.add(mInputs.keyAt(input_index)); | 
 | 3710 |             } | 
 | 3711 |         } | 
 | 3712 |  | 
 | 3713 |         // then look for input profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3714 |         SortedVector< sp<IOProfile> > profiles; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3715 |         for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) | 
 | 3716 |         { | 
 | 3717 |             if (mHwModules[module_idx]->mHandle == 0) { | 
 | 3718 |                 continue; | 
 | 3719 |             } | 
 | 3720 |             for (size_t profile_index = 0; | 
 | 3721 |                  profile_index < mHwModules[module_idx]->mInputProfiles.size(); | 
 | 3722 |                  profile_index++) | 
 | 3723 |             { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3724 |                 sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; | 
 | 3725 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3726 |                 if (profile->supportDevice(device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3727 |                     if (!device_distinguishes_on_address(device) || | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3728 |                             profile->supportDeviceAddress(address)) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3729 |                         profiles.add(profile); | 
 | 3730 |                         ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", | 
 | 3731 |                               profile_index, module_idx); | 
 | 3732 |                     } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3733 |                 } | 
 | 3734 |             } | 
 | 3735 |         } | 
 | 3736 |  | 
 | 3737 |         if (profiles.isEmpty() && inputs.isEmpty()) { | 
 | 3738 |             ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); | 
 | 3739 |             return BAD_VALUE; | 
 | 3740 |         } | 
 | 3741 |  | 
 | 3742 |         // open inputs for matching profiles if needed. Direct inputs are also opened to | 
 | 3743 |         // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
 | 3744 |         for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { | 
 | 3745 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3746 |             sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3747 |             // nothing to do if one input is already opened for this profile | 
 | 3748 |             size_t input_index; | 
 | 3749 |             for (input_index = 0; input_index < mInputs.size(); input_index++) { | 
 | 3750 |                 desc = mInputs.valueAt(input_index); | 
 | 3751 |                 if (desc->mProfile == profile) { | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3752 |                     if (audio_device_is_digital(device)) { | 
 | 3753 |                         devDesc->importAudioPort(profile); | 
 | 3754 |                     } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3755 |                     break; | 
 | 3756 |                 } | 
 | 3757 |             } | 
 | 3758 |             if (input_index != mInputs.size()) { | 
 | 3759 |                 continue; | 
 | 3760 |             } | 
 | 3761 |  | 
 | 3762 |             ALOGV("opening input for device 0x%X with params %s", device, address.string()); | 
 | 3763 |             desc = new AudioInputDescriptor(profile); | 
 | 3764 |             desc->mDevice = device; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3765 |             audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
 | 3766 |             config.sample_rate = desc->mSamplingRate; | 
 | 3767 |             config.channel_mask = desc->mChannelMask; | 
 | 3768 |             config.format = desc->mFormat; | 
 | 3769 |             audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3770 |             status_t status = mpClientInterface->openInput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3771 |                                                            &input, | 
 | 3772 |                                                            &config, | 
 | 3773 |                                                            &desc->mDevice, | 
 | 3774 |                                                            address, | 
 | 3775 |                                                            AUDIO_SOURCE_MIC, | 
 | 3776 |                                                            AUDIO_INPUT_FLAG_NONE /*FIXME*/); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3777 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3778 |             if (status == NO_ERROR) { | 
 | 3779 |                 desc->mSamplingRate = config.sample_rate; | 
 | 3780 |                 desc->mChannelMask = config.channel_mask; | 
 | 3781 |                 desc->mFormat = config.format; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3782 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3783 |                 if (!address.isEmpty()) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3784 |                     char *param = audio_device_address_to_parameter(device, address); | 
 | 3785 |                     mpClientInterface->setParameters(input, String8(param)); | 
 | 3786 |                     free(param); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3787 |                 } | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3788 |                 updateAudioProfiles(input, profile->getAudioProfiles()); | 
 | 3789 |                 if (!profile->hasValidAudioProfile()) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3790 |                     ALOGW("checkInputsForDevice() direct input missing param"); | 
 | 3791 |                     mpClientInterface->closeInput(input); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3792 |                     input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3793 |                 } | 
 | 3794 |  | 
 | 3795 |                 if (input != 0) { | 
 | 3796 |                     addInput(input, desc); | 
 | 3797 |                 } | 
 | 3798 |             } // endif input != 0 | 
 | 3799 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3800 |             if (input == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3801 |                 ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3802 |                 profiles.removeAt(profile_index); | 
 | 3803 |                 profile_index--; | 
 | 3804 |             } else { | 
 | 3805 |                 inputs.add(input); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3806 |                 if (audio_device_is_digital(device)) { | 
 | 3807 |                     devDesc->importAudioPort(profile); | 
 | 3808 |                 } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3809 |                 ALOGV("checkInputsForDevice(): adding input %d", input); | 
 | 3810 |             } | 
 | 3811 |         } // end scan profiles | 
 | 3812 |  | 
 | 3813 |         if (profiles.isEmpty()) { | 
 | 3814 |             ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); | 
 | 3815 |             return BAD_VALUE; | 
 | 3816 |         } | 
 | 3817 |     } else { | 
 | 3818 |         // Disconnect | 
 | 3819 |         // check if one opened input is not needed any more after disconnecting one device | 
 | 3820 |         for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
 | 3821 |             desc = mInputs.valueAt(input_index); | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3822 |             if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3823 |                 ALOGV("checkInputsForDevice(): disconnecting adding input %d", | 
 | 3824 |                       mInputs.keyAt(input_index)); | 
 | 3825 |                 inputs.add(mInputs.keyAt(input_index)); | 
 | 3826 |             } | 
 | 3827 |         } | 
 | 3828 |         // Clear any profiles associated with the disconnected device. | 
 | 3829 |         for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { | 
 | 3830 |             if (mHwModules[module_index]->mHandle == 0) { | 
 | 3831 |                 continue; | 
 | 3832 |             } | 
 | 3833 |             for (size_t profile_index = 0; | 
 | 3834 |                  profile_index < mHwModules[module_index]->mInputProfiles.size(); | 
 | 3835 |                  profile_index++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3836 |                 sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3837 |                 if (profile->supportDevice(device)) { | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 3838 |                     ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3839 |                           profile_index, module_index); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3840 |                     profile->clearAudioProfiles(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3841 |                 } | 
 | 3842 |             } | 
 | 3843 |         } | 
 | 3844 |     } // end disconnect | 
 | 3845 |  | 
 | 3846 |     return NO_ERROR; | 
 | 3847 | } | 
 | 3848 |  | 
 | 3849 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3850 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3851 | { | 
 | 3852 |     ALOGV("closeOutput(%d)", output); | 
 | 3853 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3854 |     sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3855 |     if (outputDesc == NULL) { | 
 | 3856 |         ALOGW("closeOutput() unknown output %d", output); | 
 | 3857 |         return; | 
 | 3858 |     } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3859 |     mPolicyMixes.closeOutput(outputDesc); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3860 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3861 |     // look for duplicated outputs connected to the output being removed. | 
 | 3862 |     for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3863 |         sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3864 |         if (dupOutputDesc->isDuplicated() && | 
 | 3865 |                 (dupOutputDesc->mOutput1 == outputDesc || | 
 | 3866 |                 dupOutputDesc->mOutput2 == outputDesc)) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3867 |             sp<AudioOutputDescriptor> outputDesc2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3868 |             if (dupOutputDesc->mOutput1 == outputDesc) { | 
 | 3869 |                 outputDesc2 = dupOutputDesc->mOutput2; | 
 | 3870 |             } else { | 
 | 3871 |                 outputDesc2 = dupOutputDesc->mOutput1; | 
 | 3872 |             } | 
 | 3873 |             // As all active tracks on duplicated output will be deleted, | 
 | 3874 |             // and as they were also referenced on the other output, the reference | 
 | 3875 |             // count for their stream type must be adjusted accordingly on | 
 | 3876 |             // the other output. | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3877 |             for (int j = 0; j < AUDIO_STREAM_CNT; j++) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3878 |                 int refCount = dupOutputDesc->mRefCount[j]; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3879 |                 outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3880 |             } | 
 | 3881 |             audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); | 
 | 3882 |             ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); | 
 | 3883 |  | 
 | 3884 |             mpClientInterface->closeOutput(duplicatedOutput); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3885 |             removeOutput(duplicatedOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3886 |         } | 
 | 3887 |     } | 
 | 3888 |  | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3889 |     nextAudioPortGeneration(); | 
 | 3890 |  | 
 | 3891 |     ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); | 
 | 3892 |     if (index >= 0) { | 
 | 3893 |         sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
 | 3894 |         status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
 | 3895 |         mAudioPatches.removeItemsAt(index); | 
 | 3896 |         mpClientInterface->onAudioPatchListUpdate(); | 
 | 3897 |     } | 
 | 3898 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3899 |     AudioParameter param; | 
 | 3900 |     param.add(String8("closing"), String8("true")); | 
 | 3901 |     mpClientInterface->setParameters(output, param.toString()); | 
 | 3902 |  | 
 | 3903 |     mpClientInterface->closeOutput(output); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3904 |     removeOutput(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3905 |     mPreviousOutputs = mOutputs; | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3906 | } | 
 | 3907 |  | 
 | 3908 | void AudioPolicyManager::closeInput(audio_io_handle_t input) | 
 | 3909 | { | 
 | 3910 |     ALOGV("closeInput(%d)", input); | 
 | 3911 |  | 
 | 3912 |     sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
 | 3913 |     if (inputDesc == NULL) { | 
 | 3914 |         ALOGW("closeInput() unknown input %d", input); | 
 | 3915 |         return; | 
 | 3916 |     } | 
 | 3917 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3918 |     nextAudioPortGeneration(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3919 |  | 
 | 3920 |     ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); | 
 | 3921 |     if (index >= 0) { | 
 | 3922 |         sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
 | 3923 |         status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
 | 3924 |         mAudioPatches.removeItemsAt(index); | 
 | 3925 |         mpClientInterface->onAudioPatchListUpdate(); | 
 | 3926 |     } | 
 | 3927 |  | 
 | 3928 |     mpClientInterface->closeInput(input); | 
 | 3929 |     mInputs.removeItem(input); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3930 | } | 
 | 3931 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3932 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( | 
 | 3933 |                                                                 audio_devices_t device, | 
 | 3934 |                                                                 SwAudioOutputCollection openOutputs) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3935 | { | 
 | 3936 |     SortedVector<audio_io_handle_t> outputs; | 
 | 3937 |  | 
 | 3938 |     ALOGVV("getOutputsForDevice() device %04x", device); | 
 | 3939 |     for (size_t i = 0; i < openOutputs.size(); i++) { | 
 | 3940 |         ALOGVV("output %d isDuplicated=%d device=%04x", | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3941 |                 i, openOutputs.valueAt(i)->isDuplicated(), | 
 | 3942 |                 openOutputs.valueAt(i)->supportedDevices()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3943 |         if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { | 
 | 3944 |             ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); | 
 | 3945 |             outputs.add(openOutputs.keyAt(i)); | 
 | 3946 |         } | 
 | 3947 |     } | 
 | 3948 |     return outputs; | 
 | 3949 | } | 
 | 3950 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3951 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3952 |                                       SortedVector<audio_io_handle_t>& outputs2) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3953 | { | 
 | 3954 |     if (outputs1.size() != outputs2.size()) { | 
 | 3955 |         return false; | 
 | 3956 |     } | 
 | 3957 |     for (size_t i = 0; i < outputs1.size(); i++) { | 
 | 3958 |         if (outputs1[i] != outputs2[i]) { | 
 | 3959 |             return false; | 
 | 3960 |         } | 
 | 3961 |     } | 
 | 3962 |     return true; | 
 | 3963 | } | 
 | 3964 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3965 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3966 | { | 
 | 3967 |     audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); | 
 | 3968 |     audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); | 
 | 3969 |     SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); | 
 | 3970 |     SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); | 
 | 3971 |  | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 3972 |     // also take into account external policy-related changes: add all outputs which are | 
 | 3973 |     // associated with policies in the "before" and "after" output vectors | 
 | 3974 |     ALOGVV("checkOutputForStrategy(): policy related outputs"); | 
 | 3975 |     for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3976 |         const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 3977 |         if (desc != 0 && desc->mPolicyMix != NULL) { | 
 | 3978 |             srcOutputs.add(desc->mIoHandle); | 
 | 3979 |             ALOGVV(" previous outputs: adding %d", desc->mIoHandle); | 
 | 3980 |         } | 
 | 3981 |     } | 
 | 3982 |     for (size_t i = 0 ; i < mOutputs.size() ; i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3983 |         const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 3984 |         if (desc != 0 && desc->mPolicyMix != NULL) { | 
 | 3985 |             dstOutputs.add(desc->mIoHandle); | 
 | 3986 |             ALOGVV(" new outputs: adding %d", desc->mIoHandle); | 
 | 3987 |         } | 
 | 3988 |     } | 
 | 3989 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3990 |     if (!vectorsEqual(srcOutputs,dstOutputs)) { | 
 | 3991 |         ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", | 
 | 3992 |               strategy, srcOutputs[0], dstOutputs[0]); | 
 | 3993 |         // mute strategy while moving tracks from one output to another | 
 | 3994 |         for (size_t i = 0; i < srcOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3995 |             sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3996 |             if (isStrategyActive(desc, strategy)) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3997 |                 setStrategyMute(strategy, true, desc); | 
 | 3998 |                 setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3999 |             } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4000 |             sp<AudioSourceDescriptor> source = | 
 | 4001 |                     getSourceForStrategyOnOutput(srcOutputs[i], strategy); | 
 | 4002 |             if (source != 0){ | 
 | 4003 |                 connectAudioSource(source); | 
 | 4004 |             } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4005 |         } | 
 | 4006 |  | 
 | 4007 |         // Move effects associated to this strategy from previous output to new output | 
 | 4008 |         if (strategy == STRATEGY_MEDIA) { | 
 | 4009 |             audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); | 
 | 4010 |             SortedVector<audio_io_handle_t> moved; | 
 | 4011 |             for (size_t i = 0; i < mEffects.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4012 |                 sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); | 
 | 4013 |                 if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && | 
 | 4014 |                         effectDesc->mIo != fxOutput) { | 
 | 4015 |                     if (moved.indexOf(effectDesc->mIo) < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4016 |                         ALOGV("checkOutputForStrategy() moving effect %d to output %d", | 
 | 4017 |                               mEffects.keyAt(i), fxOutput); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4018 |                         mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4019 |                                                        fxOutput); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4020 |                         moved.add(effectDesc->mIo); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4021 |                     } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4022 |                     effectDesc->mIo = fxOutput; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4023 |                 } | 
 | 4024 |             } | 
 | 4025 |         } | 
 | 4026 |         // Move tracks associated to this strategy from previous output to new output | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4027 |         for (int i = 0; i < AUDIO_STREAM_CNT; i++) { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4028 |             if (i == AUDIO_STREAM_PATCH) { | 
 | 4029 |                 continue; | 
 | 4030 |             } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4031 |             if (getStrategy((audio_stream_type_t)i) == strategy) { | 
 | 4032 |                 mpClientInterface->invalidateStream((audio_stream_type_t)i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4033 |             } | 
 | 4034 |         } | 
 | 4035 |     } | 
 | 4036 | } | 
 | 4037 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4038 | void AudioPolicyManager::checkOutputForAllStrategies() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4039 | { | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4040 |     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] | 4041 |         checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4042 |     checkOutputForStrategy(STRATEGY_PHONE); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4043 |     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] | 4044 |         checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4045 |     checkOutputForStrategy(STRATEGY_SONIFICATION); | 
 | 4046 |     checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4047 |     checkOutputForStrategy(STRATEGY_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4048 |     checkOutputForStrategy(STRATEGY_MEDIA); | 
 | 4049 |     checkOutputForStrategy(STRATEGY_DTMF); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4050 |     checkOutputForStrategy(STRATEGY_REROUTING); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4051 | } | 
 | 4052 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4053 | void AudioPolicyManager::checkA2dpSuspend() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4054 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4055 |     audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4056 |     if (a2dpOutput == 0) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4057 |         mA2dpSuspended = false; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4058 |         return; | 
 | 4059 |     } | 
 | 4060 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4061 |     bool isScoConnected = | 
| Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4062 |             ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & | 
 | 4063 |                     ~AUDIO_DEVICE_BIT_IN) != 0) || | 
 | 4064 |             ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4065 |     // suspend A2DP output if: | 
 | 4066 |     //      (NOT already suspended) && | 
 | 4067 |     //      ((SCO device is connected && | 
 | 4068 |     //       (forced usage for communication || for record is SCO))) || | 
 | 4069 |     //      (phone state is ringing || in call) | 
 | 4070 |     // | 
 | 4071 |     // restore A2DP output if: | 
 | 4072 |     //      (Already suspended) && | 
 | 4073 |     //      ((SCO device is NOT connected || | 
 | 4074 |     //       (forced usage NOT for communication && NOT for record is SCO))) && | 
 | 4075 |     //      (phone state is NOT ringing && NOT in call) | 
 | 4076 |     // | 
 | 4077 |     if (mA2dpSuspended) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4078 |         if ((!isScoConnected || | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4079 |              ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) && | 
 | 4080 |               (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) && | 
 | 4081 |              ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && | 
 | 4082 |               (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4083 |  | 
 | 4084 |             mpClientInterface->restoreOutput(a2dpOutput); | 
 | 4085 |             mA2dpSuspended = false; | 
 | 4086 |         } | 
 | 4087 |     } else { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4088 |         if ((isScoConnected && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4089 |              ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) || | 
 | 4090 |               (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) || | 
 | 4091 |              ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || | 
 | 4092 |               (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4093 |  | 
 | 4094 |             mpClientInterface->suspendOutput(a2dpOutput); | 
 | 4095 |             mA2dpSuspended = true; | 
 | 4096 |         } | 
 | 4097 |     } | 
 | 4098 | } | 
 | 4099 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4100 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
 | 4101 |                                                        bool fromCache) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4102 | { | 
 | 4103 |     audio_devices_t device = AUDIO_DEVICE_NONE; | 
 | 4104 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4105 |     ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); | 
 | 4106 |     if (index >= 0) { | 
 | 4107 |         sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
 | 4108 |         if (patchDesc->mUid != mUidCached) { | 
 | 4109 |             ALOGV("getNewOutputDevice() device %08x forced by patch %d", | 
 | 4110 |                   outputDesc->device(), outputDesc->mPatchHandle); | 
 | 4111 |             return outputDesc->device(); | 
 | 4112 |         } | 
 | 4113 |     } | 
 | 4114 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4115 |     // 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] | 4116 |     // 1: the strategy enforced audible is active and enforced on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4117 |     //      use device for strategy enforced audible | 
 | 4118 |     // 2: we are in call or the strategy phone is active on the output: | 
 | 4119 |     //      use device for strategy phone | 
| Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4120 |     // 3: the strategy for enforced audible is active but not enforced on the output: | 
 | 4121 |     //      use the device for strategy enforced audible | 
| Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4122 |     // 4: the strategy accessibility is active on the output: | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4123 |     //      use device for strategy accessibility | 
| Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4124 |     // 5: the strategy sonification is active on the output: | 
 | 4125 |     //      use device for strategy sonification | 
 | 4126 |     // 6: the strategy "respectful" sonification is active on the output: | 
 | 4127 |     //      use device for strategy "respectful" sonification | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4128 |     // 7: the strategy media is active on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4129 |     //      use device for strategy media | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4130 |     // 8: the strategy DTMF is active on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4131 |     //      use device for strategy DTMF | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4132 |     // 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] | 4133 |     //      use device for strategy t-t-s | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4134 |     if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4135 |         mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4136 |         device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); | 
 | 4137 |     } else if (isInCall() || | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4138 |                     isStrategyActive(outputDesc, STRATEGY_PHONE)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4139 |         device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4140 |     } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { | 
| Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4141 |         device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); | 
| Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4142 |     } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { | 
 | 4143 |         device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4144 |     } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4145 |         device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4146 |     } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4147 |         device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4148 |     } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4149 |         device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4150 |     } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4151 |         device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4152 |     } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4153 |         device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4154 |     } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4155 |         device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4156 |     } | 
 | 4157 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4158 |     ALOGV("getNewOutputDevice() selected device %x", device); | 
 | 4159 |     return device; | 
 | 4160 | } | 
 | 4161 |  | 
 | 4162 | audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input) | 
 | 4163 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4164 |     sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4165 |  | 
 | 4166 |     ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); | 
 | 4167 |     if (index >= 0) { | 
 | 4168 |         sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
 | 4169 |         if (patchDesc->mUid != mUidCached) { | 
 | 4170 |             ALOGV("getNewInputDevice() device %08x forced by patch %d", | 
 | 4171 |                   inputDesc->mDevice, inputDesc->mPatchHandle); | 
 | 4172 |             return inputDesc->mDevice; | 
 | 4173 |         } | 
 | 4174 |     } | 
 | 4175 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 4176 |     audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->inputSource()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4177 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4178 |     return device; | 
 | 4179 | } | 
 | 4180 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4181 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4182 |     return (uint32_t)getStrategy(stream); | 
 | 4183 | } | 
 | 4184 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4185 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4186 |     // By checking the range of stream before calling getStrategy, we avoid | 
 | 4187 |     // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE | 
 | 4188 |     // 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] | 4189 |     if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4190 |         return AUDIO_DEVICE_NONE; | 
 | 4191 |     } | 
 | 4192 |     audio_devices_t devices; | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 4193 |     routing_strategy strategy = getStrategy(stream); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4194 |     devices = getDeviceForStrategy(strategy, true /*fromCache*/); | 
 | 4195 |     SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs); | 
 | 4196 |     for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4197 |         sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4198 |         if (isStrategyActive(outputDesc, strategy)) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4199 |             devices = outputDesc->device(); | 
 | 4200 |             break; | 
 | 4201 |         } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4202 |     } | 
| Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4203 |  | 
 | 4204 |     /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it | 
 | 4205 |       and doesn't really need to.*/ | 
 | 4206 |     if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { | 
 | 4207 |         devices |= AUDIO_DEVICE_OUT_SPEAKER; | 
 | 4208 |         devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; | 
 | 4209 |     } | 
 | 4210 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4211 |     return devices; | 
 | 4212 | } | 
 | 4213 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4214 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const | 
 | 4215 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4216 |     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] | 4217 |     return mEngine->getStrategyForStream(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4218 | } | 
 | 4219 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4220 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { | 
 | 4221 |     // flags to strategy mapping | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4222 |     if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { | 
 | 4223 |         return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; | 
 | 4224 |     } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4225 |     if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { | 
 | 4226 |         return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; | 
 | 4227 |     } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4228 |     // usage to strategy mapping | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4229 |     return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4230 | } | 
 | 4231 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4232 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4233 |     switch(stream) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4234 |     case AUDIO_STREAM_MUSIC: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4235 |         checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); | 
 | 4236 |         updateDevicesAndOutputs(); | 
 | 4237 |         break; | 
 | 4238 |     default: | 
 | 4239 |         break; | 
 | 4240 |     } | 
 | 4241 | } | 
 | 4242 |  | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4243 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4244 |  | 
 | 4245 |     // skip beacon mute management if a dedicated TTS output is available | 
 | 4246 |     if (mTtsOutputAvailable) { | 
 | 4247 |         return 0; | 
 | 4248 |     } | 
 | 4249 |  | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4250 |     switch(event) { | 
 | 4251 |     case STARTING_OUTPUT: | 
 | 4252 |         mBeaconMuteRefCount++; | 
 | 4253 |         break; | 
 | 4254 |     case STOPPING_OUTPUT: | 
 | 4255 |         if (mBeaconMuteRefCount > 0) { | 
 | 4256 |             mBeaconMuteRefCount--; | 
 | 4257 |         } | 
 | 4258 |         break; | 
 | 4259 |     case STARTING_BEACON: | 
 | 4260 |         mBeaconPlayingRefCount++; | 
 | 4261 |         break; | 
 | 4262 |     case STOPPING_BEACON: | 
 | 4263 |         if (mBeaconPlayingRefCount > 0) { | 
 | 4264 |             mBeaconPlayingRefCount--; | 
 | 4265 |         } | 
 | 4266 |         break; | 
 | 4267 |     } | 
 | 4268 |  | 
 | 4269 |     if (mBeaconMuteRefCount > 0) { | 
 | 4270 |         // any playback causes beacon to be muted | 
 | 4271 |         return setBeaconMute(true); | 
 | 4272 |     } else { | 
 | 4273 |         // no other playback: unmute when beacon starts playing, mute when it stops | 
 | 4274 |         return setBeaconMute(mBeaconPlayingRefCount == 0); | 
 | 4275 |     } | 
 | 4276 | } | 
 | 4277 |  | 
 | 4278 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { | 
 | 4279 |     ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", | 
 | 4280 |             mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); | 
 | 4281 |     // keep track of muted state to avoid repeating mute/unmute operations | 
 | 4282 |     if (mBeaconMuted != mute) { | 
 | 4283 |         // mute/unmute AUDIO_STREAM_TTS on all outputs | 
 | 4284 |         ALOGV("\t muting %d", mute); | 
 | 4285 |         uint32_t maxLatency = 0; | 
 | 4286 |         for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4287 |             sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4288 |             setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4289 |                     desc, | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4290 |                     0 /*delay*/, AUDIO_DEVICE_NONE); | 
 | 4291 |             const uint32_t latency = desc->latency() * 2; | 
 | 4292 |             if (latency > maxLatency) { | 
 | 4293 |                 maxLatency = latency; | 
 | 4294 |             } | 
 | 4295 |         } | 
 | 4296 |         mBeaconMuted = mute; | 
 | 4297 |         return maxLatency; | 
 | 4298 |     } | 
 | 4299 |     return 0; | 
 | 4300 | } | 
 | 4301 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4302 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4303 |                                                          bool fromCache) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4304 | { | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4305 |     // Routing | 
 | 4306 |     // see if we have an explicit route | 
 | 4307 |     // scan the whole RouteMap, for each entry, convert the stream type to a strategy | 
 | 4308 |     // (getStrategy(stream)). | 
 | 4309 |     // if the strategy from the stream type in the RouteMap is the same as the argument above, | 
 | 4310 |     // and activity count is non-zero | 
 | 4311 |     // the device = the device from the descriptor in the RouteMap, and exit. | 
 | 4312 |     for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { | 
 | 4313 |         sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); | 
 | 4314 |         routing_strategy strat = getStrategy(route->mStreamType); | 
| Eric Laurent | 093a20c | 2015-06-11 12:33:29 -0700 | [diff] [blame] | 4315 |         // Special case for accessibility strategy which must follow any strategy it is | 
 | 4316 |         // currently remapped to | 
 | 4317 |         bool strategyMatch = (strat == strategy) || | 
 | 4318 |                              ((strategy == STRATEGY_ACCESSIBILITY) && | 
 | 4319 |                               ((mEngine->getStrategyForUsage( | 
 | 4320 |                                       AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY) == strat) || | 
 | 4321 |                                (strat == STRATEGY_MEDIA))); | 
 | 4322 |         if (strategyMatch && route->isActive()) { | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4323 |             return route->mDeviceDescriptor->type(); | 
 | 4324 |         } | 
 | 4325 |     } | 
 | 4326 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4327 |     if (fromCache) { | 
 | 4328 |         ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", | 
 | 4329 |               strategy, mDeviceForStrategy[strategy]); | 
 | 4330 |         return mDeviceForStrategy[strategy]; | 
 | 4331 |     } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4332 |     return mEngine->getDeviceForStrategy(strategy); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4333 | } | 
 | 4334 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4335 | void AudioPolicyManager::updateDevicesAndOutputs() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4336 | { | 
 | 4337 |     for (int i = 0; i < NUM_STRATEGIES; i++) { | 
 | 4338 |         mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); | 
 | 4339 |     } | 
 | 4340 |     mPreviousOutputs = mOutputs; | 
 | 4341 | } | 
 | 4342 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4343 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4344 |                                                        audio_devices_t prevDevice, | 
 | 4345 |                                                        uint32_t delayMs) | 
 | 4346 | { | 
 | 4347 |     // mute/unmute strategies using an incompatible device combination | 
 | 4348 |     // if muting, wait for the audio in pcm buffer to be drained before proceeding | 
 | 4349 |     // if unmuting, unmute only after the specified delay | 
 | 4350 |     if (outputDesc->isDuplicated()) { | 
 | 4351 |         return 0; | 
 | 4352 |     } | 
 | 4353 |  | 
 | 4354 |     uint32_t muteWaitMs = 0; | 
 | 4355 |     audio_devices_t device = outputDesc->device(); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4356 |     bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4357 |  | 
 | 4358 |     for (size_t i = 0; i < NUM_STRATEGIES; i++) { | 
 | 4359 |         audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4360 |         curDevice = curDevice & outputDesc->supportedDevices(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4361 |         bool mute = shouldMute && (curDevice & device) && (curDevice != device); | 
 | 4362 |         bool doMute = false; | 
 | 4363 |  | 
 | 4364 |         if (mute && !outputDesc->mStrategyMutedByDevice[i]) { | 
 | 4365 |             doMute = true; | 
 | 4366 |             outputDesc->mStrategyMutedByDevice[i] = true; | 
 | 4367 |         } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ | 
 | 4368 |             doMute = true; | 
 | 4369 |             outputDesc->mStrategyMutedByDevice[i] = false; | 
 | 4370 |         } | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4371 |         if (doMute) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4372 |             for (size_t j = 0; j < mOutputs.size(); j++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4373 |                 sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4374 |                 // skip output if it does not share any device with current output | 
 | 4375 |                 if ((desc->supportedDevices() & outputDesc->supportedDevices()) | 
 | 4376 |                         == AUDIO_DEVICE_NONE) { | 
 | 4377 |                     continue; | 
 | 4378 |                 } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4379 |                 ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)", | 
 | 4380 |                       mute ? "muting" : "unmuting", i, curDevice); | 
 | 4381 |                 setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4382 |                 if (isStrategyActive(desc, (routing_strategy)i)) { | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4383 |                     if (mute) { | 
 | 4384 |                         // FIXME: should not need to double latency if volume could be applied | 
 | 4385 |                         // immediately by the audioflinger mixer. We must account for the delay | 
 | 4386 |                         // between now and the next time the audioflinger thread for this output | 
 | 4387 |                         // will process a buffer (which corresponds to one buffer size, | 
 | 4388 |                         // usually 1/2 or 1/4 of the latency). | 
 | 4389 |                         if (muteWaitMs < desc->latency() * 2) { | 
 | 4390 |                             muteWaitMs = desc->latency() * 2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4391 |                         } | 
 | 4392 |                     } | 
 | 4393 |                 } | 
 | 4394 |             } | 
 | 4395 |         } | 
 | 4396 |     } | 
 | 4397 |  | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4398 |     // temporary mute output if device selection changes to avoid volume bursts due to | 
 | 4399 |     // different per device volumes | 
 | 4400 |     if (outputDesc->isActive() && (device != prevDevice)) { | 
 | 4401 |         if (muteWaitMs < outputDesc->latency() * 2) { | 
 | 4402 |             muteWaitMs = outputDesc->latency() * 2; | 
 | 4403 |         } | 
 | 4404 |         for (size_t i = 0; i < NUM_STRATEGIES; i++) { | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4405 |             if (isStrategyActive(outputDesc, (routing_strategy)i)) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4406 |                 setStrategyMute((routing_strategy)i, true, outputDesc); | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4407 |                 // do tempMute unmute after twice the mute wait time | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4408 |                 setStrategyMute((routing_strategy)i, false, outputDesc, | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4409 |                                 muteWaitMs *2, device); | 
 | 4410 |             } | 
 | 4411 |         } | 
 | 4412 |     } | 
 | 4413 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4414 |     // wait for the PCM output buffers to empty before proceeding with the rest of the command | 
 | 4415 |     if (muteWaitMs > delayMs) { | 
 | 4416 |         muteWaitMs -= delayMs; | 
 | 4417 |         usleep(muteWaitMs * 1000); | 
 | 4418 |         return muteWaitMs; | 
 | 4419 |     } | 
 | 4420 |     return 0; | 
 | 4421 | } | 
 | 4422 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4423 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4424 |                                              audio_devices_t device, | 
 | 4425 |                                              bool force, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4426 |                                              int delayMs, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4427 |                                              audio_patch_handle_t *patchHandle, | 
 | 4428 |                                              const char* address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4429 | { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4430 |     ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4431 |     AudioParameter param; | 
 | 4432 |     uint32_t muteWaitMs; | 
 | 4433 |  | 
 | 4434 |     if (outputDesc->isDuplicated()) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4435 |         muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs); | 
 | 4436 |         muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4437 |         return muteWaitMs; | 
 | 4438 |     } | 
 | 4439 |     // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current | 
 | 4440 |     // output profile | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4441 |     if ((device != AUDIO_DEVICE_NONE) && | 
 | 4442 |             ((device & outputDesc->supportedDevices()) == 0)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4443 |         return 0; | 
 | 4444 |     } | 
 | 4445 |  | 
 | 4446 |     // filter devices according to output selected | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4447 |     device = (audio_devices_t)(device & outputDesc->supportedDevices()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4448 |  | 
 | 4449 |     audio_devices_t prevDevice = outputDesc->mDevice; | 
 | 4450 |  | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4451 |     ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4452 |  | 
 | 4453 |     if (device != AUDIO_DEVICE_NONE) { | 
 | 4454 |         outputDesc->mDevice = device; | 
 | 4455 |     } | 
 | 4456 |     muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); | 
 | 4457 |  | 
 | 4458 |     // Do not change the routing if: | 
| Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4459 |     //      the requested device is AUDIO_DEVICE_NONE | 
 | 4460 |     //      OR the requested device is the same as current device | 
 | 4461 |     //  AND force is not specified | 
 | 4462 |     //  AND the output is connected by a valid audio patch. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4463 |     // Doing this check here allows the caller to call setOutputDevice() without conditions | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4464 |     if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && | 
 | 4465 |         !force && | 
 | 4466 |         outputDesc->mPatchHandle != 0) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4467 |         ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4468 |         return muteWaitMs; | 
 | 4469 |     } | 
 | 4470 |  | 
 | 4471 |     ALOGV("setOutputDevice() changing device"); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4472 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4473 |     // do the routing | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4474 |     if (device == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4475 |         resetOutputDevice(outputDesc, delayMs, NULL); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4476 |     } else { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4477 |         DeviceVector deviceList = (address == NULL) ? | 
 | 4478 |                 mAvailableOutputDevices.getDevicesFromType(device) | 
 | 4479 |                 : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4480 |         if (!deviceList.isEmpty()) { | 
 | 4481 |             struct audio_patch patch; | 
 | 4482 |             outputDesc->toAudioPortConfig(&patch.sources[0]); | 
 | 4483 |             patch.num_sources = 1; | 
 | 4484 |             patch.num_sinks = 0; | 
 | 4485 |             for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { | 
 | 4486 |                 deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4487 |                 patch.num_sinks++; | 
 | 4488 |             } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4489 |             ssize_t index; | 
 | 4490 |             if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { | 
 | 4491 |                 index = mAudioPatches.indexOfKey(*patchHandle); | 
 | 4492 |             } else { | 
 | 4493 |                 index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); | 
 | 4494 |             } | 
 | 4495 |             sp< AudioPatch> patchDesc; | 
 | 4496 |             audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
 | 4497 |             if (index >= 0) { | 
 | 4498 |                 patchDesc = mAudioPatches.valueAt(index); | 
 | 4499 |                 afPatchHandle = patchDesc->mAfPatchHandle; | 
 | 4500 |             } | 
 | 4501 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4502 |             status_t status = mpClientInterface->createAudioPatch(&patch, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4503 |                                                                    &afPatchHandle, | 
 | 4504 |                                                                    delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4505 |             ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" | 
 | 4506 |                     "num_sources %d num_sinks %d", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4507 |                                        status, afPatchHandle, patch.num_sources, patch.num_sinks); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4508 |             if (status == NO_ERROR) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4509 |                 if (index < 0) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4510 |                     patchDesc = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4511 |                     addAudioPatch(patchDesc->mHandle, patchDesc); | 
 | 4512 |                 } else { | 
 | 4513 |                     patchDesc->mPatch = patch; | 
 | 4514 |                 } | 
 | 4515 |                 patchDesc->mAfPatchHandle = afPatchHandle; | 
 | 4516 |                 patchDesc->mUid = mUidCached; | 
 | 4517 |                 if (patchHandle) { | 
 | 4518 |                     *patchHandle = patchDesc->mHandle; | 
 | 4519 |                 } | 
 | 4520 |                 outputDesc->mPatchHandle = patchDesc->mHandle; | 
 | 4521 |                 nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4522 |                 mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4523 |             } | 
 | 4524 |         } | 
| bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4525 |  | 
 | 4526 |         // inform all input as well | 
 | 4527 |         for (size_t i = 0; i < mInputs.size(); i++) { | 
 | 4528 |             const sp<AudioInputDescriptor>  inputDescriptor = mInputs.valueAt(i); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4529 |             if (!is_virtual_input_device(inputDescriptor->mDevice)) { | 
| bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4530 |                 AudioParameter inputCmd = AudioParameter(); | 
 | 4531 |                 ALOGV("%s: inform input %d of device:%d", __func__, | 
 | 4532 |                       inputDescriptor->mIoHandle, device); | 
 | 4533 |                 inputCmd.addInt(String8(AudioParameter::keyRouting),device); | 
 | 4534 |                 mpClientInterface->setParameters(inputDescriptor->mIoHandle, | 
 | 4535 |                                                  inputCmd.toString(), | 
 | 4536 |                                                  delayMs); | 
 | 4537 |             } | 
 | 4538 |         } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4539 |     } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4540 |  | 
 | 4541 |     // update stream volumes according to new device | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4542 |     applyStreamVolumes(outputDesc, device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4543 |  | 
 | 4544 |     return muteWaitMs; | 
 | 4545 | } | 
 | 4546 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4547 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4548 |                                                int delayMs, | 
 | 4549 |                                                audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4550 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4551 |     ssize_t index; | 
 | 4552 |     if (patchHandle) { | 
 | 4553 |         index = mAudioPatches.indexOfKey(*patchHandle); | 
 | 4554 |     } else { | 
 | 4555 |         index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); | 
 | 4556 |     } | 
 | 4557 |     if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4558 |         return INVALID_OPERATION; | 
 | 4559 |     } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4560 |     sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
 | 4561 |     status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4562 |     ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); | 
 | 4563 |     outputDesc->mPatchHandle = 0; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4564 |     removeAudioPatch(patchDesc->mHandle); | 
 | 4565 |     nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4566 |     mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4567 |     return status; | 
 | 4568 | } | 
 | 4569 |  | 
 | 4570 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, | 
 | 4571 |                                             audio_devices_t device, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4572 |                                             bool force, | 
 | 4573 |                                             audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4574 | { | 
 | 4575 |     status_t status = NO_ERROR; | 
 | 4576 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4577 |     sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4578 |     if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { | 
 | 4579 |         inputDesc->mDevice = device; | 
 | 4580 |  | 
 | 4581 |         DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); | 
 | 4582 |         if (!deviceList.isEmpty()) { | 
 | 4583 |             struct audio_patch patch; | 
 | 4584 |             inputDesc->toAudioPortConfig(&patch.sinks[0]); | 
| Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4585 |             // AUDIO_SOURCE_HOTWORD is for internal use only: | 
 | 4586 |             // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4587 |             if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 4588 |                     !inputDesc->isSoundTrigger()) { | 
| Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4589 |                 patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; | 
 | 4590 |             } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4591 |             patch.num_sinks = 1; | 
 | 4592 |             //only one input device for now | 
 | 4593 |             deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4594 |             patch.num_sources = 1; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4595 |             ssize_t index; | 
 | 4596 |             if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { | 
 | 4597 |                 index = mAudioPatches.indexOfKey(*patchHandle); | 
 | 4598 |             } else { | 
 | 4599 |                 index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); | 
 | 4600 |             } | 
 | 4601 |             sp< AudioPatch> patchDesc; | 
 | 4602 |             audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
 | 4603 |             if (index >= 0) { | 
 | 4604 |                 patchDesc = mAudioPatches.valueAt(index); | 
 | 4605 |                 afPatchHandle = patchDesc->mAfPatchHandle; | 
 | 4606 |             } | 
 | 4607 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4608 |             status_t status = mpClientInterface->createAudioPatch(&patch, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4609 |                                                                   &afPatchHandle, | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4610 |                                                                   0); | 
 | 4611 |             ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4612 |                                                                           status, afPatchHandle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4613 |             if (status == NO_ERROR) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4614 |                 if (index < 0) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4615 |                     patchDesc = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4616 |                     addAudioPatch(patchDesc->mHandle, patchDesc); | 
 | 4617 |                 } else { | 
 | 4618 |                     patchDesc->mPatch = patch; | 
 | 4619 |                 } | 
 | 4620 |                 patchDesc->mAfPatchHandle = afPatchHandle; | 
 | 4621 |                 patchDesc->mUid = mUidCached; | 
 | 4622 |                 if (patchHandle) { | 
 | 4623 |                     *patchHandle = patchDesc->mHandle; | 
 | 4624 |                 } | 
 | 4625 |                 inputDesc->mPatchHandle = patchDesc->mHandle; | 
 | 4626 |                 nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4627 |                 mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4628 |             } | 
 | 4629 |         } | 
 | 4630 |     } | 
 | 4631 |     return status; | 
 | 4632 | } | 
 | 4633 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4634 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, | 
 | 4635 |                                               audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4636 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4637 |     sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4638 |     ssize_t index; | 
 | 4639 |     if (patchHandle) { | 
 | 4640 |         index = mAudioPatches.indexOfKey(*patchHandle); | 
 | 4641 |     } else { | 
 | 4642 |         index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); | 
 | 4643 |     } | 
 | 4644 |     if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4645 |         return INVALID_OPERATION; | 
 | 4646 |     } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4647 |     sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
 | 4648 |     status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4649 |     ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); | 
 | 4650 |     inputDesc->mPatchHandle = 0; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4651 |     removeAudioPatch(patchDesc->mHandle); | 
 | 4652 |     nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4653 |     mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4654 |     return status; | 
 | 4655 | } | 
 | 4656 |  | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 4657 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4658 |                                                   String8 address, | 
 | 4659 |                                                   uint32_t& samplingRate, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4660 |                                                   audio_format_t& format, | 
 | 4661 |                                                   audio_channel_mask_t& channelMask, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4662 |                                                   audio_input_flags_t flags) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4663 | { | 
 | 4664 |     // Choose an input profile based on the requested capture parameters: select the first available | 
 | 4665 |     // profile supporting all requested parameters. | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4666 |     // | 
 | 4667 |     // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return | 
 | 4668 |     // the best matching profile, not the first one. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4669 |  | 
 | 4670 |     for (size_t i = 0; i < mHwModules.size(); i++) | 
 | 4671 |     { | 
 | 4672 |         if (mHwModules[i]->mHandle == 0) { | 
 | 4673 |             continue; | 
 | 4674 |         } | 
 | 4675 |         for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) | 
 | 4676 |         { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4677 |             sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4678 |             // profile->log(); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4679 |             if (profile->isCompatibleProfile(device, address, samplingRate, | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4680 |                                              &samplingRate /*updatedSamplingRate*/, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4681 |                                              format, | 
 | 4682 |                                              &format /*updatedFormat*/, | 
 | 4683 |                                              channelMask, | 
 | 4684 |                                              &channelMask /*updatedChannelMask*/, | 
 | 4685 |                                              (audio_output_flags_t) flags)) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4686 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4687 |                 return profile; | 
 | 4688 |             } | 
 | 4689 |         } | 
 | 4690 |     } | 
 | 4691 |     return NULL; | 
 | 4692 | } | 
 | 4693 |  | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4694 |  | 
 | 4695 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4696 |                                                                   AudioMix **policyMix) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4697 | { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4698 |     audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; | 
 | 4699 |     audio_devices_t selectedDeviceFromMix = | 
 | 4700 |            mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4701 |  | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4702 |     if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { | 
 | 4703 |         return selectedDeviceFromMix; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4704 |     } | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4705 |     return getDeviceForInputSource(inputSource); | 
 | 4706 | } | 
 | 4707 |  | 
 | 4708 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) | 
 | 4709 | { | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4710 |     for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { | 
 | 4711 |          sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4712 |          if (inputSource == route->mSource && route->isActive()) { | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4713 |              return route->mDeviceDescriptor->type(); | 
 | 4714 |          } | 
 | 4715 |      } | 
 | 4716 |  | 
 | 4717 |      return mEngine->getDeviceForInputSource(inputSource); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4718 | } | 
 | 4719 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4720 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4721 |                                             int index, | 
 | 4722 |                                             audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4723 | { | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4724 |     float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4725 |  | 
 | 4726 |     // if a headset is connected, apply the following rules to ring tones and notifications | 
 | 4727 |     // to avoid sound level bursts in user's ears: | 
 | 4728 |     // - always attenuate ring tones and notifications volume by 6dB | 
 | 4729 |     // - if music is playing, always limit the volume to current music volume, | 
 | 4730 |     // with a minimum threshold at -36dB so that notification is always perceived. | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4731 |     const routing_strategy stream_strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4732 |     if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | | 
 | 4733 |             AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | | 
 | 4734 |             AUDIO_DEVICE_OUT_WIRED_HEADSET | | 
 | 4735 |             AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && | 
 | 4736 |         ((stream_strategy == STRATEGY_SONIFICATION) | 
 | 4737 |                 || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4738 |                 || (stream == AUDIO_STREAM_SYSTEM) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4739 |                 || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4740 |                     (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && | 
 | 4741 |             mStreams.canBeMuted(stream)) { | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4742 |         volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4743 |         // when the phone is ringing we must consider that music could have been paused just before | 
 | 4744 |         // by the music application and behave as if music was active if the last music track was | 
 | 4745 |         // just stopped | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4746 |         if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4747 |                 mLimitRingtoneVolume) { | 
 | 4748 |             audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4749 |             float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, | 
 | 4750 |                                  mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice), | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4751 |                                musicDevice); | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4752 |             float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? | 
 | 4753 |                     musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; | 
 | 4754 |             if (volumeDb > minVolDB) { | 
 | 4755 |                 volumeDb = minVolDB; | 
 | 4756 |                 ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4757 |             } | 
 | 4758 |         } | 
 | 4759 |     } | 
 | 4760 |  | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4761 |     return volumeDb; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4762 | } | 
 | 4763 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4764 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4765 |                                                    int index, | 
 | 4766 |                                                    const sp<AudioOutputDescriptor>& outputDesc, | 
 | 4767 |                                                    audio_devices_t device, | 
 | 4768 |                                                    int delayMs, | 
 | 4769 |                                                    bool force) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4770 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4771 |     // do not change actual stream volume if the stream is muted | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4772 |     if (outputDesc->mMuteCount[stream] != 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4773 |         ALOGVV("checkAndSetVolume() stream %d muted count %d", | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4774 |               stream, outputDesc->mMuteCount[stream]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4775 |         return NO_ERROR; | 
 | 4776 |     } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4777 |     audio_policy_forced_cfg_t forceUseForComm = | 
 | 4778 |             mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4779 |     // 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] | 4780 |     if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || | 
 | 4781 |         (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4782 |         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] | 4783 |              stream, forceUseForComm); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4784 |         return INVALID_OPERATION; | 
 | 4785 |     } | 
 | 4786 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4787 |     if (device == AUDIO_DEVICE_NONE) { | 
 | 4788 |         device = outputDesc->device(); | 
 | 4789 |     } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4790 |  | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4791 |     float volumeDb = computeVolume(stream, index, device); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4792 |     if (outputDesc->isFixedVolume(device)) { | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4793 |         volumeDb = 0.0f; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4794 |     } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4795 |  | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4796 |     outputDesc->setVolume(volumeDb, stream, device, delayMs, force); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4797 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4798 |     if (stream == AUDIO_STREAM_VOICE_CALL || | 
 | 4799 |         stream == AUDIO_STREAM_BLUETOOTH_SCO) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4800 |         float voiceVolume; | 
 | 4801 |         // 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] | 4802 |         if (stream == AUDIO_STREAM_VOICE_CALL) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4803 |             voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4804 |         } else { | 
 | 4805 |             voiceVolume = 1.0; | 
 | 4806 |         } | 
 | 4807 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4808 |         if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4809 |             mpClientInterface->setVoiceVolume(voiceVolume, delayMs); | 
 | 4810 |             mLastVoiceVolume = voiceVolume; | 
 | 4811 |         } | 
 | 4812 |     } | 
 | 4813 |  | 
 | 4814 |     return NO_ERROR; | 
 | 4815 | } | 
 | 4816 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4817 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, | 
 | 4818 |                                                 audio_devices_t device, | 
 | 4819 |                                                 int delayMs, | 
 | 4820 |                                                 bool force) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4821 | { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4822 |     ALOGVV("applyStreamVolumes() for device %08x", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4823 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4824 |     for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4825 |         if (stream == AUDIO_STREAM_PATCH) { | 
 | 4826 |             continue; | 
 | 4827 |         } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4828 |         checkAndSetVolume((audio_stream_type_t)stream, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4829 |                           mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device), | 
 | 4830 |                           outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4831 |                           device, | 
 | 4832 |                           delayMs, | 
 | 4833 |                           force); | 
 | 4834 |     } | 
 | 4835 | } | 
 | 4836 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4837 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4838 |                                              bool on, | 
 | 4839 |                                              const sp<AudioOutputDescriptor>& outputDesc, | 
 | 4840 |                                              int delayMs, | 
 | 4841 |                                              audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4842 | { | 
| Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 4843 |     ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", | 
 | 4844 |            strategy, on, outputDesc->getId()); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4845 |     for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4846 |         if (stream == AUDIO_STREAM_PATCH) { | 
 | 4847 |             continue; | 
 | 4848 |         } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4849 |         if (getStrategy((audio_stream_type_t)stream) == strategy) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4850 |             setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4851 |         } | 
 | 4852 |     } | 
 | 4853 | } | 
 | 4854 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4855 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4856 |                                            bool on, | 
 | 4857 |                                            const sp<AudioOutputDescriptor>& outputDesc, | 
 | 4858 |                                            int delayMs, | 
 | 4859 |                                            audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4860 | { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4861 |     const StreamDescriptor& streamDesc = mStreams.valueFor(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4862 |     if (device == AUDIO_DEVICE_NONE) { | 
 | 4863 |         device = outputDesc->device(); | 
 | 4864 |     } | 
 | 4865 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4866 |     ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", | 
 | 4867 |           stream, on, outputDesc->mMuteCount[stream], device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4868 |  | 
 | 4869 |     if (on) { | 
 | 4870 |         if (outputDesc->mMuteCount[stream] == 0) { | 
| François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 4871 |             if (streamDesc.canBeMuted() && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4872 |                     ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4873 |                      (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4874 |                 checkAndSetVolume(stream, 0, outputDesc, device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4875 |             } | 
 | 4876 |         } | 
 | 4877 |         // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored | 
 | 4878 |         outputDesc->mMuteCount[stream]++; | 
 | 4879 |     } else { | 
 | 4880 |         if (outputDesc->mMuteCount[stream] == 0) { | 
 | 4881 |             ALOGV("setStreamMute() unmuting non muted stream!"); | 
 | 4882 |             return; | 
 | 4883 |         } | 
 | 4884 |         if (--outputDesc->mMuteCount[stream] == 0) { | 
 | 4885 |             checkAndSetVolume(stream, | 
 | 4886 |                               streamDesc.getVolumeIndex(device), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4887 |                               outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4888 |                               device, | 
 | 4889 |                               delayMs); | 
 | 4890 |         } | 
 | 4891 |     } | 
 | 4892 | } | 
 | 4893 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4894 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4895 |                                                       bool starting, bool stateChange) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4896 | { | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4897 |     if(!hasPrimaryOutput()) { | 
 | 4898 |         return; | 
 | 4899 |     } | 
 | 4900 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4901 |     // if the stream pertains to sonification strategy and we are in call we must | 
 | 4902 |     // mute the stream if it is low visibility. If it is high visibility, we must play a tone | 
 | 4903 |     // in the device used for phone strategy and play the tone if the selected device does not | 
 | 4904 |     // interfere with the device used for phone strategy | 
 | 4905 |     // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as | 
 | 4906 |     // many times as there are active tracks on the output | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4907 |     const routing_strategy stream_strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4908 |     if ((stream_strategy == STRATEGY_SONIFICATION) || | 
 | 4909 |             ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4910 |         sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4911 |         ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", | 
 | 4912 |                 stream, starting, outputDesc->mDevice, stateChange); | 
 | 4913 |         if (outputDesc->mRefCount[stream]) { | 
 | 4914 |             int muteCount = 1; | 
 | 4915 |             if (stateChange) { | 
 | 4916 |                 muteCount = outputDesc->mRefCount[stream]; | 
 | 4917 |             } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4918 |             if (audio_is_low_visibility(stream)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4919 |                 ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); | 
 | 4920 |                 for (int i = 0; i < muteCount; i++) { | 
 | 4921 |                     setStreamMute(stream, starting, mPrimaryOutput); | 
 | 4922 |                 } | 
 | 4923 |             } else { | 
 | 4924 |                 ALOGV("handleIncallSonification() high visibility"); | 
 | 4925 |                 if (outputDesc->device() & | 
 | 4926 |                         getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { | 
 | 4927 |                     ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); | 
 | 4928 |                     for (int i = 0; i < muteCount; i++) { | 
 | 4929 |                         setStreamMute(stream, starting, mPrimaryOutput); | 
 | 4930 |                     } | 
 | 4931 |                 } | 
 | 4932 |                 if (starting) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4933 |                     mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, | 
 | 4934 |                                                  AUDIO_STREAM_VOICE_CALL); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4935 |                 } else { | 
 | 4936 |                     mpClientInterface->stopTone(); | 
 | 4937 |                 } | 
 | 4938 |             } | 
 | 4939 |         } | 
 | 4940 |     } | 
 | 4941 | } | 
 | 4942 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4943 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) | 
 | 4944 | { | 
 | 4945 |     // flags to stream type mapping | 
 | 4946 |     if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { | 
 | 4947 |         return AUDIO_STREAM_ENFORCED_AUDIBLE; | 
 | 4948 |     } | 
 | 4949 |     if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { | 
 | 4950 |         return AUDIO_STREAM_BLUETOOTH_SCO; | 
 | 4951 |     } | 
| Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 4952 |     if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { | 
 | 4953 |         return AUDIO_STREAM_TTS; | 
 | 4954 |     } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4955 |  | 
 | 4956 |     // usage to stream type mapping | 
 | 4957 |     switch (attr->usage) { | 
 | 4958 |     case AUDIO_USAGE_MEDIA: | 
 | 4959 |     case AUDIO_USAGE_GAME: | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4960 |     case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
 | 4961 |         return AUDIO_STREAM_MUSIC; | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4962 |     case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 4963 |         if (isStreamActive(AUDIO_STREAM_ALARM)) { | 
 | 4964 |             return AUDIO_STREAM_ALARM; | 
 | 4965 |         } | 
 | 4966 |         if (isStreamActive(AUDIO_STREAM_RING)) { | 
 | 4967 |             return AUDIO_STREAM_RING; | 
 | 4968 |         } | 
 | 4969 |         if (isInCall()) { | 
 | 4970 |             return AUDIO_STREAM_VOICE_CALL; | 
 | 4971 |         } | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4972 |         return AUDIO_STREAM_ACCESSIBILITY; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4973 |     case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
 | 4974 |         return AUDIO_STREAM_SYSTEM; | 
 | 4975 |     case AUDIO_USAGE_VOICE_COMMUNICATION: | 
 | 4976 |         return AUDIO_STREAM_VOICE_CALL; | 
 | 4977 |  | 
 | 4978 |     case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
 | 4979 |         return AUDIO_STREAM_DTMF; | 
 | 4980 |  | 
 | 4981 |     case AUDIO_USAGE_ALARM: | 
 | 4982 |         return AUDIO_STREAM_ALARM; | 
 | 4983 |     case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
 | 4984 |         return AUDIO_STREAM_RING; | 
 | 4985 |  | 
 | 4986 |     case AUDIO_USAGE_NOTIFICATION: | 
 | 4987 |     case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
 | 4988 |     case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
 | 4989 |     case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
 | 4990 |     case AUDIO_USAGE_NOTIFICATION_EVENT: | 
 | 4991 |         return AUDIO_STREAM_NOTIFICATION; | 
 | 4992 |  | 
 | 4993 |     case AUDIO_USAGE_UNKNOWN: | 
 | 4994 |     default: | 
 | 4995 |         return AUDIO_STREAM_MUSIC; | 
 | 4996 |     } | 
 | 4997 | } | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 4998 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4999 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) | 
 | 5000 | { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5001 |     // has flags that map to a strategy? | 
 | 5002 |     if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { | 
 | 5003 |         return true; | 
 | 5004 |     } | 
 | 5005 |  | 
 | 5006 |     // has known usage? | 
 | 5007 |     switch (paa->usage) { | 
 | 5008 |     case AUDIO_USAGE_UNKNOWN: | 
 | 5009 |     case AUDIO_USAGE_MEDIA: | 
 | 5010 |     case AUDIO_USAGE_VOICE_COMMUNICATION: | 
 | 5011 |     case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
 | 5012 |     case AUDIO_USAGE_ALARM: | 
 | 5013 |     case AUDIO_USAGE_NOTIFICATION: | 
 | 5014 |     case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
 | 5015 |     case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
 | 5016 |     case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
 | 5017 |     case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
 | 5018 |     case AUDIO_USAGE_NOTIFICATION_EVENT: | 
 | 5019 |     case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
 | 5020 |     case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
 | 5021 |     case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
 | 5022 |     case AUDIO_USAGE_GAME: | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5023 |     case AUDIO_USAGE_VIRTUAL_SOURCE: | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5024 |         break; | 
 | 5025 |     default: | 
 | 5026 |         return false; | 
 | 5027 |     } | 
 | 5028 |     return true; | 
 | 5029 | } | 
 | 5030 |  | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5031 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, | 
 | 5032 |                                           routing_strategy strategy, uint32_t inPastMs, | 
 | 5033 |                                           nsecs_t sysTime) const | 
 | 5034 | { | 
 | 5035 |     if ((sysTime == 0) && (inPastMs != 0)) { | 
 | 5036 |         sysTime = systemTime(); | 
 | 5037 |     } | 
 | 5038 |     for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) { | 
 | 5039 |         if (i == AUDIO_STREAM_PATCH) { | 
 | 5040 |             continue; | 
 | 5041 |         } | 
 | 5042 |         if (((getStrategy((audio_stream_type_t)i) == strategy) || | 
 | 5043 |                 (NUM_STRATEGIES == strategy)) && | 
 | 5044 |                 outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { | 
 | 5045 |             return true; | 
 | 5046 |         } | 
 | 5047 |     } | 
 | 5048 |     return false; | 
 | 5049 | } | 
 | 5050 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5051 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) | 
 | 5052 | { | 
 | 5053 |     return mEngine->getForceUse(usage); | 
 | 5054 | } | 
 | 5055 |  | 
 | 5056 | bool AudioPolicyManager::isInCall() | 
 | 5057 | { | 
 | 5058 |     return isStateInCall(mEngine->getPhoneState()); | 
 | 5059 | } | 
 | 5060 |  | 
 | 5061 | bool AudioPolicyManager::isStateInCall(int state) | 
 | 5062 | { | 
 | 5063 |     return is_state_in_call(state); | 
 | 5064 | } | 
 | 5065 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5066 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) | 
 | 5067 | { | 
 | 5068 |     for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--)  { | 
 | 5069 |         sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
 | 5070 |         if (sourceDesc->mDevice->equals(deviceDesc)) { | 
 | 5071 |             ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); | 
 | 5072 |             stopAudioSource(sourceDesc->getHandle()); | 
 | 5073 |         } | 
 | 5074 |     } | 
 | 5075 |  | 
 | 5076 |     for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--)  { | 
 | 5077 |         sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); | 
 | 5078 |         bool release = false; | 
 | 5079 |         for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++)  { | 
 | 5080 |             const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; | 
 | 5081 |             if (source->type == AUDIO_PORT_TYPE_DEVICE && | 
 | 5082 |                     source->ext.device.type == deviceDesc->type()) { | 
 | 5083 |                 release = true; | 
 | 5084 |             } | 
 | 5085 |         } | 
 | 5086 |         for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++)  { | 
 | 5087 |             const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; | 
 | 5088 |             if (sink->type == AUDIO_PORT_TYPE_DEVICE && | 
 | 5089 |                     sink->ext.device.type == deviceDesc->type()) { | 
 | 5090 |                 release = true; | 
 | 5091 |             } | 
 | 5092 |         } | 
 | 5093 |         if (release) { | 
 | 5094 |             ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); | 
 | 5095 |             releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); | 
 | 5096 |         } | 
 | 5097 |     } | 
 | 5098 | } | 
 | 5099 |  | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5100 | void AudioPolicyManager::updateAudioProfiles(audio_io_handle_t ioHandle, | 
 | 5101 |                                              AudioProfileVector &profiles) | 
 | 5102 | { | 
 | 5103 |     String8 reply; | 
 | 5104 |     char *value; | 
 | 5105 |     // Format MUST be checked first to update the list of AudioProfile | 
 | 5106 |     if (profiles.hasDynamicFormat()) { | 
 | 5107 |         reply = mpClientInterface->getParameters(ioHandle, | 
 | 5108 |                                                  String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); | 
 | 5109 |         ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); | 
 | 5110 |         value = strpbrk((char *)reply.string(), "="); | 
 | 5111 |         if (value == NULL) { | 
 | 5112 |             ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); | 
 | 5113 |             return; | 
 | 5114 |         } | 
 | 5115 |         profiles.setFormats(formatsFromString(value + 1)); | 
 | 5116 |     } | 
 | 5117 |     const FormatVector &supportedFormats = profiles.getSupportedFormats(); | 
 | 5118 |  | 
 | 5119 |     for(size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) { | 
 | 5120 |         audio_format_t format = supportedFormats[formatIndex]; | 
 | 5121 |         AudioParameter requestedParameters; | 
 | 5122 |         requestedParameters.addInt(String8(AUDIO_PARAMETER_STREAM_FORMAT), format); | 
 | 5123 |  | 
 | 5124 |         if (profiles.hasDynamicRateFor(format)) { | 
 | 5125 |             reply = mpClientInterface->getParameters(ioHandle, | 
 | 5126 |                                                      requestedParameters.toString() + ";" + | 
 | 5127 |                                                      AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES); | 
 | 5128 |             ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); | 
 | 5129 |             value = strpbrk((char *)reply.string(), "="); | 
 | 5130 |             if (value != NULL) { | 
 | 5131 |                 profiles.setSampleRatesFor(samplingRatesFromString(value + 1), format); | 
 | 5132 |             } | 
 | 5133 |         } | 
 | 5134 |         if (profiles.hasDynamicChannelsFor(format)) { | 
 | 5135 |             reply = mpClientInterface->getParameters(ioHandle, | 
 | 5136 |                                                      requestedParameters.toString() + ";" + | 
 | 5137 |                                                      AUDIO_PARAMETER_STREAM_SUP_CHANNELS); | 
 | 5138 |             ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); | 
 | 5139 |             value = strpbrk((char *)reply.string(), "="); | 
 | 5140 |             if (value != NULL) { | 
 | 5141 |                 profiles.setChannelsFor(channelMasksFromString(value + 1), format); | 
 | 5142 |             } | 
 | 5143 |         } | 
 | 5144 |     } | 
 | 5145 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5146 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5147 | }; // namespace android |