blob: 7dd6d70ab55708a0274809b7c5744428de9fa378 [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
Eric Laurentf4e63452017-11-06 19:31:46 +0000144audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800145{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800146 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700147 return AUDIO_IO_HANDLE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700148 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700149 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700150 return AUDIO_IO_HANDLE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800151 }
152 ALOGV("getOutput()");
153 Mutex::Autolock _l(mLock);
Eric Laurentf4e63452017-11-06 19:31:46 +0000154 return mAudioPolicyManager->getOutput(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800155}
156
Eric Laurente83b55d2014-11-14 10:06:21 -0800157status_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr,
158 audio_io_handle_t *output,
159 audio_session_t session,
160 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700161 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800162 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800163 audio_output_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700164 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800165 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700166{
167 if (mAudioPolicyManager == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800168 return NO_INIT;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700169 }
Eric Laurentf4e63452017-11-06 19:31:46 +0000170 ALOGV("getOutputForAttr()");
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700171 Mutex::Autolock _l(mLock);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700172
Marco Nelissendcb346b2015-09-09 10:47:29 -0700173 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
174 if (!isTrustedCallingUid(callingUid) || uid == (uid_t)-1) {
175 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
176 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
177 uid = callingUid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700178 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800179 return mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid,
180 config,
181 flags, selectedDeviceId, portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700182}
183
Eric Laurent2d388ec2014-03-07 13:25:54 -0800184status_t AudioPolicyService::startOutput(audio_io_handle_t output,
185 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800186 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800187{
Eric Laurentdea15412014-10-28 15:46:45 -0700188 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
189 return BAD_VALUE;
190 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700191 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800192 return NO_INIT;
193 }
194 ALOGV("startOutput()");
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700195 sp<AudioPolicyEffects>audioPolicyEffects;
196 {
197 Mutex::Autolock _l(mLock);
198 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800199 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700200 if (audioPolicyEffects != 0) {
201 // create audio processors according to stream
202 status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session);
203 if (status != NO_ERROR && status != ALREADY_EXISTS) {
204 ALOGW("Failed to add effects on session %d", session);
205 }
206 }
207 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700208 return mAudioPolicyManager->startOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800209}
210
211status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
212 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800213 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800214{
Eric Laurentdea15412014-10-28 15:46:45 -0700215 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
216 return BAD_VALUE;
217 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700218 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800219 return NO_INIT;
220 }
221 ALOGV("stopOutput()");
222 mOutputCommandThread->stopOutputCommand(output, stream, session);
223 return NO_ERROR;
224}
225
226status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
227 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800228 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800229{
230 ALOGV("doStopOutput from tid %d", gettid());
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700231 sp<AudioPolicyEffects>audioPolicyEffects;
232 {
233 Mutex::Autolock _l(mLock);
234 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800235 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700236 if (audioPolicyEffects != 0) {
237 // release audio processors from the stream
238 status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
239 if (status != NO_ERROR && status != ALREADY_EXISTS) {
240 ALOGW("Failed to release effects on session %d", session);
241 }
242 }
243 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700244 return mAudioPolicyManager->stopOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800245}
246
Eric Laurente83b55d2014-11-14 10:06:21 -0800247void AudioPolicyService::releaseOutput(audio_io_handle_t output,
248 audio_stream_type_t stream,
249 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800250{
Eric Laurentdce54a12014-03-10 12:19:46 -0700251 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800252 return;
253 }
254 ALOGV("releaseOutput()");
Eric Laurente83b55d2014-11-14 10:06:21 -0800255 mOutputCommandThread->releaseOutputCommand(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800256}
257
Eric Laurente83b55d2014-11-14 10:06:21 -0800258void AudioPolicyService::doReleaseOutput(audio_io_handle_t output,
259 audio_stream_type_t stream,
260 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800261{
262 ALOGV("doReleaseOutput from tid %d", gettid());
263 Mutex::Autolock _l(mLock);
Eric Laurente83b55d2014-11-14 10:06:21 -0800264 mAudioPolicyManager->releaseOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800265}
266
Eric Laurentcaf7f482014-11-25 17:50:47 -0800267status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr,
268 audio_io_handle_t *input,
269 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700270 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700271 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800272 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600273 audio_input_flags_t flags,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700274 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800275 audio_port_handle_t *portId)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800276{
Eric Laurentdce54a12014-03-10 12:19:46 -0700277 if (mAudioPolicyManager == NULL) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800278 return NO_INIT;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800279 }
280 // already checked by client, but double-check in case the client wrapper is bypassed
Eric Laurentfe231122017-11-17 17:48:06 -0800281 if (attr->source < AUDIO_SOURCE_DEFAULT && attr->source >= AUDIO_SOURCE_CNT &&
282 attr->source != AUDIO_SOURCE_HOTWORD && attr->source != AUDIO_SOURCE_FM_TUNER) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800283 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800284 }
285
Eric Laurentb2379ba2016-05-23 17:42:12 -0700286 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -0700287 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurentb2379ba2016-05-23 17:42:12 -0700288 if (!isTrustedCallingUid(callingUid)) {
Eric Laurent9f39f8d2016-05-25 12:34:48 -0700289 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -0700290 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
291 uid = callingUid;
Eric Laurentb2379ba2016-05-23 17:42:12 -0700292 updatePid = true;
293 }
294
295 if (updatePid) {
296 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Eric Laurent9f39f8d2016-05-25 12:34:48 -0700297 ALOGW_IF(pid != (pid_t)-1 && pid != callingPid,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700298 "%s uid %d pid %d tried to pass itself off as pid %d",
299 __func__, callingUid, callingPid, pid);
300 pid = callingPid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700301 }
302
Eric Laurent7504b9e2017-08-15 18:17:26 -0700303 if ((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed(pid, uid)) {
304 return BAD_VALUE;
305 }
306
307 sp<AudioPolicyEffects>audioPolicyEffects;
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700308 {
Eric Laurent7504b9e2017-08-15 18:17:26 -0700309 status_t status;
310 AudioPolicyInterface::input_type_t inputType;
311
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700312 Mutex::Autolock _l(mLock);
313 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700314 status = mAudioPolicyManager->getInputForAttr(attr, input, session, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800315 config,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700316 flags, selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800317 &inputType, portId);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700318 audioPolicyEffects = mAudioPolicyEffects;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800319
320 if (status == NO_ERROR) {
321 // enforce permission (if any) required for each type of input
322 switch (inputType) {
323 case AudioPolicyInterface::API_INPUT_LEGACY:
324 break;
Eric Laurent82db2692015-08-07 13:59:42 -0700325 case AudioPolicyInterface::API_INPUT_TELEPHONY_RX:
326 // FIXME: use the same permission as for remote submix for now.
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800327 case AudioPolicyInterface::API_INPUT_MIX_CAPTURE:
Eric Laurentb2379ba2016-05-23 17:42:12 -0700328 if (!captureAudioOutputAllowed(pid, uid)) {
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800329 ALOGE("getInputForAttr() permission denied: capture not allowed");
330 status = PERMISSION_DENIED;
331 }
332 break;
333 case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE:
334 if (!modifyAudioRoutingAllowed()) {
335 ALOGE("getInputForAttr() permission denied: modify audio routing not allowed");
336 status = PERMISSION_DENIED;
337 }
338 break;
339 case AudioPolicyInterface::API_INPUT_INVALID:
340 default:
341 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d",
342 (int)inputType);
343 }
344 }
345
346 if (status != NO_ERROR) {
347 if (status == PERMISSION_DENIED) {
348 mAudioPolicyManager->releaseInput(*input, session);
349 }
350 return status;
351 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700352 }
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800353
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700354 if (audioPolicyEffects != 0) {
355 // create audio pre processors according to input source
Eric Laurentcaf7f482014-11-25 17:50:47 -0800356 status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700357 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800358 ALOGW("Failed to add effects on input %d", *input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700359 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800360 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800361 return NO_ERROR;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800362}
363
Eric Laurent4dc68062014-07-28 17:26:49 -0700364status_t AudioPolicyService::startInput(audio_io_handle_t input,
365 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800366{
Eric Laurentdce54a12014-03-10 12:19:46 -0700367 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800368 return NO_INIT;
369 }
370 Mutex::Autolock _l(mLock);
Eric Laurentfb66dd92016-01-28 18:32:03 -0800371 AudioPolicyInterface::concurrency_type__mask_t concurrency;
372 status_t status = mAudioPolicyManager->startInput(input, session, &concurrency);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800373
Eric Laurentfb66dd92016-01-28 18:32:03 -0800374 if (status == NO_ERROR) {
Eric Laurent43423352016-02-05 11:57:57 -0800375 LOG_ALWAYS_FATAL_IF(concurrency & ~AudioPolicyInterface::API_INPUT_CONCURRENCY_ALL,
376 "startInput(): invalid concurrency type %d", (int)concurrency);
377
Eric Laurentfb66dd92016-01-28 18:32:03 -0800378 // enforce permission (if any) required for each type of concurrency
Eric Laurent43423352016-02-05 11:57:57 -0800379 if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CALL) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800380 //TODO: check incall capture permission
Eric Laurent43423352016-02-05 11:57:57 -0800381 }
382 if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CAPTURE) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800383 //TODO: check concurrent capture permission
Eric Laurentfb66dd92016-01-28 18:32:03 -0800384 }
385 }
386
387 return status;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800388}
389
Eric Laurent4dc68062014-07-28 17:26:49 -0700390status_t AudioPolicyService::stopInput(audio_io_handle_t input,
391 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800392{
Eric Laurentdce54a12014-03-10 12:19:46 -0700393 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800394 return NO_INIT;
395 }
396 Mutex::Autolock _l(mLock);
397
Eric Laurent4dc68062014-07-28 17:26:49 -0700398 return mAudioPolicyManager->stopInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800399}
400
Eric Laurent4dc68062014-07-28 17:26:49 -0700401void AudioPolicyService::releaseInput(audio_io_handle_t input,
402 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800403{
Eric Laurentdce54a12014-03-10 12:19:46 -0700404 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800405 return;
406 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700407 sp<AudioPolicyEffects>audioPolicyEffects;
408 {
409 Mutex::Autolock _l(mLock);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700410 audioPolicyEffects = mAudioPolicyEffects;
411 }
412 if (audioPolicyEffects != 0) {
413 // release audio processors from the input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800414 status_t status = audioPolicyEffects->releaseInputEffects(input, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700415 if(status != NO_ERROR) {
416 ALOGW("Failed to release effects on input %d", input);
417 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800418 }
Eric Laurentf10c7092016-12-06 17:09:56 -0800419 {
420 Mutex::Autolock _l(mLock);
421 mAudioPolicyManager->releaseInput(input, session);
422 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800423}
424
425status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
426 int indexMin,
427 int indexMax)
428{
Eric Laurentdce54a12014-03-10 12:19:46 -0700429 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800430 return NO_INIT;
431 }
432 if (!settingsAllowed()) {
433 return PERMISSION_DENIED;
434 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800435 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800436 return BAD_VALUE;
437 }
438 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700439 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800440 return NO_ERROR;
441}
442
443status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
444 int index,
445 audio_devices_t device)
446{
Eric Laurentdce54a12014-03-10 12:19:46 -0700447 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800448 return NO_INIT;
449 }
450 if (!settingsAllowed()) {
451 return PERMISSION_DENIED;
452 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800453 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800454 return BAD_VALUE;
455 }
456 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700457 return mAudioPolicyManager->setStreamVolumeIndex(stream,
458 index,
459 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800460}
461
462status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
463 int *index,
464 audio_devices_t device)
465{
Eric Laurentdce54a12014-03-10 12:19:46 -0700466 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800467 return NO_INIT;
468 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800469 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800470 return BAD_VALUE;
471 }
472 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700473 return mAudioPolicyManager->getStreamVolumeIndex(stream,
474 index,
475 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800476}
477
478uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
479{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800480 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700481 return 0;
Eric Laurentdea15412014-10-28 15:46:45 -0700482 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700483 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800484 return 0;
485 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700486 return mAudioPolicyManager->getStrategyForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800487}
488
489//audio policy: use audio_device_t appropriately
490
491audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
492{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800493 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700494 return AUDIO_DEVICE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700495 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700496 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700497 return AUDIO_DEVICE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800498 }
Haynes Mathew Georgedfb9f3b2015-10-26 18:22:13 -0700499 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700500 return mAudioPolicyManager->getDevicesForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800501}
502
503audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
504{
505 // FIXME change return type to status_t, and return NO_INIT here
Eric Laurentdce54a12014-03-10 12:19:46 -0700506 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800507 return 0;
508 }
509 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700510 return mAudioPolicyManager->getOutputForEffect(desc);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800511}
512
513status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
514 audio_io_handle_t io,
515 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800516 audio_session_t session,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800517 int id)
518{
Eric Laurentdce54a12014-03-10 12:19:46 -0700519 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800520 return NO_INIT;
521 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700522 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700523 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800524}
525
526status_t AudioPolicyService::unregisterEffect(int id)
527{
Eric Laurentdce54a12014-03-10 12:19:46 -0700528 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800529 return NO_INIT;
530 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700531 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700532 return mAudioPolicyManager->unregisterEffect(id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800533}
534
535status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
536{
Eric Laurentdce54a12014-03-10 12:19:46 -0700537 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800538 return NO_INIT;
539 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700540 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700541 return mAudioPolicyManager->setEffectEnabled(id, enabled);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800542}
543
544bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
545{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800546 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700547 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700548 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700549 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700550 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800551 }
552 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700553 return mAudioPolicyManager->isStreamActive(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800554}
555
556bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
557{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800558 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700559 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700560 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700561 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700562 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800563 }
564 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700565 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800566}
567
568bool AudioPolicyService::isSourceActive(audio_source_t source) const
569{
Eric Laurentdce54a12014-03-10 12:19:46 -0700570 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800571 return false;
572 }
573 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700574 return mAudioPolicyManager->isSourceActive(source);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800575}
576
Glenn Kastend848eb42016-03-08 13:42:11 -0800577status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800578 effect_descriptor_t *descriptors,
579 uint32_t *count)
580{
Eric Laurentdce54a12014-03-10 12:19:46 -0700581 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800582 *count = 0;
583 return NO_INIT;
584 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700585 sp<AudioPolicyEffects>audioPolicyEffects;
586 {
587 Mutex::Autolock _l(mLock);
588 audioPolicyEffects = mAudioPolicyEffects;
589 }
590 if (audioPolicyEffects == 0) {
591 *count = 0;
592 return NO_INIT;
593 }
Eric Laurentfb66dd92016-01-28 18:32:03 -0800594 return audioPolicyEffects->queryDefaultInputEffects(
595 (audio_session_t)audioSession, descriptors, count);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800596}
597
598bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
599{
Eric Laurentdce54a12014-03-10 12:19:46 -0700600 if (mAudioPolicyManager == NULL) {
601 ALOGV("mAudioPolicyManager == NULL");
Eric Laurent2d388ec2014-03-07 13:25:54 -0800602 return false;
603 }
Andy Hung2ddee192015-12-18 17:34:44 -0800604 Mutex::Autolock _l(mLock);
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700605 Mutex::Autolock _le(mEffectsLock); // isOffloadSupported queries for
606 // non-offloadable effects
Eric Laurentdce54a12014-03-10 12:19:46 -0700607 return mAudioPolicyManager->isOffloadSupported(info);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800608}
609
Eric Laurent6a94d692014-05-20 11:18:06 -0700610status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
611 audio_port_type_t type,
Eric Laurent203b1a12014-04-01 10:34:16 -0700612 unsigned int *num_ports,
Eric Laurent6a94d692014-05-20 11:18:06 -0700613 struct audio_port *ports,
614 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700615{
Eric Laurent6a94d692014-05-20 11:18:06 -0700616 Mutex::Autolock _l(mLock);
617 if (mAudioPolicyManager == NULL) {
618 return NO_INIT;
619 }
620
621 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700622}
623
Eric Laurent6a94d692014-05-20 11:18:06 -0700624status_t AudioPolicyService::getAudioPort(struct audio_port *port)
Eric Laurent203b1a12014-04-01 10:34:16 -0700625{
Eric Laurent6a94d692014-05-20 11:18:06 -0700626 Mutex::Autolock _l(mLock);
627 if (mAudioPolicyManager == NULL) {
628 return NO_INIT;
629 }
630
631 return mAudioPolicyManager->getAudioPort(port);
Eric Laurent203b1a12014-04-01 10:34:16 -0700632}
633
Eric Laurent6a94d692014-05-20 11:18:06 -0700634status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
635 audio_patch_handle_t *handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700636{
Eric Laurent6a94d692014-05-20 11:18:06 -0700637 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700638 if(!modifyAudioRoutingAllowed()) {
639 return PERMISSION_DENIED;
640 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700641 if (mAudioPolicyManager == NULL) {
642 return NO_INIT;
643 }
644 return mAudioPolicyManager->createAudioPatch(patch, handle,
645 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700646}
647
Eric Laurent6a94d692014-05-20 11:18:06 -0700648status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700649{
Eric Laurent6a94d692014-05-20 11:18:06 -0700650 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700651 if(!modifyAudioRoutingAllowed()) {
652 return PERMISSION_DENIED;
653 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700654 if (mAudioPolicyManager == NULL) {
655 return NO_INIT;
656 }
657
658 return mAudioPolicyManager->releaseAudioPatch(handle,
659 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700660}
661
662status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
Eric Laurent6a94d692014-05-20 11:18:06 -0700663 struct audio_patch *patches,
664 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700665{
Eric Laurent6a94d692014-05-20 11:18:06 -0700666 Mutex::Autolock _l(mLock);
667 if (mAudioPolicyManager == NULL) {
668 return NO_INIT;
669 }
670
671 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700672}
673
Eric Laurent6a94d692014-05-20 11:18:06 -0700674status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent203b1a12014-04-01 10:34:16 -0700675{
Eric Laurent6a94d692014-05-20 11:18:06 -0700676 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700677 if(!modifyAudioRoutingAllowed()) {
678 return PERMISSION_DENIED;
679 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700680 if (mAudioPolicyManager == NULL) {
681 return NO_INIT;
682 }
683
684 return mAudioPolicyManager->setAudioPortConfig(config);
Eric Laurent203b1a12014-04-01 10:34:16 -0700685}
Eric Laurent2d388ec2014-03-07 13:25:54 -0800686
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700687status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
688 audio_io_handle_t *ioHandle,
689 audio_devices_t *device)
690{
Andy Hungf759b8c2017-08-15 12:48:54 -0700691 Mutex::Autolock _l(mLock);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700692 if (mAudioPolicyManager == NULL) {
693 return NO_INIT;
694 }
695
696 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
697}
698
699status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
700{
Andy Hungf759b8c2017-08-15 12:48:54 -0700701 Mutex::Autolock _l(mLock);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700702 if (mAudioPolicyManager == NULL) {
703 return NO_INIT;
704 }
705
706 return mAudioPolicyManager->releaseSoundTriggerSession(session);
707}
708
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700709status_t AudioPolicyService::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800710{
711 Mutex::Autolock _l(mLock);
712 if(!modifyAudioRoutingAllowed()) {
713 return PERMISSION_DENIED;
714 }
715 if (mAudioPolicyManager == NULL) {
716 return NO_INIT;
717 }
718 if (registration) {
719 return mAudioPolicyManager->registerPolicyMixes(mixes);
720 } else {
721 return mAudioPolicyManager->unregisterPolicyMixes(mixes);
722 }
723}
724
Eric Laurent554a2772015-04-10 11:29:24 -0700725status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
726 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -0700727 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700728{
729 Mutex::Autolock _l(mLock);
730 if (mAudioPolicyManager == NULL) {
731 return NO_INIT;
732 }
733
Eric Laurentd60560a2015-04-10 11:31:20 -0700734 return mAudioPolicyManager->startAudioSource(source, attributes, handle,
735 IPCThreadState::self()->getCallingUid());
Eric Laurent554a2772015-04-10 11:29:24 -0700736}
737
Glenn Kasten559d4392016-03-29 13:42:57 -0700738status_t AudioPolicyService::stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700739{
740 Mutex::Autolock _l(mLock);
741 if (mAudioPolicyManager == NULL) {
742 return NO_INIT;
743 }
744
745 return mAudioPolicyManager->stopAudioSource(handle);
746}
747
Andy Hung2ddee192015-12-18 17:34:44 -0800748status_t AudioPolicyService::setMasterMono(bool mono)
749{
750 if (mAudioPolicyManager == NULL) {
751 return NO_INIT;
752 }
753 if (!settingsAllowed()) {
754 return PERMISSION_DENIED;
755 }
756 Mutex::Autolock _l(mLock);
757 return mAudioPolicyManager->setMasterMono(mono);
758}
759
760status_t AudioPolicyService::getMasterMono(bool *mono)
761{
762 if (mAudioPolicyManager == NULL) {
763 return NO_INIT;
764 }
765 Mutex::Autolock _l(mLock);
766 return mAudioPolicyManager->getMasterMono(mono);
767}
768
Eric Laurentac9cef52017-06-09 15:46:26 -0700769
770float AudioPolicyService::getStreamVolumeDB(
771 audio_stream_type_t stream, int index, audio_devices_t device)
772{
773 if (mAudioPolicyManager == NULL) {
774 return NAN;
775 }
776 Mutex::Autolock _l(mLock);
777 return mAudioPolicyManager->getStreamVolumeDB(stream, index, device);
778}
779
780
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800781} // namespace android