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