| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [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 |  | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AudioPolicyIntefaceImpl" | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 | 
 | 19 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 20 | #include "AudioPolicyService.h" | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 21 | #include "TypeConverter.h" | 
| Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 22 | #include <media/MediaMetricsItem.h> | 
| Kevin Rocard | be20185 | 2019-02-20 22:33:28 -0800 | [diff] [blame] | 23 | #include <media/AudioPolicy.h> | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 24 | #include <utils/Log.h> | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 25 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 26 | namespace android { | 
 | 27 |  | 
| Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 28 | const std::vector<audio_usage_t>& SYSTEM_USAGES = { | 
 | 29 |     AUDIO_USAGE_CALL_ASSISTANT, | 
 | 30 |     AUDIO_USAGE_EMERGENCY, | 
 | 31 |     AUDIO_USAGE_SAFETY, | 
 | 32 |     AUDIO_USAGE_VEHICLE_STATUS, | 
 | 33 |     AUDIO_USAGE_ANNOUNCEMENT | 
 | 34 | }; | 
 | 35 |  | 
 | 36 | bool isSystemUsage(audio_usage_t usage) { | 
 | 37 |     return std::find(std::begin(SYSTEM_USAGES), std::end(SYSTEM_USAGES), usage) | 
 | 38 |         != std::end(SYSTEM_USAGES); | 
 | 39 | } | 
 | 40 |  | 
 | 41 | bool AudioPolicyService::isSupportedSystemUsage(audio_usage_t usage) { | 
 | 42 |     return std::find(std::begin(mSupportedSystemUsages), std::end(mSupportedSystemUsages), usage) | 
 | 43 |         != std::end(mSupportedSystemUsages); | 
 | 44 | } | 
 | 45 |  | 
 | 46 | status_t AudioPolicyService::validateUsage(audio_usage_t usage) { | 
 | 47 |      return validateUsage(usage, IPCThreadState::self()->getCallingPid(), | 
 | 48 |         IPCThreadState::self()->getCallingUid()); | 
 | 49 | } | 
 | 50 |  | 
 | 51 | status_t AudioPolicyService::validateUsage(audio_usage_t usage, pid_t pid, uid_t uid) { | 
 | 52 |     if (isSystemUsage(usage)) { | 
 | 53 |         if (isSupportedSystemUsage(usage)) { | 
 | 54 |             if (!modifyAudioRoutingAllowed(pid, uid)) { | 
 | 55 |                 ALOGE("permission denied: modify audio routing not allowed for uid %d", uid); | 
 | 56 |                 return PERMISSION_DENIED; | 
 | 57 |             } | 
 | 58 |         } else { | 
 | 59 |             return BAD_VALUE; | 
 | 60 |         } | 
 | 61 |     } | 
 | 62 |     return NO_ERROR; | 
 | 63 | } | 
 | 64 |  | 
 | 65 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 66 |  | 
 | 67 | // ---------------------------------------------------------------------------- | 
 | 68 |  | 
| Mikhail Naganov | 88b30d2 | 2020-03-09 19:43:13 +0000 | [diff] [blame] | 69 | void AudioPolicyService::doOnNewAudioModulesAvailable() | 
 | 70 | { | 
 | 71 |     if (mAudioPolicyManager == NULL) return; | 
 | 72 |     Mutex::Autolock _l(mLock); | 
 | 73 |     AutoCallerClear acc; | 
 | 74 |     mAudioPolicyManager->onNewAudioModulesAvailable(); | 
 | 75 | } | 
 | 76 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 77 | status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device, | 
 | 78 |                                                   audio_policy_dev_state_t state, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 79 |                                                   const char *device_address, | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 80 |                                                   const char *device_name, | 
 | 81 |                                                   audio_format_t encodedFormat) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 82 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 83 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 84 |         return NO_INIT; | 
 | 85 |     } | 
 | 86 |     if (!settingsAllowed()) { | 
 | 87 |         return PERMISSION_DENIED; | 
 | 88 |     } | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 89 |     if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE && | 
 | 90 |             state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
 | 91 |         return BAD_VALUE; | 
 | 92 |     } | 
 | 93 |  | 
 | 94 |     ALOGV("setDeviceConnectionState()"); | 
 | 95 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 96 |     AutoCallerClear acc; | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 97 |     return mAudioPolicyManager->setDeviceConnectionState(device, state, | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 98 |                                                          device_address, device_name, encodedFormat); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 99 | } | 
 | 100 |  | 
 | 101 | audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState( | 
 | 102 |                                                               audio_devices_t device, | 
 | 103 |                                                               const char *device_address) | 
 | 104 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 105 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 106 |         return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
 | 107 |     } | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 108 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 109 |     return mAudioPolicyManager->getDeviceConnectionState(device, | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 110 |                                                       device_address); | 
 | 111 | } | 
 | 112 |  | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 113 | status_t AudioPolicyService::handleDeviceConfigChange(audio_devices_t device, | 
 | 114 |                                                   const char *device_address, | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 115 |                                                   const char *device_name, | 
 | 116 |                                                   audio_format_t encodedFormat) | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 117 | { | 
 | 118 |     if (mAudioPolicyManager == NULL) { | 
 | 119 |         return NO_INIT; | 
 | 120 |     } | 
 | 121 |     if (!settingsAllowed()) { | 
 | 122 |         return PERMISSION_DENIED; | 
 | 123 |     } | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 124 |  | 
 | 125 |     ALOGV("handleDeviceConfigChange()"); | 
 | 126 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 127 |     AutoCallerClear acc; | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 128 |     return mAudioPolicyManager->handleDeviceConfigChange(device, device_address, | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 129 |                                                          device_name, encodedFormat); | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 130 | } | 
 | 131 |  | 
| Eric Laurent | 00dba06 | 2020-02-11 15:52:09 -0800 | [diff] [blame] | 132 | status_t AudioPolicyService::setPhoneState(audio_mode_t state, uid_t uid) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 133 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 134 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 135 |         return NO_INIT; | 
 | 136 |     } | 
 | 137 |     if (!settingsAllowed()) { | 
 | 138 |         return PERMISSION_DENIED; | 
 | 139 |     } | 
 | 140 |     if (uint32_t(state) >= AUDIO_MODE_CNT) { | 
 | 141 |         return BAD_VALUE; | 
 | 142 |     } | 
 | 143 |  | 
 | 144 |     ALOGV("setPhoneState()"); | 
 | 145 |  | 
| Eric Laurent | beb07fe | 2015-09-16 15:49:30 -0700 | [diff] [blame] | 146 |     // acquire lock before calling setMode() so that setMode() + setPhoneState() are an atomic | 
 | 147 |     // operation from policy manager standpoint (no other operation (e.g track start or stop) | 
 | 148 |     // can be interleaved). | 
 | 149 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 150 |     // TODO: check if it is more appropriate to do it in platform specific policy manager | 
 | 151 |     AudioSystem::setMode(state); | 
 | 152 |  | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 153 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 154 |     mAudioPolicyManager->setPhoneState(state); | 
| Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 155 |     mPhoneState = state; | 
| Eric Laurent | 00dba06 | 2020-02-11 15:52:09 -0800 | [diff] [blame] | 156 |     mPhoneStateOwnerUid = uid; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 157 |     return NO_ERROR; | 
 | 158 | } | 
 | 159 |  | 
| Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 160 | audio_mode_t AudioPolicyService::getPhoneState() | 
 | 161 | { | 
 | 162 |     Mutex::Autolock _l(mLock); | 
 | 163 |     return mPhoneState; | 
 | 164 | } | 
 | 165 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 166 | status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage, | 
 | 167 |                                          audio_policy_forced_cfg_t config) | 
 | 168 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 169 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 170 |         return NO_INIT; | 
 | 171 |     } | 
| Eric Laurent | e17378d | 2018-05-09 14:43:01 -0700 | [diff] [blame] | 172 |  | 
 | 173 |     if (!modifyAudioRoutingAllowed()) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 174 |         return PERMISSION_DENIED; | 
 | 175 |     } | 
| Eric Laurent | e17378d | 2018-05-09 14:43:01 -0700 | [diff] [blame] | 176 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 177 |     if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { | 
 | 178 |         return BAD_VALUE; | 
 | 179 |     } | 
 | 180 |     if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) { | 
 | 181 |         return BAD_VALUE; | 
 | 182 |     } | 
 | 183 |     ALOGV("setForceUse()"); | 
 | 184 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 185 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 186 |     mAudioPolicyManager->setForceUse(usage, config); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 187 |     return NO_ERROR; | 
 | 188 | } | 
 | 189 |  | 
 | 190 | audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage) | 
 | 191 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 192 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 193 |         return AUDIO_POLICY_FORCE_NONE; | 
 | 194 |     } | 
 | 195 |     if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { | 
 | 196 |         return AUDIO_POLICY_FORCE_NONE; | 
 | 197 |     } | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 198 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 199 |     return mAudioPolicyManager->getForceUse(usage); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 200 | } | 
 | 201 |  | 
| Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 202 | audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 203 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 204 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 205 |         return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | dea1541 | 2014-10-28 15:46:45 -0700 | [diff] [blame] | 206 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 207 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 208 |         return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 209 |     } | 
 | 210 |     ALOGV("getOutput()"); | 
 | 211 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 212 |     AutoCallerClear acc; | 
| Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 213 |     return mAudioPolicyManager->getOutput(stream); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 214 | } | 
 | 215 |  | 
| Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 216 | status_t AudioPolicyService::getOutputForAttr(audio_attributes_t *attr, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 217 |                                               audio_io_handle_t *output, | 
 | 218 |                                               audio_session_t session, | 
 | 219 |                                               audio_stream_type_t *stream, | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 220 |                                               pid_t pid, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 221 |                                               uid_t uid, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 222 |                                               const audio_config_t *config, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 223 |                                               audio_output_flags_t flags, | 
| Eric Laurent | 9ae8c59 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 224 |                                               audio_port_handle_t *selectedDeviceId, | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 225 |                                               audio_port_handle_t *portId, | 
 | 226 |                                               std::vector<audio_io_handle_t> *secondaryOutputs) | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 227 | { | 
 | 228 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 229 |         return NO_INIT; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 230 |     } | 
| Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 231 |  | 
 | 232 |     status_t result = validateUsage(attr->usage, pid, uid); | 
 | 233 |     if (result != NO_ERROR) { | 
 | 234 |         return result; | 
 | 235 |     } | 
 | 236 |  | 
| Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 237 |     ALOGV("%s()", __func__); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 238 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 239 |  | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 240 |     const uid_t callingUid = IPCThreadState::self()->getCallingUid(); | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 241 |     if (!isAudioServerOrMediaServerUid(callingUid) || uid == (uid_t)-1) { | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 242 |         ALOGW_IF(uid != (uid_t)-1 && uid != callingUid, | 
| Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 243 |                 "%s uid %d tried to pass itself off as %d", __func__, callingUid, uid); | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 244 |         uid = callingUid; | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 245 |     } | 
| Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 246 |     if (!mPackageManager.allowPlaybackCapture(uid)) { | 
| Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 247 |         attr->flags |= AUDIO_FLAG_NO_MEDIA_PROJECTION; | 
 | 248 |     } | 
| Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 249 |     if (((attr->flags & (AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY|AUDIO_FLAG_BYPASS_MUTE)) != 0) | 
 | 250 |             && !bypassInterruptionPolicyAllowed(pid, uid)) { | 
| Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 251 |         attr->flags &= ~(AUDIO_FLAG_BYPASS_INTERRUPTION_POLICY|AUDIO_FLAG_BYPASS_MUTE); | 
| Kevin Rocard | 8be9497 | 2019-02-22 13:26:25 -0800 | [diff] [blame] | 252 |     } | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 253 |     AutoCallerClear acc; | 
| Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 254 |     AudioPolicyInterface::output_type_t outputType; | 
| Hayden Gomes | 3e8bbb9 | 2020-01-10 13:37:05 -0800 | [diff] [blame] | 255 |     result = mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 256 |                                                  config, | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 257 |                                                  &flags, selectedDeviceId, portId, | 
| Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 258 |                                                  secondaryOutputs, | 
 | 259 |                                                  &outputType); | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 260 |  | 
 | 261 |     // FIXME: Introduce a way to check for the the telephony device before opening the output | 
| Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 262 |     if (result == NO_ERROR) { | 
 | 263 |         // enforce permission (if any) required for each type of input | 
 | 264 |         switch (outputType) { | 
 | 265 |         case AudioPolicyInterface::API_OUTPUT_LEGACY: | 
 | 266 |             break; | 
 | 267 |         case AudioPolicyInterface::API_OUTPUT_TELEPHONY_TX: | 
| Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 268 |             if (!modifyPhoneStateAllowed(pid, uid)) { | 
| Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 269 |                 ALOGE("%s() permission denied: modify phone state not allowed for uid %d", | 
 | 270 |                     __func__, uid); | 
 | 271 |                 result = PERMISSION_DENIED; | 
 | 272 |             } | 
 | 273 |             break; | 
 | 274 |         case AudioPolicyInterface::API_OUT_MIX_PLAYBACK: | 
 | 275 |             if (!modifyAudioRoutingAllowed(pid, uid)) { | 
 | 276 |                 ALOGE("%s() permission denied: modify audio routing not allowed for uid %d", | 
 | 277 |                     __func__, uid); | 
 | 278 |                 result = PERMISSION_DENIED; | 
 | 279 |             } | 
 | 280 |             break; | 
 | 281 |         case AudioPolicyInterface::API_OUTPUT_INVALID: | 
 | 282 |         default: | 
 | 283 |             LOG_ALWAYS_FATAL("%s() encountered an invalid output type %d", | 
 | 284 |                 __func__, (int)outputType); | 
 | 285 |         } | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 286 |     } | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 287 |  | 
 | 288 |     if (result == NO_ERROR) { | 
 | 289 |         sp <AudioPlaybackClient> client = | 
| Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 290 |             new AudioPlaybackClient(*attr, *output, uid, pid, session, *portId, *selectedDeviceId, *stream); | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 291 |         mAudioPlaybackClients.add(*portId, client); | 
 | 292 |     } | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 293 |     return result; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 294 | } | 
 | 295 |  | 
| Eric Laurent | bcfe5be | 2019-04-09 19:56:39 -0700 | [diff] [blame] | 296 | void AudioPolicyService::getPlaybackClientAndEffects(audio_port_handle_t portId, | 
 | 297 |                                                      sp<AudioPlaybackClient>& client, | 
 | 298 |                                                      sp<AudioPolicyEffects>& effects, | 
 | 299 |                                                      const char *context) | 
 | 300 | { | 
 | 301 |     Mutex::Autolock _l(mLock); | 
 | 302 |     const ssize_t index = mAudioPlaybackClients.indexOfKey(portId); | 
 | 303 |     if (index < 0) { | 
 | 304 |         ALOGE("%s AudioTrack client not found for portId %d", context, portId); | 
 | 305 |         return; | 
 | 306 |     } | 
 | 307 |     client = mAudioPlaybackClients.valueAt(index); | 
 | 308 |     effects = mAudioPolicyEffects; | 
 | 309 | } | 
 | 310 |  | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 311 | status_t AudioPolicyService::startOutput(audio_port_handle_t portId) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 312 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 313 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 314 |         return NO_INIT; | 
 | 315 |     } | 
 | 316 |     ALOGV("startOutput()"); | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 317 |     sp<AudioPlaybackClient> client; | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 318 |     sp<AudioPolicyEffects>audioPolicyEffects; | 
| Eric Laurent | bcfe5be | 2019-04-09 19:56:39 -0700 | [diff] [blame] | 319 |  | 
 | 320 |     getPlaybackClientAndEffects(portId, client, audioPolicyEffects, __func__); | 
 | 321 |  | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 322 |     if (audioPolicyEffects != 0) { | 
 | 323 |         // create audio processors according to stream | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 324 |         status_t status = audioPolicyEffects->addOutputSessionEffects( | 
 | 325 |             client->io, client->stream, client->session); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 326 |         if (status != NO_ERROR && status != ALREADY_EXISTS) { | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 327 |             ALOGW("Failed to add effects on session %d", client->session); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 328 |         } | 
 | 329 |     } | 
 | 330 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 331 |     AutoCallerClear acc; | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 332 |     status_t status = mAudioPolicyManager->startOutput(portId); | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 333 |     if (status == NO_ERROR) { | 
 | 334 |         client->active = true; | 
 | 335 |     } | 
 | 336 |     return status; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 337 | } | 
 | 338 |  | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 339 | status_t AudioPolicyService::stopOutput(audio_port_handle_t portId) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 340 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 341 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 342 |         return NO_INIT; | 
 | 343 |     } | 
 | 344 |     ALOGV("stopOutput()"); | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 345 |     mOutputCommandThread->stopOutputCommand(portId); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 346 |     return NO_ERROR; | 
 | 347 | } | 
 | 348 |  | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 349 | status_t  AudioPolicyService::doStopOutput(audio_port_handle_t portId) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 350 | { | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 351 |     ALOGV("doStopOutput"); | 
 | 352 |     sp<AudioPlaybackClient> client; | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 353 |     sp<AudioPolicyEffects>audioPolicyEffects; | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 354 |  | 
| Eric Laurent | bcfe5be | 2019-04-09 19:56:39 -0700 | [diff] [blame] | 355 |     getPlaybackClientAndEffects(portId, client, audioPolicyEffects, __func__); | 
 | 356 |  | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 357 |     if (audioPolicyEffects != 0) { | 
 | 358 |         // release audio processors from the stream | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 359 |         status_t status = audioPolicyEffects->releaseOutputSessionEffects( | 
 | 360 |             client->io, client->stream, client->session); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 361 |         if (status != NO_ERROR && status != ALREADY_EXISTS) { | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 362 |             ALOGW("Failed to release effects on session %d", client->session); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 363 |         } | 
 | 364 |     } | 
 | 365 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 366 |     AutoCallerClear acc; | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 367 |     status_t status = mAudioPolicyManager->stopOutput(portId); | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 368 |     if (status == NO_ERROR) { | 
 | 369 |         client->active = false; | 
 | 370 |     } | 
 | 371 |     return status; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 372 | } | 
 | 373 |  | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 374 | void AudioPolicyService::releaseOutput(audio_port_handle_t portId) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 375 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 376 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 377 |         return; | 
 | 378 |     } | 
 | 379 |     ALOGV("releaseOutput()"); | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 380 |     mOutputCommandThread->releaseOutputCommand(portId); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 381 | } | 
 | 382 |  | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 383 | void AudioPolicyService::doReleaseOutput(audio_port_handle_t portId) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 384 | { | 
 | 385 |     ALOGV("doReleaseOutput from tid %d", gettid()); | 
| Eric Laurent | bcfe5be | 2019-04-09 19:56:39 -0700 | [diff] [blame] | 386 |     sp<AudioPlaybackClient> client; | 
 | 387 |     sp<AudioPolicyEffects> audioPolicyEffects; | 
 | 388 |  | 
 | 389 |     getPlaybackClientAndEffects(portId, client, audioPolicyEffects, __func__); | 
 | 390 |  | 
 | 391 |     if (audioPolicyEffects != 0 && client->active) { | 
 | 392 |         // clean up effects if output was not stopped before being released | 
 | 393 |         audioPolicyEffects->releaseOutputSessionEffects( | 
 | 394 |             client->io, client->stream, client->session); | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 395 |     } | 
| Eric Laurent | bcfe5be | 2019-04-09 19:56:39 -0700 | [diff] [blame] | 396 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | d400724 | 2019-03-27 12:42:16 -0700 | [diff] [blame] | 397 |     mAudioPlaybackClients.removeItem(portId); | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 398 |  | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 399 |     // called from internal thread: no need to clear caller identity | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 400 |     mAudioPolicyManager->releaseOutput(portId); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 401 | } | 
 | 402 |  | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 403 | status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr, | 
 | 404 |                                              audio_io_handle_t *input, | 
| Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 405 |                                              audio_unique_id_t riid, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 406 |                                              audio_session_t session, | 
| Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 407 |                                              pid_t pid, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 408 |                                              uid_t uid, | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 409 |                                              const String16& opPackageName, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 410 |                                              const audio_config_base_t *config, | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 411 |                                              audio_input_flags_t flags, | 
| Eric Laurent | 9ae8c59 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 412 |                                              audio_port_handle_t *selectedDeviceId, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 413 |                                              audio_port_handle_t *portId) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 414 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 415 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 416 |         return NO_INIT; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 417 |     } | 
| Eric Laurent | 7dca8a8 | 2018-01-29 18:44:26 -0800 | [diff] [blame] | 418 |  | 
| Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 419 |     status_t result = validateUsage(attr->usage, pid, uid); | 
 | 420 |     if (result != NO_ERROR) { | 
 | 421 |         return result; | 
 | 422 |     } | 
 | 423 |  | 
| Hiroaki Hayashi | 4de0b45 | 2019-07-18 19:50:47 +0900 | [diff] [blame] | 424 |     audio_source_t inputSource = attr->source; | 
 | 425 |     if (inputSource == AUDIO_SOURCE_DEFAULT) { | 
 | 426 |         inputSource = AUDIO_SOURCE_MIC; | 
 | 427 |     } | 
 | 428 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 429 |     // already checked by client, but double-check in case the client wrapper is bypassed | 
| Hiroaki Hayashi | 4de0b45 | 2019-07-18 19:50:47 +0900 | [diff] [blame] | 430 |     if ((inputSource < AUDIO_SOURCE_DEFAULT) | 
 | 431 |             || (inputSource >= AUDIO_SOURCE_CNT | 
 | 432 |                 && inputSource != AUDIO_SOURCE_HOTWORD | 
 | 433 |                 && inputSource != AUDIO_SOURCE_FM_TUNER | 
 | 434 |                 && inputSource != AUDIO_SOURCE_ECHO_REFERENCE)) { | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 435 |         return BAD_VALUE; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 436 |     } | 
 | 437 |  | 
| Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 438 |     bool updatePid = (pid == -1); | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 439 |     const uid_t callingUid = IPCThreadState::self()->getCallingUid(); | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 440 |     if (!isAudioServerOrMediaServerUid(callingUid)) { | 
| Eric Laurent | 9f39f8d | 2016-05-25 12:34:48 -0700 | [diff] [blame] | 441 |         ALOGW_IF(uid != (uid_t)-1 && uid != callingUid, | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 442 |                 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid); | 
 | 443 |         uid = callingUid; | 
| Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 444 |         updatePid = true; | 
 | 445 |     } | 
 | 446 |  | 
 | 447 |     if (updatePid) { | 
 | 448 |         const pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
| Eric Laurent | 9f39f8d | 2016-05-25 12:34:48 -0700 | [diff] [blame] | 449 |         ALOGW_IF(pid != (pid_t)-1 && pid != callingPid, | 
| Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 450 |                  "%s uid %d pid %d tried to pass itself off as pid %d", | 
 | 451 |                  __func__, callingUid, callingPid, pid); | 
 | 452 |         pid = callingPid; | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 453 |     } | 
 | 454 |  | 
| Eric Laurent | 58a0dd8 | 2019-10-24 12:42:17 -0700 | [diff] [blame] | 455 |     // check calling permissions. | 
 | 456 |     // Capturing from FM_TUNER source is controlled by captureAudioOutputAllowed() only as this | 
 | 457 |     // does not affect users privacy as does capturing from an actual microphone. | 
 | 458 |     if (!(recordingAllowed(opPackageName, pid, uid) || attr->source == AUDIO_SOURCE_FM_TUNER)) { | 
| Eric Laurent | 7dca8a8 | 2018-01-29 18:44:26 -0800 | [diff] [blame] | 459 |         ALOGE("%s permission denied: recording not allowed for uid %d pid %d", | 
 | 460 |                 __func__, uid, pid); | 
 | 461 |         return PERMISSION_DENIED; | 
 | 462 |     } | 
 | 463 |  | 
| Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 464 |     bool canCaptureOutput = captureAudioOutputAllowed(pid, uid); | 
| Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 465 |     if ((inputSource == AUDIO_SOURCE_VOICE_UPLINK || | 
 | 466 |         inputSource == AUDIO_SOURCE_VOICE_DOWNLINK || | 
 | 467 |         inputSource == AUDIO_SOURCE_VOICE_CALL || | 
 | 468 |         inputSource == AUDIO_SOURCE_ECHO_REFERENCE|| | 
 | 469 |         inputSource == AUDIO_SOURCE_FM_TUNER) && | 
| Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 470 |         !canCaptureOutput) { | 
| Nadav Bar | 744be48 | 2018-05-08 13:26:21 +0300 | [diff] [blame] | 471 |         return PERMISSION_DENIED; | 
 | 472 |     } | 
 | 473 |  | 
| jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 474 |     bool canCaptureHotword = captureHotwordAllowed(opPackageName, pid, uid); | 
| Hiroaki Hayashi | 4de0b45 | 2019-07-18 19:50:47 +0900 | [diff] [blame] | 475 |     if ((inputSource == AUDIO_SOURCE_HOTWORD) && !canCaptureHotword) { | 
| Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 476 |         return BAD_VALUE; | 
 | 477 |     } | 
 | 478 |  | 
 | 479 |     sp<AudioPolicyEffects>audioPolicyEffects; | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 480 |     { | 
| Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 481 |         status_t status; | 
 | 482 |         AudioPolicyInterface::input_type_t inputType; | 
 | 483 |  | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 484 |         Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 485 |         { | 
 | 486 |             AutoCallerClear acc; | 
 | 487 |             // the audio_in_acoustics_t parameter is ignored by get_input() | 
| Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 488 |             status = mAudioPolicyManager->getInputForAttr(attr, input, riid, session, uid, | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 489 |                                                          config, | 
 | 490 |                                                          flags, selectedDeviceId, | 
 | 491 |                                                          &inputType, portId); | 
 | 492 |         } | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 493 |         audioPolicyEffects = mAudioPolicyEffects; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 494 |  | 
 | 495 |         if (status == NO_ERROR) { | 
 | 496 |             // enforce permission (if any) required for each type of input | 
 | 497 |             switch (inputType) { | 
| Kevin Rocard | 25f9b05 | 2019-02-27 15:08:54 -0800 | [diff] [blame] | 498 |             case AudioPolicyInterface::API_INPUT_MIX_PUBLIC_CAPTURE_PLAYBACK: | 
 | 499 |                 // this use case has been validated in audio service with a MediaProjection token, | 
 | 500 |                 // and doesn't rely on regular permissions | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 501 |             case AudioPolicyInterface::API_INPUT_LEGACY: | 
 | 502 |                 break; | 
| Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 503 |             case AudioPolicyInterface::API_INPUT_TELEPHONY_RX: | 
 | 504 |                 // FIXME: use the same permission as for remote submix for now. | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 505 |             case AudioPolicyInterface::API_INPUT_MIX_CAPTURE: | 
| Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 506 |                 if (!canCaptureOutput) { | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 507 |                     ALOGE("getInputForAttr() permission denied: capture not allowed"); | 
 | 508 |                     status = PERMISSION_DENIED; | 
 | 509 |                 } | 
 | 510 |                 break; | 
 | 511 |             case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE: | 
| Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 512 |                 if (!modifyAudioRoutingAllowed(pid, uid)) { | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 513 |                     ALOGE("getInputForAttr() permission denied: modify audio routing not allowed"); | 
 | 514 |                     status = PERMISSION_DENIED; | 
 | 515 |                 } | 
 | 516 |                 break; | 
 | 517 |             case AudioPolicyInterface::API_INPUT_INVALID: | 
 | 518 |             default: | 
 | 519 |                 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d", | 
 | 520 |                         (int)inputType); | 
 | 521 |             } | 
 | 522 |         } | 
 | 523 |  | 
 | 524 |         if (status != NO_ERROR) { | 
 | 525 |             if (status == PERMISSION_DENIED) { | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 526 |                 AutoCallerClear acc; | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 527 |                 mAudioPolicyManager->releaseInput(*portId); | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 528 |             } | 
 | 529 |             return status; | 
 | 530 |         } | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 531 |  | 
| Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 532 |         sp<AudioRecordClient> client = new AudioRecordClient(*attr, *input, uid, pid, session, *portId, | 
| Eric Laurent | 1ff16a7 | 2019-03-14 18:35:04 -0700 | [diff] [blame] | 533 |                                                              *selectedDeviceId, opPackageName, | 
| Ricardo Correa | 57a3769 | 2020-03-23 17:27:25 -0700 | [diff] [blame] | 534 |                                                              canCaptureOutput, canCaptureHotword); | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 535 |         mAudioRecordClients.add(*portId, client); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 536 |     } | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 537 |  | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 538 |     if (audioPolicyEffects != 0) { | 
 | 539 |         // create audio pre processors according to input source | 
| Hiroaki Hayashi | 4de0b45 | 2019-07-18 19:50:47 +0900 | [diff] [blame] | 540 |         status_t status = audioPolicyEffects->addInputEffects(*input, inputSource, session); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 541 |         if (status != NO_ERROR && status != ALREADY_EXISTS) { | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 542 |             ALOGW("Failed to add effects on input %d", *input); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 543 |         } | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 544 |     } | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 545 |     return NO_ERROR; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 546 | } | 
 | 547 |  | 
| Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 548 | std::string AudioPolicyService::getDeviceTypeStrForPortId(audio_port_handle_t portId) { | 
| Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 549 |     struct audio_port port = {}; | 
 | 550 |     port.id = portId; | 
 | 551 |     status_t status = mAudioPolicyManager->getAudioPort(&port); | 
 | 552 |     if (status == NO_ERROR && port.type == AUDIO_PORT_TYPE_DEVICE) { | 
| Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 553 |         return toString(port.ext.device.type); | 
| Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 554 |     } | 
| Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 555 |     return {}; | 
| Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 556 | } | 
 | 557 |  | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 558 | status_t AudioPolicyService::startInput(audio_port_handle_t portId) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 559 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 560 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 561 |         return NO_INIT; | 
 | 562 |     } | 
| Eric Laurent | 7dca8a8 | 2018-01-29 18:44:26 -0800 | [diff] [blame] | 563 |     sp<AudioRecordClient> client; | 
 | 564 |     { | 
 | 565 |         Mutex::Autolock _l(mLock); | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 566 |  | 
| Eric Laurent | 7dca8a8 | 2018-01-29 18:44:26 -0800 | [diff] [blame] | 567 |         ssize_t index = mAudioRecordClients.indexOfKey(portId); | 
 | 568 |         if (index < 0) { | 
 | 569 |             return INVALID_OPERATION; | 
 | 570 |         } | 
 | 571 |         client = mAudioRecordClients.valueAt(index); | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 572 |     } | 
| Eric Laurent | 7dca8a8 | 2018-01-29 18:44:26 -0800 | [diff] [blame] | 573 |  | 
 | 574 |     // check calling permissions | 
| Narayan Kamath | d127644 | 2020-08-20 17:19:57 +0100 | [diff] [blame] | 575 |     if (!(startRecording(client->opPackageName, client->pid, client->uid, | 
 | 576 |             client->attributes.source == AUDIO_SOURCE_HOTWORD) | 
| Eric Laurent | 58a0dd8 | 2019-10-24 12:42:17 -0700 | [diff] [blame] | 577 |             || client->attributes.source == AUDIO_SOURCE_FM_TUNER)) { | 
| Eric Laurent | 7dca8a8 | 2018-01-29 18:44:26 -0800 | [diff] [blame] | 578 |         ALOGE("%s permission denied: recording not allowed for uid %d pid %d", | 
 | 579 |                 __func__, client->uid, client->pid); | 
 | 580 |         return PERMISSION_DENIED; | 
 | 581 |     } | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 582 |  | 
| Eric Laurent | df62892 | 2018-12-06 21:45:51 +0000 | [diff] [blame] | 583 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 584 |  | 
 | 585 |     client->active = true; | 
 | 586 |     client->startTimeNs = systemTime(); | 
 | 587 |     updateUidStates_l(); | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 588 |  | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 589 |     status_t status; | 
 | 590 |     { | 
 | 591 |         AutoCallerClear acc; | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 592 |         status = mAudioPolicyManager->startInput(portId); | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 593 |  | 
 | 594 |     } | 
 | 595 |  | 
| Ray Essick | f6a57cd | 2018-05-22 16:20:54 -0700 | [diff] [blame] | 596 |     // including successes gets very verbose | 
| Muhammad Qureshi | 087b37c | 2020-06-16 16:37:36 -0700 | [diff] [blame] | 597 |     // but once we cut over to statsd, log them all. | 
| Ray Essick | f6a57cd | 2018-05-22 16:20:54 -0700 | [diff] [blame] | 598 |     if (status != NO_ERROR) { | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 599 |  | 
 | 600 |         static constexpr char kAudioPolicy[] = "audiopolicy"; | 
 | 601 |  | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 602 |         static constexpr char kAudioPolicyStatus[] = "android.media.audiopolicy.status"; | 
 | 603 |         static constexpr char kAudioPolicyRqstSrc[] = "android.media.audiopolicy.rqst.src"; | 
 | 604 |         static constexpr char kAudioPolicyRqstPkg[] = "android.media.audiopolicy.rqst.pkg"; | 
 | 605 |         static constexpr char kAudioPolicyRqstSession[] = "android.media.audiopolicy.rqst.session"; | 
| Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 606 |         static constexpr char kAudioPolicyRqstDevice[] = | 
 | 607 |                 "android.media.audiopolicy.rqst.device"; | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 608 |         static constexpr char kAudioPolicyActiveSrc[] = "android.media.audiopolicy.active.src"; | 
 | 609 |         static constexpr char kAudioPolicyActivePkg[] = "android.media.audiopolicy.active.pkg"; | 
| Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 610 |         static constexpr char kAudioPolicyActiveSession[] = | 
 | 611 |                 "android.media.audiopolicy.active.session"; | 
 | 612 |         static constexpr char kAudioPolicyActiveDevice[] = | 
 | 613 |                 "android.media.audiopolicy.active.device"; | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 614 |  | 
| Ray Essick | f27e987 | 2019-12-07 06:28:46 -0800 | [diff] [blame] | 615 |         mediametrics::Item *item = mediametrics::Item::create(kAudioPolicy); | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 616 |         if (item != NULL) { | 
 | 617 |  | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 618 |             item->setInt32(kAudioPolicyStatus, status); | 
 | 619 |  | 
| Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 620 |             item->setCString(kAudioPolicyRqstSrc, | 
| Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 621 |                              toString(client->attributes.source).c_str()); | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 622 |             item->setInt32(kAudioPolicyRqstSession, client->session); | 
| Ray Essick | 5186695 | 2018-05-30 11:22:27 -0700 | [diff] [blame] | 623 |             if (client->opPackageName.size() != 0) { | 
 | 624 |                 item->setCString(kAudioPolicyRqstPkg, | 
 | 625 |                                  std::string(String8(client->opPackageName).string()).c_str()); | 
 | 626 |             } else { | 
| Kevin Rocard | fbdfebe | 2018-06-18 12:30:40 -0700 | [diff] [blame] | 627 |                 item->setCString(kAudioPolicyRqstPkg, std::to_string(client->uid).c_str()); | 
| Ray Essick | 5186695 | 2018-05-30 11:22:27 -0700 | [diff] [blame] | 628 |             } | 
| Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 629 |             item->setCString( | 
 | 630 |                     kAudioPolicyRqstDevice, getDeviceTypeStrForPortId(client->deviceId).c_str()); | 
 | 631 |  | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 632 |             int count = mAudioRecordClients.size(); | 
 | 633 |             for (int i = 0; i < count ; i++) { | 
 | 634 |                 if (portId == mAudioRecordClients.keyAt(i)) { | 
 | 635 |                     continue; | 
 | 636 |                 } | 
 | 637 |                 sp<AudioRecordClient> other = mAudioRecordClients.valueAt(i); | 
 | 638 |                 if (other->active) { | 
 | 639 |                     // keeps the last of the clients marked active | 
 | 640 |                     item->setCString(kAudioPolicyActiveSrc, | 
| Andy Hung | 9b18195 | 2019-02-25 14:53:36 -0800 | [diff] [blame] | 641 |                                      toString(other->attributes.source).c_str()); | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 642 |                     item->setInt32(kAudioPolicyActiveSession, other->session); | 
 | 643 |                     if (other->opPackageName.size() != 0) { | 
 | 644 |                         item->setCString(kAudioPolicyActivePkg, | 
 | 645 |                              std::string(String8(other->opPackageName).string()).c_str()); | 
 | 646 |                     } else { | 
 | 647 |                         item->setCString(kAudioPolicyRqstPkg, | 
 | 648 |                                          std::to_string(other->uid).c_str()); | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 649 |                     } | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 650 |                     item->setCString(kAudioPolicyActiveDevice, | 
 | 651 |                                      getDeviceTypeStrForPortId(other->deviceId).c_str()); | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 652 |                 } | 
 | 653 |             } | 
 | 654 |             item->selfrecord(); | 
 | 655 |             delete item; | 
 | 656 |             item = NULL; | 
 | 657 |         } | 
| Ray Essick | 6ce27e5 | 2019-02-15 10:58:05 -0800 | [diff] [blame] | 658 |     } | 
 | 659 |  | 
 | 660 |     if (status != NO_ERROR) { | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 661 |         client->active = false; | 
 | 662 |         client->startTimeNs = 0; | 
 | 663 |         updateUidStates_l(); | 
| Narayan Kamath | d127644 | 2020-08-20 17:19:57 +0100 | [diff] [blame] | 664 |         finishRecording(client->opPackageName, client->uid, | 
 | 665 |                         client->attributes.source == AUDIO_SOURCE_HOTWORD); | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 666 |     } | 
 | 667 |  | 
 | 668 |     return status; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 669 | } | 
 | 670 |  | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 671 | status_t AudioPolicyService::stopInput(audio_port_handle_t portId) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 672 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 673 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 674 |         return NO_INIT; | 
 | 675 |     } | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 676 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 677 |     Mutex::Autolock _l(mLock); | 
 | 678 |  | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 679 |     ssize_t index = mAudioRecordClients.indexOfKey(portId); | 
 | 680 |     if (index < 0) { | 
 | 681 |         return INVALID_OPERATION; | 
 | 682 |     } | 
 | 683 |     sp<AudioRecordClient> client = mAudioRecordClients.valueAt(index); | 
 | 684 |  | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 685 |     client->active = false; | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 686 |     client->startTimeNs = 0; | 
 | 687 |  | 
 | 688 |     updateUidStates_l(); | 
| Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 689 |  | 
| Svet Ganov | 6e64137 | 2018-03-02 09:21:30 -0800 | [diff] [blame] | 690 |     // finish the recording app op | 
| Narayan Kamath | d127644 | 2020-08-20 17:19:57 +0100 | [diff] [blame] | 691 |     finishRecording(client->opPackageName, client->uid, | 
 | 692 |                     client->attributes.source == AUDIO_SOURCE_HOTWORD); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 693 |     AutoCallerClear acc; | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 694 |     return mAudioPolicyManager->stopInput(portId); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 695 | } | 
 | 696 |  | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 697 | void AudioPolicyService::releaseInput(audio_port_handle_t portId) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 698 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 699 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 700 |         return; | 
 | 701 |     } | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 702 |     sp<AudioPolicyEffects>audioPolicyEffects; | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 703 |     sp<AudioRecordClient> client; | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 704 |     { | 
 | 705 |         Mutex::Autolock _l(mLock); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 706 |         audioPolicyEffects = mAudioPolicyEffects; | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 707 |         ssize_t index = mAudioRecordClients.indexOfKey(portId); | 
 | 708 |         if (index < 0) { | 
 | 709 |             return; | 
 | 710 |         } | 
 | 711 |         client = mAudioRecordClients.valueAt(index); | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 712 |  | 
 | 713 |         if (client->active) { | 
 | 714 |             ALOGW("%s releasing active client portId %d", __FUNCTION__, portId); | 
 | 715 |             client->active = false; | 
 | 716 |             client->startTimeNs = 0; | 
 | 717 |             updateUidStates_l(); | 
 | 718 |         } | 
 | 719 |  | 
| Eric Laurent | fee1976 | 2018-01-29 18:44:13 -0800 | [diff] [blame] | 720 |         mAudioRecordClients.removeItem(portId); | 
 | 721 |     } | 
 | 722 |     if (client == 0) { | 
 | 723 |         return; | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 724 |     } | 
 | 725 |     if (audioPolicyEffects != 0) { | 
 | 726 |         // release audio processors from the input | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 727 |         status_t status = audioPolicyEffects->releaseInputEffects(client->io, client->session); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 728 |         if(status != NO_ERROR) { | 
| Eric Laurent | d7fe086 | 2018-07-14 16:48:01 -0700 | [diff] [blame] | 729 |             ALOGW("Failed to release effects on input %d", client->io); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 730 |         } | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 731 |     } | 
| Eric Laurent | f10c709 | 2016-12-06 17:09:56 -0800 | [diff] [blame] | 732 |     { | 
 | 733 |         Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 734 |         AutoCallerClear acc; | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 735 |         mAudioPolicyManager->releaseInput(portId); | 
| Eric Laurent | f10c709 | 2016-12-06 17:09:56 -0800 | [diff] [blame] | 736 |     } | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 737 | } | 
 | 738 |  | 
 | 739 | status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream, | 
 | 740 |                                             int indexMin, | 
 | 741 |                                             int indexMax) | 
 | 742 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 743 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 744 |         return NO_INIT; | 
 | 745 |     } | 
 | 746 |     if (!settingsAllowed()) { | 
 | 747 |         return PERMISSION_DENIED; | 
 | 748 |     } | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 749 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 750 |         return BAD_VALUE; | 
 | 751 |     } | 
 | 752 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 753 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 754 |     mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 755 |     return NO_ERROR; | 
 | 756 | } | 
 | 757 |  | 
 | 758 | status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, | 
 | 759 |                                                   int index, | 
 | 760 |                                                   audio_devices_t device) | 
 | 761 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 762 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 763 |         return NO_INIT; | 
 | 764 |     } | 
 | 765 |     if (!settingsAllowed()) { | 
 | 766 |         return PERMISSION_DENIED; | 
 | 767 |     } | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 768 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 769 |         return BAD_VALUE; | 
 | 770 |     } | 
 | 771 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 772 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 773 |     return mAudioPolicyManager->setStreamVolumeIndex(stream, | 
 | 774 |                                                     index, | 
 | 775 |                                                     device); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 776 | } | 
 | 777 |  | 
 | 778 | status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, | 
 | 779 |                                                   int *index, | 
 | 780 |                                                   audio_devices_t device) | 
 | 781 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 782 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 783 |         return NO_INIT; | 
 | 784 |     } | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 785 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 786 |         return BAD_VALUE; | 
 | 787 |     } | 
 | 788 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 789 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 790 |     return mAudioPolicyManager->getStreamVolumeIndex(stream, | 
 | 791 |                                                     index, | 
 | 792 |                                                     device); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 793 | } | 
 | 794 |  | 
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 795 | status_t AudioPolicyService::setVolumeIndexForAttributes(const audio_attributes_t &attributes, | 
 | 796 |                                                          int index, audio_devices_t device) | 
 | 797 | { | 
 | 798 |     if (mAudioPolicyManager == NULL) { | 
 | 799 |         return NO_INIT; | 
 | 800 |     } | 
 | 801 |     if (!settingsAllowed()) { | 
 | 802 |         return PERMISSION_DENIED; | 
 | 803 |     } | 
 | 804 |     Mutex::Autolock _l(mLock); | 
 | 805 |     AutoCallerClear acc; | 
 | 806 |     return mAudioPolicyManager->setVolumeIndexForAttributes(attributes, index, device); | 
 | 807 | } | 
 | 808 |  | 
 | 809 | status_t AudioPolicyService::getVolumeIndexForAttributes(const audio_attributes_t &attributes, | 
 | 810 |                                                          int &index, audio_devices_t device) | 
 | 811 | { | 
 | 812 |     if (mAudioPolicyManager == NULL) { | 
 | 813 |         return NO_INIT; | 
 | 814 |     } | 
 | 815 |     Mutex::Autolock _l(mLock); | 
 | 816 |     AutoCallerClear acc; | 
 | 817 |     return mAudioPolicyManager->getVolumeIndexForAttributes(attributes, index, device); | 
 | 818 | } | 
 | 819 |  | 
 | 820 | status_t AudioPolicyService::getMinVolumeIndexForAttributes(const audio_attributes_t &attributes, | 
 | 821 |                                                             int &index) | 
 | 822 | { | 
 | 823 |     if (mAudioPolicyManager == NULL) { | 
 | 824 |         return NO_INIT; | 
 | 825 |     } | 
 | 826 |     Mutex::Autolock _l(mLock); | 
 | 827 |     AutoCallerClear acc; | 
 | 828 |     return mAudioPolicyManager->getMinVolumeIndexForAttributes(attributes, index); | 
 | 829 | } | 
 | 830 |  | 
 | 831 | status_t AudioPolicyService::getMaxVolumeIndexForAttributes(const audio_attributes_t &attributes, | 
 | 832 |                                                             int &index) | 
 | 833 | { | 
 | 834 |     if (mAudioPolicyManager == NULL) { | 
 | 835 |         return NO_INIT; | 
 | 836 |     } | 
 | 837 |     Mutex::Autolock _l(mLock); | 
 | 838 |     AutoCallerClear acc; | 
 | 839 |     return mAudioPolicyManager->getMaxVolumeIndexForAttributes(attributes, index); | 
 | 840 | } | 
 | 841 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 842 | uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream) | 
 | 843 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 844 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 845 |         return PRODUCT_STRATEGY_NONE; | 
| Eric Laurent | dea1541 | 2014-10-28 15:46:45 -0700 | [diff] [blame] | 846 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 847 |     if (mAudioPolicyManager == NULL) { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 848 |         return PRODUCT_STRATEGY_NONE; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 849 |     } | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 850 |     // DO NOT LOCK, may be called from AudioFlinger with lock held, reaching deadlock | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 851 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 852 |     return mAudioPolicyManager->getStrategyForStream(stream); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 853 | } | 
 | 854 |  | 
 | 855 | //audio policy: use audio_device_t appropriately | 
 | 856 |  | 
 | 857 | audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream) | 
 | 858 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 859 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 860 |         return AUDIO_DEVICE_NONE; | 
| Eric Laurent | dea1541 | 2014-10-28 15:46:45 -0700 | [diff] [blame] | 861 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 862 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 863 |         return AUDIO_DEVICE_NONE; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 864 |     } | 
| Haynes Mathew George | dfb9f3b | 2015-10-26 18:22:13 -0700 | [diff] [blame] | 865 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 866 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 867 |     return mAudioPolicyManager->getDevicesForStream(stream); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 868 | } | 
 | 869 |  | 
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 870 | status_t AudioPolicyService::getDevicesForAttributes(const AudioAttributes &aa, | 
| Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 871 |                                                      AudioDeviceTypeAddrVector *devices) const | 
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 872 | { | 
 | 873 |     if (mAudioPolicyManager == NULL) { | 
 | 874 |         return NO_INIT; | 
 | 875 |     } | 
 | 876 |     Mutex::Autolock _l(mLock); | 
 | 877 |     AutoCallerClear acc; | 
 | 878 |     return mAudioPolicyManager->getDevicesForAttributes(aa.getAttributes(), devices); | 
 | 879 | } | 
 | 880 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 881 | audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc) | 
 | 882 | { | 
 | 883 |     // FIXME change return type to status_t, and return NO_INIT here | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 884 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 885 |         return 0; | 
 | 886 |     } | 
 | 887 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 888 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 889 |     return mAudioPolicyManager->getOutputForEffect(desc); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 890 | } | 
 | 891 |  | 
 | 892 | status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc, | 
 | 893 |                                 audio_io_handle_t io, | 
 | 894 |                                 uint32_t strategy, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 895 |                                 audio_session_t session, | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 896 |                                 int id) | 
 | 897 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 898 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 899 |         return NO_INIT; | 
 | 900 |     } | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 901 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 902 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 903 |     return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 904 | } | 
 | 905 |  | 
 | 906 | status_t AudioPolicyService::unregisterEffect(int id) | 
 | 907 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 908 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 909 |         return NO_INIT; | 
 | 910 |     } | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 911 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 912 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 913 |     return mAudioPolicyManager->unregisterEffect(id); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 914 | } | 
 | 915 |  | 
 | 916 | status_t AudioPolicyService::setEffectEnabled(int id, bool enabled) | 
 | 917 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 918 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 919 |         return NO_INIT; | 
 | 920 |     } | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 921 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 922 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 923 |     return mAudioPolicyManager->setEffectEnabled(id, enabled); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 924 | } | 
 | 925 |  | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 926 | status_t AudioPolicyService::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) | 
 | 927 | { | 
 | 928 |     if (mAudioPolicyManager == NULL) { | 
 | 929 |         return NO_INIT; | 
 | 930 |     } | 
 | 931 |     Mutex::Autolock _l(mLock); | 
 | 932 |     AutoCallerClear acc; | 
 | 933 |     return mAudioPolicyManager->moveEffectsToIo(ids, io); | 
 | 934 | } | 
 | 935 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 936 | bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const | 
 | 937 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 938 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 939 |         return false; | 
| Eric Laurent | dea1541 | 2014-10-28 15:46:45 -0700 | [diff] [blame] | 940 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 941 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 942 |         return false; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 943 |     } | 
 | 944 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 945 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 946 |     return mAudioPolicyManager->isStreamActive(stream, inPastMs); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 947 | } | 
 | 948 |  | 
 | 949 | bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const | 
 | 950 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 951 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 952 |         return false; | 
| Eric Laurent | dea1541 | 2014-10-28 15:46:45 -0700 | [diff] [blame] | 953 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 954 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 955 |         return false; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 956 |     } | 
 | 957 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 958 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 959 |     return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 960 | } | 
 | 961 |  | 
 | 962 | bool AudioPolicyService::isSourceActive(audio_source_t source) const | 
 | 963 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 964 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 965 |         return false; | 
 | 966 |     } | 
 | 967 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 968 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 969 |     return mAudioPolicyManager->isSourceActive(source); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 970 | } | 
 | 971 |  | 
| Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 972 | status_t AudioPolicyService::getAudioPolicyEffects(sp<AudioPolicyEffects>& audioPolicyEffects) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 973 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 974 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 975 |         return NO_INIT; | 
 | 976 |     } | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 977 |     { | 
 | 978 |         Mutex::Autolock _l(mLock); | 
 | 979 |         audioPolicyEffects = mAudioPolicyEffects; | 
 | 980 |     } | 
 | 981 |     if (audioPolicyEffects == 0) { | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 982 |         return NO_INIT; | 
 | 983 |     } | 
| Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 984 |  | 
 | 985 |     return OK; | 
 | 986 | } | 
 | 987 |  | 
 | 988 | status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession, | 
 | 989 |                                                        effect_descriptor_t *descriptors, | 
 | 990 |                                                        uint32_t *count) | 
 | 991 | { | 
 | 992 |     sp<AudioPolicyEffects>audioPolicyEffects; | 
 | 993 |     status_t status = getAudioPolicyEffects(audioPolicyEffects); | 
 | 994 |     if (status != OK) { | 
 | 995 |         *count = 0; | 
 | 996 |         return status; | 
 | 997 |     } | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 998 |     return audioPolicyEffects->queryDefaultInputEffects( | 
 | 999 |             (audio_session_t)audioSession, descriptors, count); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1000 | } | 
 | 1001 |  | 
| Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 1002 | status_t AudioPolicyService::addSourceDefaultEffect(const effect_uuid_t *type, | 
 | 1003 |                                                     const String16& opPackageName, | 
 | 1004 |                                                     const effect_uuid_t *uuid, | 
 | 1005 |                                                     int32_t priority, | 
 | 1006 |                                                     audio_source_t source, | 
 | 1007 |                                                     audio_unique_id_t* id) | 
 | 1008 | { | 
 | 1009 |     sp<AudioPolicyEffects>audioPolicyEffects; | 
 | 1010 |     status_t status = getAudioPolicyEffects(audioPolicyEffects); | 
 | 1011 |     if (status != OK) { | 
 | 1012 |         return status; | 
 | 1013 |     } | 
 | 1014 |     if (!modifyDefaultAudioEffectsAllowed()) { | 
 | 1015 |         return PERMISSION_DENIED; | 
 | 1016 |     } | 
 | 1017 |     return audioPolicyEffects->addSourceDefaultEffect( | 
 | 1018 |             type, opPackageName, uuid, priority, source, id); | 
 | 1019 | } | 
 | 1020 |  | 
| Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 1021 | status_t AudioPolicyService::addStreamDefaultEffect(const effect_uuid_t *type, | 
 | 1022 |                                                     const String16& opPackageName, | 
 | 1023 |                                                     const effect_uuid_t *uuid, | 
 | 1024 |                                                     int32_t priority, | 
 | 1025 |                                                     audio_usage_t usage, | 
 | 1026 |                                                     audio_unique_id_t* id) | 
 | 1027 | { | 
| Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 1028 |     sp<AudioPolicyEffects>audioPolicyEffects; | 
 | 1029 |     status_t status = getAudioPolicyEffects(audioPolicyEffects); | 
 | 1030 |     if (status != OK) { | 
 | 1031 |         return status; | 
| Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 1032 |     } | 
 | 1033 |     if (!modifyDefaultAudioEffectsAllowed()) { | 
 | 1034 |         return PERMISSION_DENIED; | 
 | 1035 |     } | 
| Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 1036 |     return audioPolicyEffects->addStreamDefaultEffect( | 
 | 1037 |             type, opPackageName, uuid, priority, usage, id); | 
 | 1038 | } | 
 | 1039 |  | 
| Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 1040 | status_t AudioPolicyService::removeSourceDefaultEffect(audio_unique_id_t id) | 
| Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 1041 | { | 
| Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 1042 |     sp<AudioPolicyEffects>audioPolicyEffects; | 
 | 1043 |     status_t status = getAudioPolicyEffects(audioPolicyEffects); | 
 | 1044 |     if (status != OK) { | 
 | 1045 |         return status; | 
| Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 1046 |     } | 
 | 1047 |     if (!modifyDefaultAudioEffectsAllowed()) { | 
 | 1048 |         return PERMISSION_DENIED; | 
 | 1049 |     } | 
| Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 1050 |     return audioPolicyEffects->removeSourceDefaultEffect(id); | 
 | 1051 | } | 
 | 1052 |  | 
 | 1053 | status_t AudioPolicyService::removeStreamDefaultEffect(audio_unique_id_t id) | 
 | 1054 | { | 
| Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 1055 |     sp<AudioPolicyEffects>audioPolicyEffects; | 
| Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 1056 |     status_t status = getAudioPolicyEffects(audioPolicyEffects); | 
 | 1057 |     if (status != OK) { | 
 | 1058 |         return status; | 
| Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 1059 |     } | 
| Ari Hausman-Cohen | 2462831 | 2018-08-13 15:01:09 -0700 | [diff] [blame] | 1060 |     if (!modifyDefaultAudioEffectsAllowed()) { | 
 | 1061 |         return PERMISSION_DENIED; | 
| Ari Hausman-Cohen | 433722e | 2018-04-24 14:25:22 -0700 | [diff] [blame] | 1062 |     } | 
 | 1063 |     return audioPolicyEffects->removeStreamDefaultEffect(id); | 
 | 1064 | } | 
 | 1065 |  | 
| Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 1066 | status_t AudioPolicyService::setSupportedSystemUsages(const std::vector<audio_usage_t>& systemUsages) { | 
 | 1067 |     Mutex::Autolock _l(mLock); | 
 | 1068 |     if(!modifyAudioRoutingAllowed()) { | 
 | 1069 |         return PERMISSION_DENIED; | 
 | 1070 |     } | 
 | 1071 |  | 
 | 1072 |     bool areAllSystemUsages = std::all_of(begin(systemUsages), end(systemUsages), | 
 | 1073 |         [](audio_usage_t usage) { return isSystemUsage(usage); }); | 
 | 1074 |     if (!areAllSystemUsages) { | 
 | 1075 |         return BAD_VALUE; | 
 | 1076 |     } | 
 | 1077 |  | 
 | 1078 |     mSupportedSystemUsages = systemUsages; | 
 | 1079 |     return NO_ERROR; | 
 | 1080 | } | 
 | 1081 |  | 
| Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1082 | status_t AudioPolicyService::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) { | 
 | 1083 |     Mutex::Autolock _l(mLock); | 
 | 1084 |     if (mAudioPolicyManager == NULL) { | 
 | 1085 |         ALOGV("%s() mAudioPolicyManager == NULL", __func__); | 
 | 1086 |         return NO_INIT; | 
 | 1087 |     } | 
| Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1088 |     return mAudioPolicyManager->setAllowedCapturePolicy(uid, capturePolicy); | 
 | 1089 | } | 
 | 1090 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1091 | bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info) | 
 | 1092 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 1093 |     if (mAudioPolicyManager == NULL) { | 
 | 1094 |         ALOGV("mAudioPolicyManager == NULL"); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1095 |         return false; | 
 | 1096 |     } | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1097 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1098 |     AutoCallerClear acc; | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 1099 |     return mAudioPolicyManager->isOffloadSupported(info); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1100 | } | 
 | 1101 |  | 
| Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1102 | bool AudioPolicyService::isDirectOutputSupported(const audio_config_base_t& config, | 
 | 1103 |                                                  const audio_attributes_t& attributes) { | 
 | 1104 |     if (mAudioPolicyManager == NULL) { | 
 | 1105 |         ALOGV("mAudioPolicyManager == NULL"); | 
 | 1106 |         return false; | 
 | 1107 |     } | 
| Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 1108 |  | 
 | 1109 |     status_t result = validateUsage(attributes.usage); | 
 | 1110 |     if (result != NO_ERROR) { | 
 | 1111 |         return result; | 
 | 1112 |     } | 
 | 1113 |  | 
| Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1114 |     Mutex::Autolock _l(mLock); | 
 | 1115 |     return mAudioPolicyManager->isDirectOutputSupported(config, attributes); | 
 | 1116 | } | 
 | 1117 |  | 
 | 1118 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1119 | status_t AudioPolicyService::listAudioPorts(audio_port_role_t role, | 
 | 1120 |                                             audio_port_type_t type, | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1121 |                                             unsigned int *num_ports, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1122 |                                             struct audio_port *ports, | 
 | 1123 |                                             unsigned int *generation) | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1124 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1125 |     Mutex::Autolock _l(mLock); | 
 | 1126 |     if (mAudioPolicyManager == NULL) { | 
 | 1127 |         return NO_INIT; | 
 | 1128 |     } | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1129 |     AutoCallerClear acc; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1130 |     return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation); | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1131 | } | 
 | 1132 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1133 | status_t AudioPolicyService::getAudioPort(struct audio_port *port) | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1134 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1135 |     Mutex::Autolock _l(mLock); | 
 | 1136 |     if (mAudioPolicyManager == NULL) { | 
 | 1137 |         return NO_INIT; | 
 | 1138 |     } | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1139 |     AutoCallerClear acc; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1140 |     return mAudioPolicyManager->getAudioPort(port); | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1141 | } | 
 | 1142 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1143 | status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch, | 
 | 1144 |         audio_patch_handle_t *handle) | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1145 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1146 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 1147 |     if(!modifyAudioRoutingAllowed()) { | 
 | 1148 |         return PERMISSION_DENIED; | 
 | 1149 |     } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1150 |     if (mAudioPolicyManager == NULL) { | 
 | 1151 |         return NO_INIT; | 
 | 1152 |     } | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1153 |     AutoCallerClear acc; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1154 |     return mAudioPolicyManager->createAudioPatch(patch, handle, | 
 | 1155 |                                                   IPCThreadState::self()->getCallingUid()); | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1156 | } | 
 | 1157 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1158 | status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle) | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1159 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1160 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 1161 |     if(!modifyAudioRoutingAllowed()) { | 
 | 1162 |         return PERMISSION_DENIED; | 
 | 1163 |     } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1164 |     if (mAudioPolicyManager == NULL) { | 
 | 1165 |         return NO_INIT; | 
 | 1166 |     } | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1167 |     AutoCallerClear acc; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1168 |     return mAudioPolicyManager->releaseAudioPatch(handle, | 
 | 1169 |                                                      IPCThreadState::self()->getCallingUid()); | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1170 | } | 
 | 1171 |  | 
 | 1172 | status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1173 |         struct audio_patch *patches, | 
 | 1174 |         unsigned int *generation) | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1175 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1176 |     Mutex::Autolock _l(mLock); | 
 | 1177 |     if (mAudioPolicyManager == NULL) { | 
 | 1178 |         return NO_INIT; | 
 | 1179 |     } | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1180 |     AutoCallerClear acc; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1181 |     return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation); | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1182 | } | 
 | 1183 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1184 | status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config) | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1185 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1186 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 1187 |     if(!modifyAudioRoutingAllowed()) { | 
 | 1188 |         return PERMISSION_DENIED; | 
 | 1189 |     } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1190 |     if (mAudioPolicyManager == NULL) { | 
 | 1191 |         return NO_INIT; | 
 | 1192 |     } | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1193 |     AutoCallerClear acc; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1194 |     return mAudioPolicyManager->setAudioPortConfig(config); | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 1195 | } | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 1196 |  | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1197 | status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session, | 
 | 1198 |                                        audio_io_handle_t *ioHandle, | 
 | 1199 |                                        audio_devices_t *device) | 
 | 1200 | { | 
| Andy Hung | f759b8c | 2017-08-15 12:48:54 -0700 | [diff] [blame] | 1201 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1202 |     if (mAudioPolicyManager == NULL) { | 
 | 1203 |         return NO_INIT; | 
 | 1204 |     } | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1205 |     AutoCallerClear acc; | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1206 |     return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device); | 
 | 1207 | } | 
 | 1208 |  | 
 | 1209 | status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session) | 
 | 1210 | { | 
| Andy Hung | f759b8c | 2017-08-15 12:48:54 -0700 | [diff] [blame] | 1211 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1212 |     if (mAudioPolicyManager == NULL) { | 
 | 1213 |         return NO_INIT; | 
 | 1214 |     } | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1215 |     AutoCallerClear acc; | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1216 |     return mAudioPolicyManager->releaseSoundTriggerSession(session); | 
 | 1217 | } | 
 | 1218 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1219 | status_t AudioPolicyService::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) | 
| Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 1220 | { | 
 | 1221 |     Mutex::Autolock _l(mLock); | 
| Kevin Rocard | be20185 | 2019-02-20 22:33:28 -0800 | [diff] [blame] | 1222 |  | 
 | 1223 |     // loopback|render only need a MediaProjection (checked in caller AudioService.java) | 
 | 1224 |     bool needModifyAudioRouting = std::any_of(mixes.begin(), mixes.end(), [](auto& mix) { | 
 | 1225 |             return !is_mix_loopback_render(mix.mRouteFlags); }); | 
 | 1226 |     if (needModifyAudioRouting && !modifyAudioRoutingAllowed()) { | 
| Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 1227 |         return PERMISSION_DENIED; | 
 | 1228 |     } | 
| Kevin Rocard | be20185 | 2019-02-20 22:33:28 -0800 | [diff] [blame] | 1229 |  | 
| Nadav Bar | 287d330 | 2020-02-05 14:55:38 +0200 | [diff] [blame] | 1230 |     // If one of the mixes has needCaptureVoiceCommunicationOutput set to true, then we | 
 | 1231 |     // need to verify that the caller still has CAPTURE_VOICE_COMMUNICATION_OUTPUT | 
| Nadav Bar | dbf0a2e | 2020-01-16 23:09:25 +0200 | [diff] [blame] | 1232 |     bool needCaptureVoiceCommunicationOutput = | 
 | 1233 |         std::any_of(mixes.begin(), mixes.end(), [](auto& mix) { | 
| Nadav Bar | 287d330 | 2020-02-05 14:55:38 +0200 | [diff] [blame] | 1234 |             return mix.mVoiceCommunicationCaptureAllowed; }); | 
| Nadav Bar | dbf0a2e | 2020-01-16 23:09:25 +0200 | [diff] [blame] | 1235 |  | 
| Kevin Rocard | 36b1755 | 2019-03-07 18:48:07 -0800 | [diff] [blame] | 1236 |     bool needCaptureMediaOutput = std::any_of(mixes.begin(), mixes.end(), [](auto& mix) { | 
| Nadav Bar | 287d330 | 2020-02-05 14:55:38 +0200 | [diff] [blame] | 1237 |             return mix.mAllowPrivilegedPlaybackCapture; }); | 
| Nadav Bar | dbf0a2e | 2020-01-16 23:09:25 +0200 | [diff] [blame] | 1238 |  | 
| Kevin Rocard | 36b1755 | 2019-03-07 18:48:07 -0800 | [diff] [blame] | 1239 |     const uid_t callingUid = IPCThreadState::self()->getCallingUid(); | 
 | 1240 |     const pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
| Nadav Bar | dbf0a2e | 2020-01-16 23:09:25 +0200 | [diff] [blame] | 1241 |  | 
| Kevin Rocard | 36b1755 | 2019-03-07 18:48:07 -0800 | [diff] [blame] | 1242 |     if (needCaptureMediaOutput && !captureMediaOutputAllowed(callingPid, callingUid)) { | 
 | 1243 |         return PERMISSION_DENIED; | 
 | 1244 |     } | 
 | 1245 |  | 
| Nadav Bar | dbf0a2e | 2020-01-16 23:09:25 +0200 | [diff] [blame] | 1246 |     if (needCaptureVoiceCommunicationOutput && | 
 | 1247 |         !captureVoiceCommunicationOutputAllowed(callingPid, callingUid)) { | 
 | 1248 |         return PERMISSION_DENIED; | 
 | 1249 |     } | 
 | 1250 |  | 
| Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 1251 |     if (mAudioPolicyManager == NULL) { | 
 | 1252 |         return NO_INIT; | 
 | 1253 |     } | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1254 |     AutoCallerClear acc; | 
| Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 1255 |     if (registration) { | 
 | 1256 |         return mAudioPolicyManager->registerPolicyMixes(mixes); | 
 | 1257 |     } else { | 
 | 1258 |         return mAudioPolicyManager->unregisterPolicyMixes(mixes); | 
 | 1259 |     } | 
 | 1260 | } | 
 | 1261 |  | 
| Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 1262 | status_t AudioPolicyService::setUidDeviceAffinities(uid_t uid, | 
 | 1263 |         const Vector<AudioDeviceTypeAddr>& devices) { | 
 | 1264 |     Mutex::Autolock _l(mLock); | 
 | 1265 |     if(!modifyAudioRoutingAllowed()) { | 
 | 1266 |         return PERMISSION_DENIED; | 
 | 1267 |     } | 
 | 1268 |     if (mAudioPolicyManager == NULL) { | 
 | 1269 |         return NO_INIT; | 
 | 1270 |     } | 
 | 1271 |     AutoCallerClear acc; | 
 | 1272 |     return mAudioPolicyManager->setUidDeviceAffinities(uid, devices); | 
 | 1273 | } | 
 | 1274 |  | 
 | 1275 | status_t AudioPolicyService::removeUidDeviceAffinities(uid_t uid) { | 
 | 1276 |     Mutex::Autolock _l(mLock); | 
 | 1277 |     if(!modifyAudioRoutingAllowed()) { | 
 | 1278 |         return PERMISSION_DENIED; | 
 | 1279 |     } | 
 | 1280 |     if (mAudioPolicyManager == NULL) { | 
 | 1281 |         return NO_INIT; | 
 | 1282 |     } | 
 | 1283 |     AutoCallerClear acc; | 
 | 1284 |     return mAudioPolicyManager->removeUidDeviceAffinities(uid); | 
 | 1285 | } | 
 | 1286 |  | 
| Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 1287 | status_t AudioPolicyService::setUserIdDeviceAffinities(int userId, | 
 | 1288 |         const Vector<AudioDeviceTypeAddr>& devices) { | 
 | 1289 |     Mutex::Autolock _l(mLock); | 
 | 1290 |     if(!modifyAudioRoutingAllowed()) { | 
 | 1291 |         return PERMISSION_DENIED; | 
 | 1292 |     } | 
 | 1293 |     if (mAudioPolicyManager == NULL) { | 
 | 1294 |         return NO_INIT; | 
 | 1295 |     } | 
 | 1296 |     AutoCallerClear acc; | 
 | 1297 |     return mAudioPolicyManager->setUserIdDeviceAffinities(userId, devices); | 
 | 1298 | } | 
 | 1299 |  | 
 | 1300 | status_t AudioPolicyService::removeUserIdDeviceAffinities(int userId) { | 
 | 1301 |     Mutex::Autolock _l(mLock); | 
 | 1302 |     if(!modifyAudioRoutingAllowed()) { | 
 | 1303 |         return PERMISSION_DENIED; | 
 | 1304 |     } | 
 | 1305 |     if (mAudioPolicyManager == NULL) { | 
 | 1306 |         return NO_INIT; | 
 | 1307 |     } | 
 | 1308 |     AutoCallerClear acc; | 
 | 1309 |     return mAudioPolicyManager->removeUserIdDeviceAffinities(userId); | 
 | 1310 | } | 
 | 1311 |  | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1312 | status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source, | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 1313 |                                               const audio_attributes_t *attributes, | 
 | 1314 |                                               audio_port_handle_t *portId) | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1315 | { | 
 | 1316 |     Mutex::Autolock _l(mLock); | 
 | 1317 |     if (mAudioPolicyManager == NULL) { | 
 | 1318 |         return NO_INIT; | 
 | 1319 |     } | 
| Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 1320 |  | 
 | 1321 |     status_t result = validateUsage(attributes->usage); | 
 | 1322 |     if (result != NO_ERROR) { | 
 | 1323 |         return result; | 
 | 1324 |     } | 
 | 1325 |  | 
| Hongwei Wang | 5cd1f1d | 2019-03-26 15:21:11 -0700 | [diff] [blame] | 1326 |     // startAudioSource should be created as the calling uid | 
 | 1327 |     const uid_t callingUid = IPCThreadState::self()->getCallingUid(); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1328 |     AutoCallerClear acc; | 
| Hongwei Wang | 5cd1f1d | 2019-03-26 15:21:11 -0700 | [diff] [blame] | 1329 |     return mAudioPolicyManager->startAudioSource(source, attributes, portId, callingUid); | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1330 | } | 
 | 1331 |  | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 1332 | status_t AudioPolicyService::stopAudioSource(audio_port_handle_t portId) | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1333 | { | 
 | 1334 |     Mutex::Autolock _l(mLock); | 
 | 1335 |     if (mAudioPolicyManager == NULL) { | 
 | 1336 |         return NO_INIT; | 
 | 1337 |     } | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1338 |     AutoCallerClear acc; | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 1339 |     return mAudioPolicyManager->stopAudioSource(portId); | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 1340 | } | 
 | 1341 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1342 | status_t AudioPolicyService::setMasterMono(bool mono) | 
 | 1343 | { | 
 | 1344 |     if (mAudioPolicyManager == NULL) { | 
 | 1345 |         return NO_INIT; | 
 | 1346 |     } | 
 | 1347 |     if (!settingsAllowed()) { | 
 | 1348 |         return PERMISSION_DENIED; | 
 | 1349 |     } | 
 | 1350 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1351 |     AutoCallerClear acc; | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1352 |     return mAudioPolicyManager->setMasterMono(mono); | 
 | 1353 | } | 
 | 1354 |  | 
 | 1355 | status_t AudioPolicyService::getMasterMono(bool *mono) | 
 | 1356 | { | 
 | 1357 |     if (mAudioPolicyManager == NULL) { | 
 | 1358 |         return NO_INIT; | 
 | 1359 |     } | 
 | 1360 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1361 |     AutoCallerClear acc; | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1362 |     return mAudioPolicyManager->getMasterMono(mono); | 
 | 1363 | } | 
 | 1364 |  | 
| Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 1365 |  | 
 | 1366 | float AudioPolicyService::getStreamVolumeDB( | 
 | 1367 |             audio_stream_type_t stream, int index, audio_devices_t device) | 
 | 1368 | { | 
 | 1369 |     if (mAudioPolicyManager == NULL) { | 
 | 1370 |         return NAN; | 
 | 1371 |     } | 
 | 1372 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 10b7123 | 2018-04-13 18:14:44 -0700 | [diff] [blame] | 1373 |     AutoCallerClear acc; | 
| Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 1374 |     return mAudioPolicyManager->getStreamVolumeDB(stream, index, device); | 
 | 1375 | } | 
 | 1376 |  | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 1377 | status_t AudioPolicyService::getSurroundFormats(unsigned int *numSurroundFormats, | 
 | 1378 |                                                 audio_format_t *surroundFormats, | 
 | 1379 |                                                 bool *surroundFormatsEnabled, | 
 | 1380 |                                                 bool reported) | 
 | 1381 | { | 
 | 1382 |     if (mAudioPolicyManager == NULL) { | 
 | 1383 |         return NO_INIT; | 
 | 1384 |     } | 
 | 1385 |     Mutex::Autolock _l(mLock); | 
 | 1386 |     AutoCallerClear acc; | 
 | 1387 |     return mAudioPolicyManager->getSurroundFormats(numSurroundFormats, surroundFormats, | 
 | 1388 |                                                    surroundFormatsEnabled, reported); | 
 | 1389 | } | 
 | 1390 |  | 
| Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 1391 | status_t AudioPolicyService::getHwOffloadEncodingFormatsSupportedForA2DP( | 
 | 1392 |                                         std::vector<audio_format_t> *formats) | 
 | 1393 | { | 
 | 1394 |     if (mAudioPolicyManager == NULL) { | 
 | 1395 |         return NO_INIT; | 
 | 1396 |     } | 
 | 1397 |     Mutex::Autolock _l(mLock); | 
 | 1398 |     AutoCallerClear acc; | 
 | 1399 |     return mAudioPolicyManager->getHwOffloadEncodingFormatsSupportedForA2DP(formats); | 
 | 1400 | } | 
 | 1401 |  | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 1402 | status_t AudioPolicyService::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) | 
 | 1403 | { | 
 | 1404 |     if (mAudioPolicyManager == NULL) { | 
 | 1405 |         return NO_INIT; | 
 | 1406 |     } | 
 | 1407 |     Mutex::Autolock _l(mLock); | 
 | 1408 |     AutoCallerClear acc; | 
 | 1409 |     return mAudioPolicyManager->setSurroundFormatEnabled(audioFormat, enabled); | 
 | 1410 | } | 
| Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 1411 |  | 
| Eric Laurent | b78763e | 2018-10-17 10:08:02 -0700 | [diff] [blame] | 1412 | status_t AudioPolicyService::setAssistantUid(uid_t uid) | 
 | 1413 | { | 
 | 1414 |     Mutex::Autolock _l(mLock); | 
 | 1415 |     mUidPolicy->setAssistantUid(uid); | 
 | 1416 |     return NO_ERROR; | 
 | 1417 | } | 
 | 1418 |  | 
 | 1419 | status_t AudioPolicyService::setA11yServicesUids(const std::vector<uid_t>& uids) | 
 | 1420 | { | 
 | 1421 |     Mutex::Autolock _l(mLock); | 
 | 1422 |     mUidPolicy->setA11yUids(uids); | 
 | 1423 |     return NO_ERROR; | 
 | 1424 | } | 
 | 1425 |  | 
| Kohsuke Yatoh | a623a13 | 2020-03-24 20:10:26 -0700 | [diff] [blame] | 1426 | status_t AudioPolicyService::setCurrentImeUid(uid_t uid) | 
 | 1427 | { | 
 | 1428 |     Mutex::Autolock _l(mLock); | 
 | 1429 |     mUidPolicy->setCurrentImeUid(uid); | 
 | 1430 |     return NO_ERROR; | 
 | 1431 | } | 
 | 1432 |  | 
| jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 1433 | bool AudioPolicyService::isHapticPlaybackSupported() | 
 | 1434 | { | 
 | 1435 |     if (mAudioPolicyManager == NULL) { | 
 | 1436 |         ALOGW("%s, mAudioPolicyManager == NULL", __func__); | 
 | 1437 |         return false; | 
 | 1438 |     } | 
 | 1439 |     Mutex::Autolock _l(mLock); | 
 | 1440 |     AutoCallerClear acc; | 
 | 1441 |     return mAudioPolicyManager->isHapticPlaybackSupported(); | 
 | 1442 | } | 
 | 1443 |  | 
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1444 | status_t AudioPolicyService::listAudioProductStrategies(AudioProductStrategyVector &strategies) | 
 | 1445 | { | 
 | 1446 |     if (mAudioPolicyManager == NULL) { | 
 | 1447 |         return NO_INIT; | 
 | 1448 |     } | 
 | 1449 |     Mutex::Autolock _l(mLock); | 
 | 1450 |     return mAudioPolicyManager->listAudioProductStrategies(strategies); | 
 | 1451 | } | 
 | 1452 |  | 
| François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 1453 | status_t AudioPolicyService::getProductStrategyFromAudioAttributes( | 
 | 1454 |         const AudioAttributes &aa, product_strategy_t &productStrategy) | 
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1455 | { | 
 | 1456 |     if (mAudioPolicyManager == NULL) { | 
| François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 1457 |         return NO_INIT; | 
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1458 |     } | 
 | 1459 |     Mutex::Autolock _l(mLock); | 
| François Gaffie | 4b2018b | 2018-11-07 11:18:59 +0100 | [diff] [blame] | 1460 |     return mAudioPolicyManager->getProductStrategyFromAudioAttributes(aa, productStrategy); | 
 | 1461 | } | 
 | 1462 |  | 
 | 1463 | status_t AudioPolicyService::listAudioVolumeGroups(AudioVolumeGroupVector &groups) | 
 | 1464 | { | 
 | 1465 |     if (mAudioPolicyManager == NULL) { | 
 | 1466 |         return NO_INIT; | 
 | 1467 |     } | 
 | 1468 |     Mutex::Autolock _l(mLock); | 
 | 1469 |     return mAudioPolicyManager->listAudioVolumeGroups(groups); | 
 | 1470 | } | 
 | 1471 |  | 
 | 1472 | status_t AudioPolicyService::getVolumeGroupFromAudioAttributes(const AudioAttributes &aa, | 
 | 1473 |                                                                volume_group_t &volumeGroup) | 
 | 1474 | { | 
 | 1475 |     if (mAudioPolicyManager == NULL) { | 
 | 1476 |         return NO_INIT; | 
 | 1477 |     } | 
 | 1478 |     Mutex::Autolock _l(mLock); | 
 | 1479 |     return mAudioPolicyManager->getVolumeGroupFromAudioAttributes(aa, volumeGroup); | 
| François Gaffie | d0ba9ed | 2018-11-05 11:50:42 +0100 | [diff] [blame] | 1480 | } | 
| Eric Laurent | 6ede98f | 2019-06-11 14:50:30 -0700 | [diff] [blame] | 1481 |  | 
 | 1482 | status_t AudioPolicyService::setRttEnabled(bool enabled) | 
 | 1483 | { | 
 | 1484 |     Mutex::Autolock _l(mLock); | 
 | 1485 |     mUidPolicy->setRttEnabled(enabled); | 
 | 1486 |     return NO_ERROR; | 
 | 1487 | } | 
 | 1488 |  | 
| Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 1489 | bool AudioPolicyService::isCallScreenModeSupported() | 
 | 1490 | { | 
 | 1491 |     if (mAudioPolicyManager == NULL) { | 
 | 1492 |         ALOGW("%s, mAudioPolicyManager == NULL", __func__); | 
 | 1493 |         return false; | 
 | 1494 |     } | 
 | 1495 |     Mutex::Autolock _l(mLock); | 
 | 1496 |     AutoCallerClear acc; | 
 | 1497 |     return mAudioPolicyManager->isCallScreenModeSupported(); | 
 | 1498 | } | 
 | 1499 |  | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 1500 | status_t AudioPolicyService::setPreferredDeviceForStrategy(product_strategy_t strategy, | 
 | 1501 |                                                    const AudioDeviceTypeAddr &device) | 
 | 1502 | { | 
 | 1503 |     if (mAudioPolicyManager == NULL) { | 
 | 1504 |         return NO_INIT; | 
 | 1505 |     } | 
 | 1506 |     Mutex::Autolock _l(mLock); | 
 | 1507 |     return mAudioPolicyManager->setPreferredDeviceForStrategy(strategy, device); | 
 | 1508 | } | 
 | 1509 |  | 
 | 1510 | status_t AudioPolicyService::removePreferredDeviceForStrategy(product_strategy_t strategy) | 
 | 1511 | { | 
 | 1512 |     if (mAudioPolicyManager == NULL) { | 
 | 1513 |         return NO_INIT; | 
 | 1514 |     } | 
 | 1515 |     Mutex::Autolock _l(mLock); | 
 | 1516 |     return mAudioPolicyManager->removePreferredDeviceForStrategy(strategy); | 
 | 1517 | } | 
 | 1518 |  | 
 | 1519 | status_t AudioPolicyService::getPreferredDeviceForStrategy(product_strategy_t strategy, | 
 | 1520 |                                                    AudioDeviceTypeAddr &device) | 
 | 1521 | { | 
 | 1522 |     if (mAudioPolicyManager == NULL) { | 
 | 1523 |         return NO_INIT; | 
 | 1524 |     } | 
 | 1525 |     Mutex::Autolock _l(mLock); | 
 | 1526 |     return mAudioPolicyManager->getPreferredDeviceForStrategy(strategy, device); | 
 | 1527 | } | 
 | 1528 |  | 
| Ytai Ben-Tsvi | 85093d5 | 2020-03-26 09:41:15 -0700 | [diff] [blame] | 1529 | status_t AudioPolicyService::registerSoundTriggerCaptureStateListener( | 
 | 1530 |     const sp<media::ICaptureStateListener>& listener, | 
 | 1531 |     bool* result) | 
 | 1532 | { | 
 | 1533 |     *result = mCaptureStateNotifier.RegisterListener(listener); | 
 | 1534 |     return NO_ERROR; | 
 | 1535 | } | 
 | 1536 |  | 
| Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 1537 | } // namespace android |