| 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 |  | 
 | 20 | #include <utils/Log.h> | 
 | 21 | #include "AudioPolicyService.h" | 
 | 22 | #include "ServiceUtilities.h" | 
 | 23 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 24 | namespace android { | 
 | 25 |  | 
 | 26 |  | 
 | 27 | // ---------------------------------------------------------------------------- | 
 | 28 |  | 
 | 29 | status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device, | 
 | 30 |                                                   audio_policy_dev_state_t state, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 31 |                                                   const char *device_address, | 
 | 32 |                                                   const char *device_name) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 33 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 34 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 35 |         return NO_INIT; | 
 | 36 |     } | 
 | 37 |     if (!settingsAllowed()) { | 
 | 38 |         return PERMISSION_DENIED; | 
 | 39 |     } | 
 | 40 |     if (!audio_is_output_device(device) && !audio_is_input_device(device)) { | 
 | 41 |         return BAD_VALUE; | 
 | 42 |     } | 
 | 43 |     if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE && | 
 | 44 |             state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
 | 45 |         return BAD_VALUE; | 
 | 46 |     } | 
 | 47 |  | 
 | 48 |     ALOGV("setDeviceConnectionState()"); | 
 | 49 |     Mutex::Autolock _l(mLock); | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 50 |     return mAudioPolicyManager->setDeviceConnectionState(device, state, | 
 | 51 |                                                          device_address, device_name); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 52 | } | 
 | 53 |  | 
 | 54 | audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState( | 
 | 55 |                                                               audio_devices_t device, | 
 | 56 |                                                               const char *device_address) | 
 | 57 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 58 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 59 |         return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
 | 60 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 61 |     return mAudioPolicyManager->getDeviceConnectionState(device, | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 62 |                                                       device_address); | 
 | 63 | } | 
 | 64 |  | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 65 | status_t AudioPolicyService::handleDeviceConfigChange(audio_devices_t device, | 
 | 66 |                                                   const char *device_address, | 
 | 67 |                                                   const char *device_name) | 
 | 68 | { | 
 | 69 |     if (mAudioPolicyManager == NULL) { | 
 | 70 |         return NO_INIT; | 
 | 71 |     } | 
 | 72 |     if (!settingsAllowed()) { | 
 | 73 |         return PERMISSION_DENIED; | 
 | 74 |     } | 
 | 75 |     if (!audio_is_output_device(device) && !audio_is_input_device(device)) { | 
 | 76 |         return BAD_VALUE; | 
 | 77 |     } | 
 | 78 |  | 
 | 79 |     ALOGV("handleDeviceConfigChange()"); | 
 | 80 |     Mutex::Autolock _l(mLock); | 
 | 81 |     return mAudioPolicyManager->handleDeviceConfigChange(device, device_address, | 
 | 82 |                                                          device_name); | 
 | 83 | } | 
 | 84 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 85 | status_t AudioPolicyService::setPhoneState(audio_mode_t state) | 
 | 86 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 87 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 88 |         return NO_INIT; | 
 | 89 |     } | 
 | 90 |     if (!settingsAllowed()) { | 
 | 91 |         return PERMISSION_DENIED; | 
 | 92 |     } | 
 | 93 |     if (uint32_t(state) >= AUDIO_MODE_CNT) { | 
 | 94 |         return BAD_VALUE; | 
 | 95 |     } | 
 | 96 |  | 
 | 97 |     ALOGV("setPhoneState()"); | 
 | 98 |  | 
| Eric Laurent | beb07fe | 2015-09-16 15:49:30 -0700 | [diff] [blame] | 99 |     // acquire lock before calling setMode() so that setMode() + setPhoneState() are an atomic | 
 | 100 |     // operation from policy manager standpoint (no other operation (e.g track start or stop) | 
 | 101 |     // can be interleaved). | 
 | 102 |     Mutex::Autolock _l(mLock); | 
 | 103 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 104 |     // TODO: check if it is more appropriate to do it in platform specific policy manager | 
 | 105 |     AudioSystem::setMode(state); | 
 | 106 |  | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 107 |     mAudioPolicyManager->setPhoneState(state); | 
| Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 108 |     mPhoneState = state; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 109 |     return NO_ERROR; | 
 | 110 | } | 
 | 111 |  | 
| Eric Laurent | bb6c9a0 | 2014-09-25 14:11:47 -0700 | [diff] [blame] | 112 | audio_mode_t AudioPolicyService::getPhoneState() | 
 | 113 | { | 
 | 114 |     Mutex::Autolock _l(mLock); | 
 | 115 |     return mPhoneState; | 
 | 116 | } | 
 | 117 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 118 | status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage, | 
 | 119 |                                          audio_policy_forced_cfg_t config) | 
 | 120 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 121 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 122 |         return NO_INIT; | 
 | 123 |     } | 
 | 124 |     if (!settingsAllowed()) { | 
 | 125 |         return PERMISSION_DENIED; | 
 | 126 |     } | 
 | 127 |     if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { | 
 | 128 |         return BAD_VALUE; | 
 | 129 |     } | 
 | 130 |     if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) { | 
 | 131 |         return BAD_VALUE; | 
 | 132 |     } | 
 | 133 |     ALOGV("setForceUse()"); | 
 | 134 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 135 |     mAudioPolicyManager->setForceUse(usage, config); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 136 |     return NO_ERROR; | 
 | 137 | } | 
 | 138 |  | 
 | 139 | audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage) | 
 | 140 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 141 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 142 |         return AUDIO_POLICY_FORCE_NONE; | 
 | 143 |     } | 
 | 144 |     if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) { | 
 | 145 |         return AUDIO_POLICY_FORCE_NONE; | 
 | 146 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 147 |     return mAudioPolicyManager->getForceUse(usage); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 148 | } | 
 | 149 |  | 
 | 150 | audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream, | 
 | 151 |                                     uint32_t samplingRate, | 
 | 152 |                                     audio_format_t format, | 
 | 153 |                                     audio_channel_mask_t channelMask, | 
 | 154 |                                     audio_output_flags_t flags, | 
 | 155 |                                     const audio_offload_info_t *offloadInfo) | 
 | 156 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 157 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 158 |         return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | dea1541 | 2014-10-28 15:46:45 -0700 | [diff] [blame] | 159 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 160 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 161 |         return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 162 |     } | 
 | 163 |     ALOGV("getOutput()"); | 
 | 164 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 165 |     return mAudioPolicyManager->getOutput(stream, samplingRate, | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 166 |                                     format, channelMask, flags, offloadInfo); | 
 | 167 | } | 
 | 168 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 169 | status_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr, | 
 | 170 |                                               audio_io_handle_t *output, | 
 | 171 |                                               audio_session_t session, | 
 | 172 |                                               audio_stream_type_t *stream, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 173 |                                               uid_t uid, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 174 |                                               uint32_t samplingRate, | 
 | 175 |                                               audio_format_t format, | 
 | 176 |                                               audio_channel_mask_t channelMask, | 
 | 177 |                                               audio_output_flags_t flags, | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 178 |                                               audio_port_handle_t selectedDeviceId, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 179 |                                               const audio_offload_info_t *offloadInfo) | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 180 | { | 
 | 181 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 182 |         return NO_INIT; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 183 |     } | 
 | 184 |     ALOGV("getOutput()"); | 
 | 185 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 186 |  | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 187 |     const uid_t callingUid = IPCThreadState::self()->getCallingUid(); | 
 | 188 |     if (!isTrustedCallingUid(callingUid) || uid == (uid_t)-1) { | 
 | 189 |         ALOGW_IF(uid != (uid_t)-1 && uid != callingUid, | 
 | 190 |                 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid); | 
 | 191 |         uid = callingUid; | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 192 |     } | 
 | 193 |     return mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid, samplingRate, | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 194 |                                     format, channelMask, flags, selectedDeviceId, offloadInfo); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 195 | } | 
 | 196 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 197 | status_t AudioPolicyService::startOutput(audio_io_handle_t output, | 
 | 198 |                                          audio_stream_type_t stream, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 199 |                                          audio_session_t session) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 200 | { | 
| Eric Laurent | dea1541 | 2014-10-28 15:46:45 -0700 | [diff] [blame] | 201 |     if (uint32_t(stream) >= AUDIO_STREAM_CNT) { | 
 | 202 |         return BAD_VALUE; | 
 | 203 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 204 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 205 |         return NO_INIT; | 
 | 206 |     } | 
 | 207 |     ALOGV("startOutput()"); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 208 |     sp<AudioPolicyEffects>audioPolicyEffects; | 
 | 209 |     { | 
 | 210 |         Mutex::Autolock _l(mLock); | 
 | 211 |         audioPolicyEffects = mAudioPolicyEffects; | 
| bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 212 |     } | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 213 |     if (audioPolicyEffects != 0) { | 
 | 214 |         // create audio processors according to stream | 
 | 215 |         status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session); | 
 | 216 |         if (status != NO_ERROR && status != ALREADY_EXISTS) { | 
 | 217 |             ALOGW("Failed to add effects on session %d", session); | 
 | 218 |         } | 
 | 219 |     } | 
 | 220 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 221 |     return mAudioPolicyManager->startOutput(output, stream, session); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 222 | } | 
 | 223 |  | 
 | 224 | status_t AudioPolicyService::stopOutput(audio_io_handle_t output, | 
 | 225 |                                         audio_stream_type_t stream, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 226 |                                         audio_session_t session) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 227 | { | 
| Eric Laurent | dea1541 | 2014-10-28 15:46:45 -0700 | [diff] [blame] | 228 |     if (uint32_t(stream) >= AUDIO_STREAM_CNT) { | 
 | 229 |         return BAD_VALUE; | 
 | 230 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 231 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 232 |         return NO_INIT; | 
 | 233 |     } | 
 | 234 |     ALOGV("stopOutput()"); | 
 | 235 |     mOutputCommandThread->stopOutputCommand(output, stream, session); | 
 | 236 |     return NO_ERROR; | 
 | 237 | } | 
 | 238 |  | 
 | 239 | status_t  AudioPolicyService::doStopOutput(audio_io_handle_t output, | 
 | 240 |                                       audio_stream_type_t stream, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 241 |                                       audio_session_t session) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 242 | { | 
 | 243 |     ALOGV("doStopOutput from tid %d", gettid()); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 244 |     sp<AudioPolicyEffects>audioPolicyEffects; | 
 | 245 |     { | 
 | 246 |         Mutex::Autolock _l(mLock); | 
 | 247 |         audioPolicyEffects = mAudioPolicyEffects; | 
| bryant_liu | ba2b439 | 2014-06-11 16:49:30 +0800 | [diff] [blame] | 248 |     } | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 249 |     if (audioPolicyEffects != 0) { | 
 | 250 |         // release audio processors from the stream | 
 | 251 |         status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session); | 
 | 252 |         if (status != NO_ERROR && status != ALREADY_EXISTS) { | 
 | 253 |             ALOGW("Failed to release effects on session %d", session); | 
 | 254 |         } | 
 | 255 |     } | 
 | 256 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 257 |     return mAudioPolicyManager->stopOutput(output, stream, session); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 258 | } | 
 | 259 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 260 | void AudioPolicyService::releaseOutput(audio_io_handle_t output, | 
 | 261 |                                        audio_stream_type_t stream, | 
 | 262 |                                        audio_session_t session) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 263 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 264 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 265 |         return; | 
 | 266 |     } | 
 | 267 |     ALOGV("releaseOutput()"); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 268 |     mOutputCommandThread->releaseOutputCommand(output, stream, session); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 269 | } | 
 | 270 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 271 | void AudioPolicyService::doReleaseOutput(audio_io_handle_t output, | 
 | 272 |                                          audio_stream_type_t stream, | 
 | 273 |                                          audio_session_t session) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 274 | { | 
 | 275 |     ALOGV("doReleaseOutput from tid %d", gettid()); | 
 | 276 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 277 |     mAudioPolicyManager->releaseOutput(output, stream, session); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 278 | } | 
 | 279 |  | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 280 | status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr, | 
 | 281 |                                              audio_io_handle_t *input, | 
 | 282 |                                              audio_session_t session, | 
| Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 283 |                                              pid_t pid, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 284 |                                              uid_t uid, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 285 |                                              uint32_t samplingRate, | 
 | 286 |                                              audio_format_t format, | 
 | 287 |                                              audio_channel_mask_t channelMask, | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 288 |                                              audio_input_flags_t flags, | 
 | 289 |                                              audio_port_handle_t selectedDeviceId) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 290 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 291 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 292 |         return NO_INIT; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 293 |     } | 
 | 294 |     // already checked by client, but double-check in case the client wrapper is bypassed | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 295 |     if (attr->source >= AUDIO_SOURCE_CNT && attr->source != AUDIO_SOURCE_HOTWORD && | 
 | 296 |         attr->source != AUDIO_SOURCE_FM_TUNER) { | 
 | 297 |         return BAD_VALUE; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 298 |     } | 
 | 299 |  | 
| Eric Laurent | ab300c8 | 2015-04-13 13:47:33 -0700 | [diff] [blame] | 300 |     if ((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) { | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 301 |         return BAD_VALUE; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 302 |     } | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 303 |     sp<AudioPolicyEffects>audioPolicyEffects; | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 304 |     status_t status; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 305 |     AudioPolicyInterface::input_type_t inputType; | 
| Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 306 |  | 
 | 307 |     bool updatePid = (pid == -1); | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 308 |     const uid_t callingUid = IPCThreadState::self()->getCallingUid(); | 
| Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 309 |     if (!isTrustedCallingUid(callingUid)) { | 
| Eric Laurent | 9f39f8d | 2016-05-25 12:34:48 -0700 | [diff] [blame] | 310 |         ALOGW_IF(uid != (uid_t)-1 && uid != callingUid, | 
| Marco Nelissen | dcb346b | 2015-09-09 10:47:29 -0700 | [diff] [blame] | 311 |                 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid); | 
 | 312 |         uid = callingUid; | 
| Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 313 |         updatePid = true; | 
 | 314 |     } | 
 | 315 |  | 
 | 316 |     if (updatePid) { | 
 | 317 |         const pid_t callingPid = IPCThreadState::self()->getCallingPid(); | 
| Eric Laurent | 9f39f8d | 2016-05-25 12:34:48 -0700 | [diff] [blame] | 318 |         ALOGW_IF(pid != (pid_t)-1 && pid != callingPid, | 
| Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 319 |                  "%s uid %d pid %d tried to pass itself off as pid %d", | 
 | 320 |                  __func__, callingUid, callingPid, pid); | 
 | 321 |         pid = callingPid; | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 322 |     } | 
 | 323 |  | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 324 |     { | 
 | 325 |         Mutex::Autolock _l(mLock); | 
 | 326 |         // the audio_in_acoustics_t parameter is ignored by get_input() | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 327 |         status = mAudioPolicyManager->getInputForAttr(attr, input, session, uid, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 328 |                                                      samplingRate, format, channelMask, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 329 |                                                      flags, selectedDeviceId, | 
 | 330 |                                                      &inputType); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 331 |         audioPolicyEffects = mAudioPolicyEffects; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 332 |  | 
 | 333 |         if (status == NO_ERROR) { | 
 | 334 |             // enforce permission (if any) required for each type of input | 
 | 335 |             switch (inputType) { | 
 | 336 |             case AudioPolicyInterface::API_INPUT_LEGACY: | 
 | 337 |                 break; | 
| Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 338 |             case AudioPolicyInterface::API_INPUT_TELEPHONY_RX: | 
 | 339 |                 // FIXME: use the same permission as for remote submix for now. | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 340 |             case AudioPolicyInterface::API_INPUT_MIX_CAPTURE: | 
| Eric Laurent | b2379ba | 2016-05-23 17:42:12 -0700 | [diff] [blame] | 341 |                 if (!captureAudioOutputAllowed(pid, uid)) { | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 342 |                     ALOGE("getInputForAttr() permission denied: capture not allowed"); | 
 | 343 |                     status = PERMISSION_DENIED; | 
 | 344 |                 } | 
 | 345 |                 break; | 
 | 346 |             case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE: | 
 | 347 |                 if (!modifyAudioRoutingAllowed()) { | 
 | 348 |                     ALOGE("getInputForAttr() permission denied: modify audio routing not allowed"); | 
 | 349 |                     status = PERMISSION_DENIED; | 
 | 350 |                 } | 
 | 351 |                 break; | 
 | 352 |             case AudioPolicyInterface::API_INPUT_INVALID: | 
 | 353 |             default: | 
 | 354 |                 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d", | 
 | 355 |                         (int)inputType); | 
 | 356 |             } | 
 | 357 |         } | 
 | 358 |  | 
 | 359 |         if (status != NO_ERROR) { | 
 | 360 |             if (status == PERMISSION_DENIED) { | 
 | 361 |                 mAudioPolicyManager->releaseInput(*input, session); | 
 | 362 |             } | 
 | 363 |             return status; | 
 | 364 |         } | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 365 |     } | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 366 |  | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 367 |     if (audioPolicyEffects != 0) { | 
 | 368 |         // create audio pre processors according to input source | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 369 |         status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 370 |         if (status != NO_ERROR && status != ALREADY_EXISTS) { | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 371 |             ALOGW("Failed to add effects on input %d", *input); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 372 |         } | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 373 |     } | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 374 |     return NO_ERROR; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 375 | } | 
 | 376 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 377 | status_t AudioPolicyService::startInput(audio_io_handle_t input, | 
 | 378 |                                         audio_session_t session) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 379 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 380 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 381 |         return NO_INIT; | 
 | 382 |     } | 
 | 383 |     Mutex::Autolock _l(mLock); | 
 | 384 |  | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 385 |     return mAudioPolicyManager->startInput(input, session); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 386 | } | 
 | 387 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 388 | status_t AudioPolicyService::stopInput(audio_io_handle_t input, | 
 | 389 |                                        audio_session_t session) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 390 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 391 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 392 |         return NO_INIT; | 
 | 393 |     } | 
 | 394 |     Mutex::Autolock _l(mLock); | 
 | 395 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 396 |     return mAudioPolicyManager->stopInput(input, session); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 397 | } | 
 | 398 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 399 | void AudioPolicyService::releaseInput(audio_io_handle_t input, | 
 | 400 |                                       audio_session_t session) | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 401 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 402 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 403 |         return; | 
 | 404 |     } | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 405 |     sp<AudioPolicyEffects>audioPolicyEffects; | 
 | 406 |     { | 
 | 407 |         Mutex::Autolock _l(mLock); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 408 |         audioPolicyEffects = mAudioPolicyEffects; | 
 | 409 |     } | 
 | 410 |     if (audioPolicyEffects != 0) { | 
 | 411 |         // release audio processors from the input | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 412 |         status_t status = audioPolicyEffects->releaseInputEffects(input); | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 413 |         if(status != NO_ERROR) { | 
 | 414 |             ALOGW("Failed to release effects on input %d", input); | 
 | 415 |         } | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 416 |     } | 
| Eric Laurent | b378b73 | 2016-12-01 15:28:29 -0800 | [diff] [blame] | 417 |     { | 
 | 418 |         Mutex::Autolock _l(mLock); | 
 | 419 |         mAudioPolicyManager->releaseInput(input, session); | 
 | 420 |     } | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 421 | } | 
 | 422 |  | 
 | 423 | status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream, | 
 | 424 |                                             int indexMin, | 
 | 425 |                                             int indexMax) | 
 | 426 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 427 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 428 |         return NO_INIT; | 
 | 429 |     } | 
 | 430 |     if (!settingsAllowed()) { | 
 | 431 |         return PERMISSION_DENIED; | 
 | 432 |     } | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 433 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 434 |         return BAD_VALUE; | 
 | 435 |     } | 
 | 436 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 437 |     mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 438 |     return NO_ERROR; | 
 | 439 | } | 
 | 440 |  | 
 | 441 | status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream, | 
 | 442 |                                                   int index, | 
 | 443 |                                                   audio_devices_t device) | 
 | 444 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 445 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 446 |         return NO_INIT; | 
 | 447 |     } | 
 | 448 |     if (!settingsAllowed()) { | 
 | 449 |         return PERMISSION_DENIED; | 
 | 450 |     } | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 451 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 452 |         return BAD_VALUE; | 
 | 453 |     } | 
 | 454 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 455 |     return mAudioPolicyManager->setStreamVolumeIndex(stream, | 
 | 456 |                                                     index, | 
 | 457 |                                                     device); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 458 | } | 
 | 459 |  | 
 | 460 | status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream, | 
 | 461 |                                                   int *index, | 
 | 462 |                                                   audio_devices_t device) | 
 | 463 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 464 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 465 |         return NO_INIT; | 
 | 466 |     } | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 467 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 468 |         return BAD_VALUE; | 
 | 469 |     } | 
 | 470 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 471 |     return mAudioPolicyManager->getStreamVolumeIndex(stream, | 
 | 472 |                                                     index, | 
 | 473 |                                                     device); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 474 | } | 
 | 475 |  | 
 | 476 | uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream) | 
 | 477 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 478 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 479 |         return 0; | 
| Eric Laurent | dea1541 | 2014-10-28 15:46:45 -0700 | [diff] [blame] | 480 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 481 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 482 |         return 0; | 
 | 483 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 484 |     return mAudioPolicyManager->getStrategyForStream(stream); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 485 | } | 
 | 486 |  | 
 | 487 | //audio policy: use audio_device_t appropriately | 
 | 488 |  | 
 | 489 | audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream) | 
 | 490 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 491 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 492 |         return AUDIO_DEVICE_NONE; | 
| Eric Laurent | dea1541 | 2014-10-28 15:46:45 -0700 | [diff] [blame] | 493 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 494 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 495 |         return AUDIO_DEVICE_NONE; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 496 |     } | 
| Haynes Mathew George | dfb9f3b | 2015-10-26 18:22:13 -0700 | [diff] [blame] | 497 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 498 |     return mAudioPolicyManager->getDevicesForStream(stream); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 499 | } | 
 | 500 |  | 
 | 501 | audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc) | 
 | 502 | { | 
 | 503 |     // FIXME change return type to status_t, and return NO_INIT here | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 504 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 505 |         return 0; | 
 | 506 |     } | 
 | 507 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 508 |     return mAudioPolicyManager->getOutputForEffect(desc); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 509 | } | 
 | 510 |  | 
 | 511 | status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc, | 
 | 512 |                                 audio_io_handle_t io, | 
 | 513 |                                 uint32_t strategy, | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 514 |                                 audio_session_t session, | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 515 |                                 int id) | 
 | 516 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 517 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 518 |         return NO_INIT; | 
 | 519 |     } | 
| Haynes Mathew George | bab7bf4 | 2015-10-30 18:02:23 -0700 | [diff] [blame] | 520 |     Mutex::Autolock _l(mEffectsLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 521 |     return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 522 | } | 
 | 523 |  | 
 | 524 | status_t AudioPolicyService::unregisterEffect(int id) | 
 | 525 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 526 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 527 |         return NO_INIT; | 
 | 528 |     } | 
| Haynes Mathew George | bab7bf4 | 2015-10-30 18:02:23 -0700 | [diff] [blame] | 529 |     Mutex::Autolock _l(mEffectsLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 530 |     return mAudioPolicyManager->unregisterEffect(id); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 531 | } | 
 | 532 |  | 
 | 533 | status_t AudioPolicyService::setEffectEnabled(int id, bool enabled) | 
 | 534 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 535 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 536 |         return NO_INIT; | 
 | 537 |     } | 
| Haynes Mathew George | bab7bf4 | 2015-10-30 18:02:23 -0700 | [diff] [blame] | 538 |     Mutex::Autolock _l(mEffectsLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 539 |     return mAudioPolicyManager->setEffectEnabled(id, enabled); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 540 | } | 
 | 541 |  | 
 | 542 | bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const | 
 | 543 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 544 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 545 |         return false; | 
| Eric Laurent | dea1541 | 2014-10-28 15:46:45 -0700 | [diff] [blame] | 546 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 547 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 548 |         return false; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 549 |     } | 
 | 550 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 551 |     return mAudioPolicyManager->isStreamActive(stream, inPastMs); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 552 | } | 
 | 553 |  | 
 | 554 | bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const | 
 | 555 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 556 |     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 557 |         return false; | 
| Eric Laurent | dea1541 | 2014-10-28 15:46:45 -0700 | [diff] [blame] | 558 |     } | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 559 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | b1322c7 | 2014-10-30 14:59:13 -0700 | [diff] [blame] | 560 |         return false; | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 561 |     } | 
 | 562 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 563 |     return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 564 | } | 
 | 565 |  | 
 | 566 | bool AudioPolicyService::isSourceActive(audio_source_t source) const | 
 | 567 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 568 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 569 |         return false; | 
 | 570 |     } | 
 | 571 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 572 |     return mAudioPolicyManager->isSourceActive(source); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 573 | } | 
 | 574 |  | 
| Glenn Kasten | d848eb4 | 2016-03-08 13:42:11 -0800 | [diff] [blame] | 575 | status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession, | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 576 |                                                        effect_descriptor_t *descriptors, | 
 | 577 |                                                        uint32_t *count) | 
 | 578 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 579 |     if (mAudioPolicyManager == NULL) { | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 580 |         *count = 0; | 
 | 581 |         return NO_INIT; | 
 | 582 |     } | 
| Eric Laurent | 8b1e80b | 2014-10-07 09:08:47 -0700 | [diff] [blame] | 583 |     sp<AudioPolicyEffects>audioPolicyEffects; | 
 | 584 |     { | 
 | 585 |         Mutex::Autolock _l(mLock); | 
 | 586 |         audioPolicyEffects = mAudioPolicyEffects; | 
 | 587 |     } | 
 | 588 |     if (audioPolicyEffects == 0) { | 
 | 589 |         *count = 0; | 
 | 590 |         return NO_INIT; | 
 | 591 |     } | 
| Eric Laurent | 232f26f | 2016-02-17 18:06:27 -0800 | [diff] [blame] | 592 |     return audioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 593 | } | 
 | 594 |  | 
 | 595 | bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info) | 
 | 596 | { | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 597 |     if (mAudioPolicyManager == NULL) { | 
 | 598 |         ALOGV("mAudioPolicyManager == NULL"); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 599 |         return false; | 
 | 600 |     } | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 601 |     Mutex::Autolock _l(mLock); | 
| Haynes Mathew George | bab7bf4 | 2015-10-30 18:02:23 -0700 | [diff] [blame] | 602 |     Mutex::Autolock _le(mEffectsLock); // isOffloadSupported queries for | 
 | 603 |                                       // non-offloadable effects | 
| Eric Laurent | dce54a1 | 2014-03-10 12:19:46 -0700 | [diff] [blame] | 604 |     return mAudioPolicyManager->isOffloadSupported(info); | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 605 | } | 
 | 606 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 607 | status_t AudioPolicyService::listAudioPorts(audio_port_role_t role, | 
 | 608 |                                             audio_port_type_t type, | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 609 |                                             unsigned int *num_ports, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 610 |                                             struct audio_port *ports, | 
 | 611 |                                             unsigned int *generation) | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 612 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 613 |     Mutex::Autolock _l(mLock); | 
 | 614 |     if (mAudioPolicyManager == NULL) { | 
 | 615 |         return NO_INIT; | 
 | 616 |     } | 
 | 617 |  | 
 | 618 |     return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation); | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 619 | } | 
 | 620 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 621 | status_t AudioPolicyService::getAudioPort(struct audio_port *port) | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 622 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 623 |     Mutex::Autolock _l(mLock); | 
 | 624 |     if (mAudioPolicyManager == NULL) { | 
 | 625 |         return NO_INIT; | 
 | 626 |     } | 
 | 627 |  | 
 | 628 |     return mAudioPolicyManager->getAudioPort(port); | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 629 | } | 
 | 630 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 631 | status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch, | 
 | 632 |         audio_patch_handle_t *handle) | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 633 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 634 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 635 |     if(!modifyAudioRoutingAllowed()) { | 
 | 636 |         return PERMISSION_DENIED; | 
 | 637 |     } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 638 |     if (mAudioPolicyManager == NULL) { | 
 | 639 |         return NO_INIT; | 
 | 640 |     } | 
 | 641 |     return mAudioPolicyManager->createAudioPatch(patch, handle, | 
 | 642 |                                                   IPCThreadState::self()->getCallingUid()); | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 643 | } | 
 | 644 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 645 | status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle) | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 646 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 647 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 648 |     if(!modifyAudioRoutingAllowed()) { | 
 | 649 |         return PERMISSION_DENIED; | 
 | 650 |     } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 651 |     if (mAudioPolicyManager == NULL) { | 
 | 652 |         return NO_INIT; | 
 | 653 |     } | 
 | 654 |  | 
 | 655 |     return mAudioPolicyManager->releaseAudioPatch(handle, | 
 | 656 |                                                      IPCThreadState::self()->getCallingUid()); | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 657 | } | 
 | 658 |  | 
 | 659 | status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 660 |         struct audio_patch *patches, | 
 | 661 |         unsigned int *generation) | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 662 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 663 |     Mutex::Autolock _l(mLock); | 
 | 664 |     if (mAudioPolicyManager == NULL) { | 
 | 665 |         return NO_INIT; | 
 | 666 |     } | 
 | 667 |  | 
 | 668 |     return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation); | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 669 | } | 
 | 670 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 671 | status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config) | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 672 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 673 |     Mutex::Autolock _l(mLock); | 
| Eric Laurent | 5284ed5 | 2014-05-29 14:37:38 -0700 | [diff] [blame] | 674 |     if(!modifyAudioRoutingAllowed()) { | 
 | 675 |         return PERMISSION_DENIED; | 
 | 676 |     } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 677 |     if (mAudioPolicyManager == NULL) { | 
 | 678 |         return NO_INIT; | 
 | 679 |     } | 
 | 680 |  | 
 | 681 |     return mAudioPolicyManager->setAudioPortConfig(config); | 
| Eric Laurent | 203b1a1 | 2014-04-01 10:34:16 -0700 | [diff] [blame] | 682 | } | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 683 |  | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 684 | status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session, | 
 | 685 |                                        audio_io_handle_t *ioHandle, | 
 | 686 |                                        audio_devices_t *device) | 
 | 687 | { | 
 | 688 |     if (mAudioPolicyManager == NULL) { | 
 | 689 |         return NO_INIT; | 
 | 690 |     } | 
 | 691 |  | 
 | 692 |     return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device); | 
 | 693 | } | 
 | 694 |  | 
 | 695 | status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session) | 
 | 696 | { | 
 | 697 |     if (mAudioPolicyManager == NULL) { | 
 | 698 |         return NO_INIT; | 
 | 699 |     } | 
 | 700 |  | 
 | 701 |     return mAudioPolicyManager->releaseSoundTriggerSession(session); | 
 | 702 | } | 
 | 703 |  | 
| Chih-Hung Hsieh | 36d0ca1 | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 704 | status_t AudioPolicyService::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration) | 
| Eric Laurent | baac183 | 2014-12-01 17:52:59 -0800 | [diff] [blame] | 705 | { | 
 | 706 |     Mutex::Autolock _l(mLock); | 
 | 707 |     if(!modifyAudioRoutingAllowed()) { | 
 | 708 |         return PERMISSION_DENIED; | 
 | 709 |     } | 
 | 710 |     if (mAudioPolicyManager == NULL) { | 
 | 711 |         return NO_INIT; | 
 | 712 |     } | 
 | 713 |     if (registration) { | 
 | 714 |         return mAudioPolicyManager->registerPolicyMixes(mixes); | 
 | 715 |     } else { | 
 | 716 |         return mAudioPolicyManager->unregisterPolicyMixes(mixes); | 
 | 717 |     } | 
 | 718 | } | 
 | 719 |  | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 720 | status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source, | 
 | 721 |                                   const audio_attributes_t *attributes, | 
 | 722 |                                   audio_io_handle_t *handle) | 
 | 723 | { | 
 | 724 |     Mutex::Autolock _l(mLock); | 
 | 725 |     if (mAudioPolicyManager == NULL) { | 
 | 726 |         return NO_INIT; | 
 | 727 |     } | 
 | 728 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 729 |     return mAudioPolicyManager->startAudioSource(source, attributes, handle, | 
 | 730 |                                                  IPCThreadState::self()->getCallingUid()); | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 731 | } | 
 | 732 |  | 
 | 733 | status_t AudioPolicyService::stopAudioSource(audio_io_handle_t handle) | 
 | 734 | { | 
 | 735 |     Mutex::Autolock _l(mLock); | 
 | 736 |     if (mAudioPolicyManager == NULL) { | 
 | 737 |         return NO_INIT; | 
 | 738 |     } | 
 | 739 |  | 
 | 740 |     return mAudioPolicyManager->stopAudioSource(handle); | 
 | 741 | } | 
 | 742 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 743 | status_t AudioPolicyService::setMasterMono(bool mono) | 
 | 744 | { | 
 | 745 |     if (mAudioPolicyManager == NULL) { | 
 | 746 |         return NO_INIT; | 
 | 747 |     } | 
 | 748 |     if (!settingsAllowed()) { | 
 | 749 |         return PERMISSION_DENIED; | 
 | 750 |     } | 
 | 751 |     Mutex::Autolock _l(mLock); | 
 | 752 |     return mAudioPolicyManager->setMasterMono(mono); | 
 | 753 | } | 
 | 754 |  | 
 | 755 | status_t AudioPolicyService::getMasterMono(bool *mono) | 
 | 756 | { | 
 | 757 |     if (mAudioPolicyManager == NULL) { | 
 | 758 |         return NO_INIT; | 
 | 759 |     } | 
 | 760 |     Mutex::Autolock _l(mLock); | 
 | 761 |     return mAudioPolicyManager->getMasterMono(mono); | 
 | 762 | } | 
 | 763 |  | 
| Eric Laurent | 2d388ec | 2014-03-07 13:25:54 -0800 | [diff] [blame] | 764 | }; // namespace android |