blob: 1d4386cd9eb32b06240795bd24c7dad1f01afe95 [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,
Eric Laurent9ae8c592017-06-22 17:17:09 -0700170 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 Laurent9ae8c592017-06-22 17:17:09 -0700280 audio_port_handle_t *selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800281 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 Laurentb2379ba2016-05-23 17:42:12 -0700292 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -0700293 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurentb2379ba2016-05-23 17:42:12 -0700294 if (!isTrustedCallingUid(callingUid)) {
Eric Laurent9f39f8d2016-05-25 12:34:48 -0700295 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -0700296 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
297 uid = callingUid;
Eric Laurentb2379ba2016-05-23 17:42:12 -0700298 updatePid = true;
299 }
300
301 if (updatePid) {
302 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Eric Laurent9f39f8d2016-05-25 12:34:48 -0700303 ALOGW_IF(pid != (pid_t)-1 && pid != callingPid,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700304 "%s uid %d pid %d tried to pass itself off as pid %d",
305 __func__, callingUid, callingPid, pid);
306 pid = callingPid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700307 }
308
Eric Laurent7504b9e2017-08-15 18:17:26 -0700309 if ((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed(pid, uid)) {
310 return BAD_VALUE;
311 }
312
313 sp<AudioPolicyEffects>audioPolicyEffects;
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700314 {
Eric Laurent7504b9e2017-08-15 18:17:26 -0700315 status_t status;
316 AudioPolicyInterface::input_type_t inputType;
317
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700318 Mutex::Autolock _l(mLock);
319 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700320 status = mAudioPolicyManager->getInputForAttr(attr, input, session, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800321 config,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700322 flags, selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800323 &inputType, portId);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700324 audioPolicyEffects = mAudioPolicyEffects;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800325
326 if (status == NO_ERROR) {
327 // enforce permission (if any) required for each type of input
328 switch (inputType) {
329 case AudioPolicyInterface::API_INPUT_LEGACY:
330 break;
Eric Laurent82db2692015-08-07 13:59:42 -0700331 case AudioPolicyInterface::API_INPUT_TELEPHONY_RX:
332 // FIXME: use the same permission as for remote submix for now.
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800333 case AudioPolicyInterface::API_INPUT_MIX_CAPTURE:
Eric Laurentb2379ba2016-05-23 17:42:12 -0700334 if (!captureAudioOutputAllowed(pid, uid)) {
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800335 ALOGE("getInputForAttr() permission denied: capture not allowed");
336 status = PERMISSION_DENIED;
337 }
338 break;
339 case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE:
340 if (!modifyAudioRoutingAllowed()) {
341 ALOGE("getInputForAttr() permission denied: modify audio routing not allowed");
342 status = PERMISSION_DENIED;
343 }
344 break;
345 case AudioPolicyInterface::API_INPUT_INVALID:
346 default:
347 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d",
348 (int)inputType);
349 }
350 }
351
352 if (status != NO_ERROR) {
353 if (status == PERMISSION_DENIED) {
354 mAudioPolicyManager->releaseInput(*input, session);
355 }
356 return status;
357 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700358 }
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800359
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700360 if (audioPolicyEffects != 0) {
361 // create audio pre processors according to input source
Eric Laurentcaf7f482014-11-25 17:50:47 -0800362 status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700363 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800364 ALOGW("Failed to add effects on input %d", *input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700365 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800366 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800367 return NO_ERROR;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800368}
369
Eric Laurent4dc68062014-07-28 17:26:49 -0700370status_t AudioPolicyService::startInput(audio_io_handle_t input,
371 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800372{
Eric Laurentdce54a12014-03-10 12:19:46 -0700373 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800374 return NO_INIT;
375 }
376 Mutex::Autolock _l(mLock);
Eric Laurentfb66dd92016-01-28 18:32:03 -0800377 AudioPolicyInterface::concurrency_type__mask_t concurrency;
378 status_t status = mAudioPolicyManager->startInput(input, session, &concurrency);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800379
Eric Laurentfb66dd92016-01-28 18:32:03 -0800380 if (status == NO_ERROR) {
Eric Laurent43423352016-02-05 11:57:57 -0800381 LOG_ALWAYS_FATAL_IF(concurrency & ~AudioPolicyInterface::API_INPUT_CONCURRENCY_ALL,
382 "startInput(): invalid concurrency type %d", (int)concurrency);
383
Eric Laurentfb66dd92016-01-28 18:32:03 -0800384 // enforce permission (if any) required for each type of concurrency
Eric Laurent43423352016-02-05 11:57:57 -0800385 if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CALL) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800386 //TODO: check incall capture permission
Eric Laurent43423352016-02-05 11:57:57 -0800387 }
388 if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CAPTURE) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800389 //TODO: check concurrent capture permission
Eric Laurentfb66dd92016-01-28 18:32:03 -0800390 }
391 }
392
393 return status;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800394}
395
Eric Laurent4dc68062014-07-28 17:26:49 -0700396status_t AudioPolicyService::stopInput(audio_io_handle_t input,
397 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800398{
Eric Laurentdce54a12014-03-10 12:19:46 -0700399 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800400 return NO_INIT;
401 }
402 Mutex::Autolock _l(mLock);
403
Eric Laurent4dc68062014-07-28 17:26:49 -0700404 return mAudioPolicyManager->stopInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800405}
406
Eric Laurent4dc68062014-07-28 17:26:49 -0700407void AudioPolicyService::releaseInput(audio_io_handle_t input,
408 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800409{
Eric Laurentdce54a12014-03-10 12:19:46 -0700410 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800411 return;
412 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700413 sp<AudioPolicyEffects>audioPolicyEffects;
414 {
415 Mutex::Autolock _l(mLock);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700416 audioPolicyEffects = mAudioPolicyEffects;
417 }
418 if (audioPolicyEffects != 0) {
419 // release audio processors from the input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800420 status_t status = audioPolicyEffects->releaseInputEffects(input, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700421 if(status != NO_ERROR) {
422 ALOGW("Failed to release effects on input %d", input);
423 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800424 }
Eric Laurentf10c7092016-12-06 17:09:56 -0800425 {
426 Mutex::Autolock _l(mLock);
427 mAudioPolicyManager->releaseInput(input, session);
428 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800429}
430
431status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
432 int indexMin,
433 int indexMax)
434{
Eric Laurentdce54a12014-03-10 12:19:46 -0700435 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800436 return NO_INIT;
437 }
438 if (!settingsAllowed()) {
439 return PERMISSION_DENIED;
440 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800441 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800442 return BAD_VALUE;
443 }
444 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700445 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800446 return NO_ERROR;
447}
448
449status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
450 int index,
451 audio_devices_t device)
452{
Eric Laurentdce54a12014-03-10 12:19:46 -0700453 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800454 return NO_INIT;
455 }
456 if (!settingsAllowed()) {
457 return PERMISSION_DENIED;
458 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800459 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800460 return BAD_VALUE;
461 }
462 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700463 return mAudioPolicyManager->setStreamVolumeIndex(stream,
464 index,
465 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800466}
467
468status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
469 int *index,
470 audio_devices_t device)
471{
Eric Laurentdce54a12014-03-10 12:19:46 -0700472 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800473 return NO_INIT;
474 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800475 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800476 return BAD_VALUE;
477 }
478 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700479 return mAudioPolicyManager->getStreamVolumeIndex(stream,
480 index,
481 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800482}
483
484uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
485{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800486 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700487 return 0;
Eric Laurentdea15412014-10-28 15:46:45 -0700488 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700489 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800490 return 0;
491 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700492 return mAudioPolicyManager->getStrategyForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800493}
494
495//audio policy: use audio_device_t appropriately
496
497audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
498{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800499 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700500 return AUDIO_DEVICE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700501 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700502 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700503 return AUDIO_DEVICE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800504 }
Haynes Mathew Georgedfb9f3b2015-10-26 18:22:13 -0700505 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700506 return mAudioPolicyManager->getDevicesForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800507}
508
509audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
510{
511 // FIXME change return type to status_t, and return NO_INIT here
Eric Laurentdce54a12014-03-10 12:19:46 -0700512 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800513 return 0;
514 }
515 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700516 return mAudioPolicyManager->getOutputForEffect(desc);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800517}
518
519status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
520 audio_io_handle_t io,
521 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800522 audio_session_t session,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800523 int id)
524{
Eric Laurentdce54a12014-03-10 12:19:46 -0700525 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800526 return NO_INIT;
527 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700528 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700529 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800530}
531
532status_t AudioPolicyService::unregisterEffect(int id)
533{
Eric Laurentdce54a12014-03-10 12:19:46 -0700534 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800535 return NO_INIT;
536 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700537 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700538 return mAudioPolicyManager->unregisterEffect(id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800539}
540
541status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
542{
Eric Laurentdce54a12014-03-10 12:19:46 -0700543 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800544 return NO_INIT;
545 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700546 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700547 return mAudioPolicyManager->setEffectEnabled(id, enabled);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800548}
549
550bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
551{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800552 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700553 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700554 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700555 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700556 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800557 }
558 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700559 return mAudioPolicyManager->isStreamActive(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800560}
561
562bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
563{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800564 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700565 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700566 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700567 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700568 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800569 }
570 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700571 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800572}
573
574bool AudioPolicyService::isSourceActive(audio_source_t source) const
575{
Eric Laurentdce54a12014-03-10 12:19:46 -0700576 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800577 return false;
578 }
579 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700580 return mAudioPolicyManager->isSourceActive(source);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800581}
582
Glenn Kastend848eb42016-03-08 13:42:11 -0800583status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800584 effect_descriptor_t *descriptors,
585 uint32_t *count)
586{
Eric Laurentdce54a12014-03-10 12:19:46 -0700587 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800588 *count = 0;
589 return NO_INIT;
590 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700591 sp<AudioPolicyEffects>audioPolicyEffects;
592 {
593 Mutex::Autolock _l(mLock);
594 audioPolicyEffects = mAudioPolicyEffects;
595 }
596 if (audioPolicyEffects == 0) {
597 *count = 0;
598 return NO_INIT;
599 }
Eric Laurentfb66dd92016-01-28 18:32:03 -0800600 return audioPolicyEffects->queryDefaultInputEffects(
601 (audio_session_t)audioSession, descriptors, count);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800602}
603
604bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
605{
Eric Laurentdce54a12014-03-10 12:19:46 -0700606 if (mAudioPolicyManager == NULL) {
607 ALOGV("mAudioPolicyManager == NULL");
Eric Laurent2d388ec2014-03-07 13:25:54 -0800608 return false;
609 }
Andy Hung2ddee192015-12-18 17:34:44 -0800610 Mutex::Autolock _l(mLock);
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700611 Mutex::Autolock _le(mEffectsLock); // isOffloadSupported queries for
612 // non-offloadable effects
Eric Laurentdce54a12014-03-10 12:19:46 -0700613 return mAudioPolicyManager->isOffloadSupported(info);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800614}
615
Eric Laurent6a94d692014-05-20 11:18:06 -0700616status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
617 audio_port_type_t type,
Eric Laurent203b1a12014-04-01 10:34:16 -0700618 unsigned int *num_ports,
Eric Laurent6a94d692014-05-20 11:18:06 -0700619 struct audio_port *ports,
620 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700621{
Eric Laurent6a94d692014-05-20 11:18:06 -0700622 Mutex::Autolock _l(mLock);
623 if (mAudioPolicyManager == NULL) {
624 return NO_INIT;
625 }
626
627 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700628}
629
Eric Laurent6a94d692014-05-20 11:18:06 -0700630status_t AudioPolicyService::getAudioPort(struct audio_port *port)
Eric Laurent203b1a12014-04-01 10:34:16 -0700631{
Eric Laurent6a94d692014-05-20 11:18:06 -0700632 Mutex::Autolock _l(mLock);
633 if (mAudioPolicyManager == NULL) {
634 return NO_INIT;
635 }
636
637 return mAudioPolicyManager->getAudioPort(port);
Eric Laurent203b1a12014-04-01 10:34:16 -0700638}
639
Eric Laurent6a94d692014-05-20 11:18:06 -0700640status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
641 audio_patch_handle_t *handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700642{
Eric Laurent6a94d692014-05-20 11:18:06 -0700643 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700644 if(!modifyAudioRoutingAllowed()) {
645 return PERMISSION_DENIED;
646 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700647 if (mAudioPolicyManager == NULL) {
648 return NO_INIT;
649 }
650 return mAudioPolicyManager->createAudioPatch(patch, handle,
651 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700652}
653
Eric Laurent6a94d692014-05-20 11:18:06 -0700654status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700655{
Eric Laurent6a94d692014-05-20 11:18:06 -0700656 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700657 if(!modifyAudioRoutingAllowed()) {
658 return PERMISSION_DENIED;
659 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700660 if (mAudioPolicyManager == NULL) {
661 return NO_INIT;
662 }
663
664 return mAudioPolicyManager->releaseAudioPatch(handle,
665 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700666}
667
668status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
Eric Laurent6a94d692014-05-20 11:18:06 -0700669 struct audio_patch *patches,
670 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700671{
Eric Laurent6a94d692014-05-20 11:18:06 -0700672 Mutex::Autolock _l(mLock);
673 if (mAudioPolicyManager == NULL) {
674 return NO_INIT;
675 }
676
677 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700678}
679
Eric Laurent6a94d692014-05-20 11:18:06 -0700680status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent203b1a12014-04-01 10:34:16 -0700681{
Eric Laurent6a94d692014-05-20 11:18:06 -0700682 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700683 if(!modifyAudioRoutingAllowed()) {
684 return PERMISSION_DENIED;
685 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700686 if (mAudioPolicyManager == NULL) {
687 return NO_INIT;
688 }
689
690 return mAudioPolicyManager->setAudioPortConfig(config);
Eric Laurent203b1a12014-04-01 10:34:16 -0700691}
Eric Laurent2d388ec2014-03-07 13:25:54 -0800692
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700693status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
694 audio_io_handle_t *ioHandle,
695 audio_devices_t *device)
696{
697 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{
706 if (mAudioPolicyManager == NULL) {
707 return NO_INIT;
708 }
709
710 return mAudioPolicyManager->releaseSoundTriggerSession(session);
711}
712
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700713status_t AudioPolicyService::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800714{
715 Mutex::Autolock _l(mLock);
716 if(!modifyAudioRoutingAllowed()) {
717 return PERMISSION_DENIED;
718 }
719 if (mAudioPolicyManager == NULL) {
720 return NO_INIT;
721 }
722 if (registration) {
723 return mAudioPolicyManager->registerPolicyMixes(mixes);
724 } else {
725 return mAudioPolicyManager->unregisterPolicyMixes(mixes);
726 }
727}
728
Eric Laurent554a2772015-04-10 11:29:24 -0700729status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
730 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -0700731 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700732{
733 Mutex::Autolock _l(mLock);
734 if (mAudioPolicyManager == NULL) {
735 return NO_INIT;
736 }
737
Eric Laurentd60560a2015-04-10 11:31:20 -0700738 return mAudioPolicyManager->startAudioSource(source, attributes, handle,
739 IPCThreadState::self()->getCallingUid());
Eric Laurent554a2772015-04-10 11:29:24 -0700740}
741
Glenn Kasten559d4392016-03-29 13:42:57 -0700742status_t AudioPolicyService::stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700743{
744 Mutex::Autolock _l(mLock);
745 if (mAudioPolicyManager == NULL) {
746 return NO_INIT;
747 }
748
749 return mAudioPolicyManager->stopAudioSource(handle);
750}
751
Andy Hung2ddee192015-12-18 17:34:44 -0800752status_t AudioPolicyService::setMasterMono(bool mono)
753{
754 if (mAudioPolicyManager == NULL) {
755 return NO_INIT;
756 }
757 if (!settingsAllowed()) {
758 return PERMISSION_DENIED;
759 }
760 Mutex::Autolock _l(mLock);
761 return mAudioPolicyManager->setMasterMono(mono);
762}
763
764status_t AudioPolicyService::getMasterMono(bool *mono)
765{
766 if (mAudioPolicyManager == NULL) {
767 return NO_INIT;
768 }
769 Mutex::Autolock _l(mLock);
770 return mAudioPolicyManager->getMasterMono(mono);
771}
772
Eric Laurentac9cef52017-06-09 15:46:26 -0700773
774float AudioPolicyService::getStreamVolumeDB(
775 audio_stream_type_t stream, int index, audio_devices_t device)
776{
777 if (mAudioPolicyManager == NULL) {
778 return NAN;
779 }
780 Mutex::Autolock _l(mLock);
781 return mAudioPolicyManager->getStreamVolumeDB(stream, index, device);
782}
783
784
Eric Laurent2d388ec2014-03-07 13:25:54 -0800785}; // namespace android