blob: 2f7b1fbbeb57ed99afacedef934aaf7473a6c4e3 [file] [log] [blame]
Eric Laurent2d388ec2014-03-07 13:25:54 -08001/*
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 Laurentdce54a12014-03-10 12:19:46 -070017#define LOG_TAG "AudioPolicyIntefaceImpl"
Eric Laurent2d388ec2014-03-07 13:25:54 -080018//#define LOG_NDEBUG 0
19
20#include <utils/Log.h>
21#include "AudioPolicyService.h"
22#include "ServiceUtilities.h"
23
Eric Laurent2d388ec2014-03-07 13:25:54 -080024namespace android {
25
26
27// ----------------------------------------------------------------------------
28
29status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
30 audio_policy_dev_state_t state,
Paul McLeane743a472015-01-28 11:07:31 -080031 const char *device_address,
32 const char *device_name)
Eric Laurent2d388ec2014-03-07 13:25:54 -080033{
Eric Laurentdce54a12014-03-10 12:19:46 -070034 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -080035 return NO_INIT;
36 }
37 if (!settingsAllowed()) {
38 return PERMISSION_DENIED;
39 }
Eric Laurent2d388ec2014-03-07 13:25:54 -080040 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
41 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
42 return BAD_VALUE;
43 }
44
45 ALOGV("setDeviceConnectionState()");
46 Mutex::Autolock _l(mLock);
Paul McLeane743a472015-01-28 11:07:31 -080047 return mAudioPolicyManager->setDeviceConnectionState(device, state,
48 device_address, device_name);
Eric Laurent2d388ec2014-03-07 13:25:54 -080049}
50
51audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
52 audio_devices_t device,
53 const char *device_address)
54{
Eric Laurentdce54a12014-03-10 12:19:46 -070055 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -080056 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
57 }
Eric Laurentdce54a12014-03-10 12:19:46 -070058 return mAudioPolicyManager->getDeviceConnectionState(device,
Eric Laurent2d388ec2014-03-07 13:25:54 -080059 device_address);
60}
61
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -080062status_t AudioPolicyService::handleDeviceConfigChange(audio_devices_t device,
63 const char *device_address,
64 const char *device_name)
65{
66 if (mAudioPolicyManager == NULL) {
67 return NO_INIT;
68 }
69 if (!settingsAllowed()) {
70 return PERMISSION_DENIED;
71 }
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -080072
73 ALOGV("handleDeviceConfigChange()");
74 Mutex::Autolock _l(mLock);
75 return mAudioPolicyManager->handleDeviceConfigChange(device, device_address,
76 device_name);
77}
78
Eric Laurent2d388ec2014-03-07 13:25:54 -080079status_t AudioPolicyService::setPhoneState(audio_mode_t state)
80{
Eric Laurentdce54a12014-03-10 12:19:46 -070081 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -080082 return NO_INIT;
83 }
84 if (!settingsAllowed()) {
85 return PERMISSION_DENIED;
86 }
87 if (uint32_t(state) >= AUDIO_MODE_CNT) {
88 return BAD_VALUE;
89 }
90
91 ALOGV("setPhoneState()");
92
Eric Laurentbeb07fe2015-09-16 15:49:30 -070093 // acquire lock before calling setMode() so that setMode() + setPhoneState() are an atomic
94 // operation from policy manager standpoint (no other operation (e.g track start or stop)
95 // can be interleaved).
96 Mutex::Autolock _l(mLock);
97
Eric Laurent2d388ec2014-03-07 13:25:54 -080098 // TODO: check if it is more appropriate to do it in platform specific policy manager
99 AudioSystem::setMode(state);
100
Eric Laurentdce54a12014-03-10 12:19:46 -0700101 mAudioPolicyManager->setPhoneState(state);
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700102 mPhoneState = state;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800103 return NO_ERROR;
104}
105
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700106audio_mode_t AudioPolicyService::getPhoneState()
107{
108 Mutex::Autolock _l(mLock);
109 return mPhoneState;
110}
111
Eric Laurent2d388ec2014-03-07 13:25:54 -0800112status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
113 audio_policy_forced_cfg_t config)
114{
Eric Laurentdce54a12014-03-10 12:19:46 -0700115 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800116 return NO_INIT;
117 }
118 if (!settingsAllowed()) {
119 return PERMISSION_DENIED;
120 }
121 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
122 return BAD_VALUE;
123 }
124 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
125 return BAD_VALUE;
126 }
127 ALOGV("setForceUse()");
128 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700129 mAudioPolicyManager->setForceUse(usage, config);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800130 return NO_ERROR;
131}
132
133audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
134{
Eric Laurentdce54a12014-03-10 12:19:46 -0700135 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800136 return AUDIO_POLICY_FORCE_NONE;
137 }
138 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
139 return AUDIO_POLICY_FORCE_NONE;
140 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700141 return mAudioPolicyManager->getForceUse(usage);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800142}
143
144audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
145 uint32_t samplingRate,
146 audio_format_t format,
147 audio_channel_mask_t channelMask,
148 audio_output_flags_t flags,
149 const audio_offload_info_t *offloadInfo)
150{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800151 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700152 return AUDIO_IO_HANDLE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700153 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700154 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700155 return AUDIO_IO_HANDLE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800156 }
157 ALOGV("getOutput()");
158 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700159 return mAudioPolicyManager->getOutput(stream, samplingRate,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800160 format, channelMask, flags, offloadInfo);
161}
162
Eric Laurente83b55d2014-11-14 10:06:21 -0800163status_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr,
164 audio_io_handle_t *output,
165 audio_session_t session,
166 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700167 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800168 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800169 audio_output_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -0600170 audio_port_handle_t selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800171 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700172{
173 if (mAudioPolicyManager == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800174 return NO_INIT;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700175 }
176 ALOGV("getOutput()");
177 Mutex::Autolock _l(mLock);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700178
Marco Nelissendcb346b2015-09-09 10:47:29 -0700179 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
180 if (!isTrustedCallingUid(callingUid) || uid == (uid_t)-1) {
181 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
182 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
183 uid = callingUid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700184 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800185 return mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid,
186 config,
187 flags, selectedDeviceId, portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700188}
189
Eric Laurent2d388ec2014-03-07 13:25:54 -0800190status_t AudioPolicyService::startOutput(audio_io_handle_t output,
191 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800192 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800193{
Eric Laurentdea15412014-10-28 15:46:45 -0700194 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
195 return BAD_VALUE;
196 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700197 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800198 return NO_INIT;
199 }
200 ALOGV("startOutput()");
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700201 sp<AudioPolicyEffects>audioPolicyEffects;
202 {
203 Mutex::Autolock _l(mLock);
204 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800205 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700206 if (audioPolicyEffects != 0) {
207 // create audio processors according to stream
208 status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session);
209 if (status != NO_ERROR && status != ALREADY_EXISTS) {
210 ALOGW("Failed to add effects on session %d", session);
211 }
212 }
213 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700214 return mAudioPolicyManager->startOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800215}
216
217status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
218 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800219 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800220{
Eric Laurentdea15412014-10-28 15:46:45 -0700221 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
222 return BAD_VALUE;
223 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700224 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800225 return NO_INIT;
226 }
227 ALOGV("stopOutput()");
228 mOutputCommandThread->stopOutputCommand(output, stream, session);
229 return NO_ERROR;
230}
231
232status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
233 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800234 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800235{
236 ALOGV("doStopOutput from tid %d", gettid());
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700237 sp<AudioPolicyEffects>audioPolicyEffects;
238 {
239 Mutex::Autolock _l(mLock);
240 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800241 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700242 if (audioPolicyEffects != 0) {
243 // release audio processors from the stream
244 status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
245 if (status != NO_ERROR && status != ALREADY_EXISTS) {
246 ALOGW("Failed to release effects on session %d", session);
247 }
248 }
249 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700250 return mAudioPolicyManager->stopOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800251}
252
Eric Laurente83b55d2014-11-14 10:06:21 -0800253void AudioPolicyService::releaseOutput(audio_io_handle_t output,
254 audio_stream_type_t stream,
255 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800256{
Eric Laurentdce54a12014-03-10 12:19:46 -0700257 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800258 return;
259 }
260 ALOGV("releaseOutput()");
Eric Laurente83b55d2014-11-14 10:06:21 -0800261 mOutputCommandThread->releaseOutputCommand(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800262}
263
Eric Laurente83b55d2014-11-14 10:06:21 -0800264void AudioPolicyService::doReleaseOutput(audio_io_handle_t output,
265 audio_stream_type_t stream,
266 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800267{
268 ALOGV("doReleaseOutput from tid %d", gettid());
269 Mutex::Autolock _l(mLock);
Eric Laurente83b55d2014-11-14 10:06:21 -0800270 mAudioPolicyManager->releaseOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800271}
272
Eric Laurentcaf7f482014-11-25 17:50:47 -0800273status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr,
274 audio_io_handle_t *input,
275 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700276 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700277 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800278 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600279 audio_input_flags_t flags,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800280 audio_port_handle_t selectedDeviceId,
281 audio_port_handle_t *portId)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800282{
Eric Laurentdce54a12014-03-10 12:19:46 -0700283 if (mAudioPolicyManager == NULL) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800284 return NO_INIT;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800285 }
286 // already checked by client, but double-check in case the client wrapper is bypassed
Eric Laurentcaf7f482014-11-25 17:50:47 -0800287 if (attr->source >= AUDIO_SOURCE_CNT && attr->source != AUDIO_SOURCE_HOTWORD &&
288 attr->source != AUDIO_SOURCE_FM_TUNER) {
289 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800290 }
291
Eric Laurentab300c82015-04-13 13:47:33 -0700292 if ((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800293 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800294 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700295 sp<AudioPolicyEffects>audioPolicyEffects;
Eric Laurentcaf7f482014-11-25 17:50:47 -0800296 status_t status;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800297 AudioPolicyInterface::input_type_t inputType;
Eric Laurentb2379ba2016-05-23 17:42:12 -0700298
299 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -0700300 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurentb2379ba2016-05-23 17:42:12 -0700301 if (!isTrustedCallingUid(callingUid)) {
Eric Laurent9f39f8d2016-05-25 12:34:48 -0700302 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -0700303 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
304 uid = callingUid;
Eric Laurentb2379ba2016-05-23 17:42:12 -0700305 updatePid = true;
306 }
307
308 if (updatePid) {
309 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Eric Laurent9f39f8d2016-05-25 12:34:48 -0700310 ALOGW_IF(pid != (pid_t)-1 && pid != callingPid,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700311 "%s uid %d pid %d tried to pass itself off as pid %d",
312 __func__, callingUid, callingPid, pid);
313 pid = callingPid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700314 }
315
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700316 {
317 Mutex::Autolock _l(mLock);
318 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700319 status = mAudioPolicyManager->getInputForAttr(attr, input, session, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800320 config,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700321 flags, selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800322 &inputType, portId);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700323 audioPolicyEffects = mAudioPolicyEffects;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800324
325 if (status == NO_ERROR) {
326 // enforce permission (if any) required for each type of input
327 switch (inputType) {
328 case AudioPolicyInterface::API_INPUT_LEGACY:
329 break;
Eric Laurent82db2692015-08-07 13:59:42 -0700330 case AudioPolicyInterface::API_INPUT_TELEPHONY_RX:
331 // FIXME: use the same permission as for remote submix for now.
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800332 case AudioPolicyInterface::API_INPUT_MIX_CAPTURE:
Eric Laurentb2379ba2016-05-23 17:42:12 -0700333 if (!captureAudioOutputAllowed(pid, uid)) {
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800334 ALOGE("getInputForAttr() permission denied: capture not allowed");
335 status = PERMISSION_DENIED;
336 }
337 break;
338 case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE:
339 if (!modifyAudioRoutingAllowed()) {
340 ALOGE("getInputForAttr() permission denied: modify audio routing not allowed");
341 status = PERMISSION_DENIED;
342 }
343 break;
344 case AudioPolicyInterface::API_INPUT_INVALID:
345 default:
346 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d",
347 (int)inputType);
348 }
349 }
350
351 if (status != NO_ERROR) {
352 if (status == PERMISSION_DENIED) {
353 mAudioPolicyManager->releaseInput(*input, session);
354 }
355 return status;
356 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700357 }
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800358
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700359 if (audioPolicyEffects != 0) {
360 // create audio pre processors according to input source
Eric Laurentcaf7f482014-11-25 17:50:47 -0800361 status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700362 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800363 ALOGW("Failed to add effects on input %d", *input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700364 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800365 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800366 return NO_ERROR;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800367}
368
Eric Laurent4dc68062014-07-28 17:26:49 -0700369status_t AudioPolicyService::startInput(audio_io_handle_t input,
370 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800371{
Eric Laurentdce54a12014-03-10 12:19:46 -0700372 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800373 return NO_INIT;
374 }
375 Mutex::Autolock _l(mLock);
Eric Laurentfb66dd92016-01-28 18:32:03 -0800376 AudioPolicyInterface::concurrency_type__mask_t concurrency;
377 status_t status = mAudioPolicyManager->startInput(input, session, &concurrency);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800378
Eric Laurentfb66dd92016-01-28 18:32:03 -0800379 if (status == NO_ERROR) {
Eric Laurent43423352016-02-05 11:57:57 -0800380 LOG_ALWAYS_FATAL_IF(concurrency & ~AudioPolicyInterface::API_INPUT_CONCURRENCY_ALL,
381 "startInput(): invalid concurrency type %d", (int)concurrency);
382
Eric Laurentfb66dd92016-01-28 18:32:03 -0800383 // enforce permission (if any) required for each type of concurrency
Eric Laurent43423352016-02-05 11:57:57 -0800384 if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CALL) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800385 //TODO: check incall capture permission
Eric Laurent43423352016-02-05 11:57:57 -0800386 }
387 if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CAPTURE) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800388 //TODO: check concurrent capture permission
Eric Laurentfb66dd92016-01-28 18:32:03 -0800389 }
390 }
391
392 return status;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800393}
394
Eric Laurent4dc68062014-07-28 17:26:49 -0700395status_t AudioPolicyService::stopInput(audio_io_handle_t input,
396 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800397{
Eric Laurentdce54a12014-03-10 12:19:46 -0700398 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800399 return NO_INIT;
400 }
401 Mutex::Autolock _l(mLock);
402
Eric Laurent4dc68062014-07-28 17:26:49 -0700403 return mAudioPolicyManager->stopInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800404}
405
Eric Laurent4dc68062014-07-28 17:26:49 -0700406void AudioPolicyService::releaseInput(audio_io_handle_t input,
407 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800408{
Eric Laurentdce54a12014-03-10 12:19:46 -0700409 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800410 return;
411 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700412 sp<AudioPolicyEffects>audioPolicyEffects;
413 {
414 Mutex::Autolock _l(mLock);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700415 audioPolicyEffects = mAudioPolicyEffects;
416 }
417 if (audioPolicyEffects != 0) {
418 // release audio processors from the input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800419 status_t status = audioPolicyEffects->releaseInputEffects(input, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700420 if(status != NO_ERROR) {
421 ALOGW("Failed to release effects on input %d", input);
422 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800423 }
Eric Laurentf10c7092016-12-06 17:09:56 -0800424 {
425 Mutex::Autolock _l(mLock);
426 mAudioPolicyManager->releaseInput(input, session);
427 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800428}
429
430status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
431 int indexMin,
432 int indexMax)
433{
Eric Laurentdce54a12014-03-10 12:19:46 -0700434 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800435 return NO_INIT;
436 }
437 if (!settingsAllowed()) {
438 return PERMISSION_DENIED;
439 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800440 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800441 return BAD_VALUE;
442 }
443 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700444 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800445 return NO_ERROR;
446}
447
448status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
449 int index,
450 audio_devices_t device)
451{
Eric Laurentdce54a12014-03-10 12:19:46 -0700452 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800453 return NO_INIT;
454 }
455 if (!settingsAllowed()) {
456 return PERMISSION_DENIED;
457 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800458 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800459 return BAD_VALUE;
460 }
461 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700462 return mAudioPolicyManager->setStreamVolumeIndex(stream,
463 index,
464 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800465}
466
467status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
468 int *index,
469 audio_devices_t device)
470{
Eric Laurentdce54a12014-03-10 12:19:46 -0700471 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800472 return NO_INIT;
473 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800474 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800475 return BAD_VALUE;
476 }
477 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700478 return mAudioPolicyManager->getStreamVolumeIndex(stream,
479 index,
480 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800481}
482
483uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
484{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800485 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700486 return 0;
Eric Laurentdea15412014-10-28 15:46:45 -0700487 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700488 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800489 return 0;
490 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700491 return mAudioPolicyManager->getStrategyForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800492}
493
494//audio policy: use audio_device_t appropriately
495
496audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
497{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800498 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700499 return AUDIO_DEVICE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700500 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700501 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700502 return AUDIO_DEVICE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800503 }
Haynes Mathew Georgedfb9f3b2015-10-26 18:22:13 -0700504 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700505 return mAudioPolicyManager->getDevicesForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800506}
507
508audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
509{
510 // FIXME change return type to status_t, and return NO_INIT here
Eric Laurentdce54a12014-03-10 12:19:46 -0700511 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800512 return 0;
513 }
514 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700515 return mAudioPolicyManager->getOutputForEffect(desc);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800516}
517
518status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
519 audio_io_handle_t io,
520 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800521 audio_session_t session,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800522 int id)
523{
Eric Laurentdce54a12014-03-10 12:19:46 -0700524 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800525 return NO_INIT;
526 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700527 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700528 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800529}
530
531status_t AudioPolicyService::unregisterEffect(int id)
532{
Eric Laurentdce54a12014-03-10 12:19:46 -0700533 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800534 return NO_INIT;
535 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700536 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700537 return mAudioPolicyManager->unregisterEffect(id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800538}
539
540status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
541{
Eric Laurentdce54a12014-03-10 12:19:46 -0700542 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800543 return NO_INIT;
544 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700545 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700546 return mAudioPolicyManager->setEffectEnabled(id, enabled);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800547}
548
549bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
550{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800551 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700552 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700553 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700554 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700555 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800556 }
557 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700558 return mAudioPolicyManager->isStreamActive(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800559}
560
561bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
562{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800563 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700564 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700565 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700566 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700567 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800568 }
569 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700570 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800571}
572
573bool AudioPolicyService::isSourceActive(audio_source_t source) const
574{
Eric Laurentdce54a12014-03-10 12:19:46 -0700575 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800576 return false;
577 }
578 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700579 return mAudioPolicyManager->isSourceActive(source);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800580}
581
Glenn Kastend848eb42016-03-08 13:42:11 -0800582status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800583 effect_descriptor_t *descriptors,
584 uint32_t *count)
585{
Eric Laurentdce54a12014-03-10 12:19:46 -0700586 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800587 *count = 0;
588 return NO_INIT;
589 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700590 sp<AudioPolicyEffects>audioPolicyEffects;
591 {
592 Mutex::Autolock _l(mLock);
593 audioPolicyEffects = mAudioPolicyEffects;
594 }
595 if (audioPolicyEffects == 0) {
596 *count = 0;
597 return NO_INIT;
598 }
Eric Laurentfb66dd92016-01-28 18:32:03 -0800599 return audioPolicyEffects->queryDefaultInputEffects(
600 (audio_session_t)audioSession, descriptors, count);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800601}
602
603bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
604{
Eric Laurentdce54a12014-03-10 12:19:46 -0700605 if (mAudioPolicyManager == NULL) {
606 ALOGV("mAudioPolicyManager == NULL");
Eric Laurent2d388ec2014-03-07 13:25:54 -0800607 return false;
608 }
Andy Hung2ddee192015-12-18 17:34:44 -0800609 Mutex::Autolock _l(mLock);
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700610 Mutex::Autolock _le(mEffectsLock); // isOffloadSupported queries for
611 // non-offloadable effects
Eric Laurentdce54a12014-03-10 12:19:46 -0700612 return mAudioPolicyManager->isOffloadSupported(info);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800613}
614
Eric Laurent6a94d692014-05-20 11:18:06 -0700615status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
616 audio_port_type_t type,
Eric Laurent203b1a12014-04-01 10:34:16 -0700617 unsigned int *num_ports,
Eric Laurent6a94d692014-05-20 11:18:06 -0700618 struct audio_port *ports,
619 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700620{
Eric Laurent6a94d692014-05-20 11:18:06 -0700621 Mutex::Autolock _l(mLock);
622 if (mAudioPolicyManager == NULL) {
623 return NO_INIT;
624 }
625
626 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700627}
628
Eric Laurent6a94d692014-05-20 11:18:06 -0700629status_t AudioPolicyService::getAudioPort(struct audio_port *port)
Eric Laurent203b1a12014-04-01 10:34:16 -0700630{
Eric Laurent6a94d692014-05-20 11:18:06 -0700631 Mutex::Autolock _l(mLock);
632 if (mAudioPolicyManager == NULL) {
633 return NO_INIT;
634 }
635
636 return mAudioPolicyManager->getAudioPort(port);
Eric Laurent203b1a12014-04-01 10:34:16 -0700637}
638
Eric Laurent6a94d692014-05-20 11:18:06 -0700639status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
640 audio_patch_handle_t *handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700641{
Eric Laurent6a94d692014-05-20 11:18:06 -0700642 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700643 if(!modifyAudioRoutingAllowed()) {
644 return PERMISSION_DENIED;
645 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700646 if (mAudioPolicyManager == NULL) {
647 return NO_INIT;
648 }
649 return mAudioPolicyManager->createAudioPatch(patch, handle,
650 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700651}
652
Eric Laurent6a94d692014-05-20 11:18:06 -0700653status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700654{
Eric Laurent6a94d692014-05-20 11:18:06 -0700655 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700656 if(!modifyAudioRoutingAllowed()) {
657 return PERMISSION_DENIED;
658 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700659 if (mAudioPolicyManager == NULL) {
660 return NO_INIT;
661 }
662
663 return mAudioPolicyManager->releaseAudioPatch(handle,
664 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700665}
666
667status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
Eric Laurent6a94d692014-05-20 11:18:06 -0700668 struct audio_patch *patches,
669 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700670{
Eric Laurent6a94d692014-05-20 11:18:06 -0700671 Mutex::Autolock _l(mLock);
672 if (mAudioPolicyManager == NULL) {
673 return NO_INIT;
674 }
675
676 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700677}
678
Eric Laurent6a94d692014-05-20 11:18:06 -0700679status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent203b1a12014-04-01 10:34:16 -0700680{
Eric Laurent6a94d692014-05-20 11:18:06 -0700681 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700682 if(!modifyAudioRoutingAllowed()) {
683 return PERMISSION_DENIED;
684 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700685 if (mAudioPolicyManager == NULL) {
686 return NO_INIT;
687 }
688
689 return mAudioPolicyManager->setAudioPortConfig(config);
Eric Laurent203b1a12014-04-01 10:34:16 -0700690}
Eric Laurent2d388ec2014-03-07 13:25:54 -0800691
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700692status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
693 audio_io_handle_t *ioHandle,
694 audio_devices_t *device)
695{
Andy Hungf759b8c2017-08-15 12:48:54 -0700696 Mutex::Autolock _l(mLock);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700697 if (mAudioPolicyManager == NULL) {
698 return NO_INIT;
699 }
700
701 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
702}
703
704status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
705{
Andy Hungf759b8c2017-08-15 12:48:54 -0700706 Mutex::Autolock _l(mLock);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700707 if (mAudioPolicyManager == NULL) {
708 return NO_INIT;
709 }
710
711 return mAudioPolicyManager->releaseSoundTriggerSession(session);
712}
713
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700714status_t AudioPolicyService::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800715{
716 Mutex::Autolock _l(mLock);
717 if(!modifyAudioRoutingAllowed()) {
718 return PERMISSION_DENIED;
719 }
720 if (mAudioPolicyManager == NULL) {
721 return NO_INIT;
722 }
723 if (registration) {
724 return mAudioPolicyManager->registerPolicyMixes(mixes);
725 } else {
726 return mAudioPolicyManager->unregisterPolicyMixes(mixes);
727 }
728}
729
Eric Laurent554a2772015-04-10 11:29:24 -0700730status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
731 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -0700732 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700733{
734 Mutex::Autolock _l(mLock);
735 if (mAudioPolicyManager == NULL) {
736 return NO_INIT;
737 }
738
Eric Laurentd60560a2015-04-10 11:31:20 -0700739 return mAudioPolicyManager->startAudioSource(source, attributes, handle,
740 IPCThreadState::self()->getCallingUid());
Eric Laurent554a2772015-04-10 11:29:24 -0700741}
742
Glenn Kasten559d4392016-03-29 13:42:57 -0700743status_t AudioPolicyService::stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700744{
745 Mutex::Autolock _l(mLock);
746 if (mAudioPolicyManager == NULL) {
747 return NO_INIT;
748 }
749
750 return mAudioPolicyManager->stopAudioSource(handle);
751}
752
Andy Hung2ddee192015-12-18 17:34:44 -0800753status_t AudioPolicyService::setMasterMono(bool mono)
754{
755 if (mAudioPolicyManager == NULL) {
756 return NO_INIT;
757 }
758 if (!settingsAllowed()) {
759 return PERMISSION_DENIED;
760 }
761 Mutex::Autolock _l(mLock);
762 return mAudioPolicyManager->setMasterMono(mono);
763}
764
765status_t AudioPolicyService::getMasterMono(bool *mono)
766{
767 if (mAudioPolicyManager == NULL) {
768 return NO_INIT;
769 }
770 Mutex::Autolock _l(mLock);
771 return mAudioPolicyManager->getMasterMono(mono);
772}
773
Eric Laurent2d388ec2014-03-07 13:25:54 -0800774}; // namespace android