blob: 0387ee6cf9d83c171649b5d7bbf2fc803d00fd9b [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 Laurente83b55d2014-11-14 10:06:21 -0800168 uint32_t samplingRate,
169 audio_format_t format,
170 audio_channel_mask_t channelMask,
171 audio_output_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -0600172 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800173 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700174{
175 if (mAudioPolicyManager == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800176 return NO_INIT;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700177 }
178 ALOGV("getOutput()");
179 Mutex::Autolock _l(mLock);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700180
Marco Nelissendcb346b2015-09-09 10:47:29 -0700181 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
182 if (!isTrustedCallingUid(callingUid) || uid == (uid_t)-1) {
183 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
184 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
185 uid = callingUid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700186 }
187 return mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid, samplingRate,
Paul McLean466dc8e2015-04-17 13:15:36 -0600188 format, channelMask, flags, selectedDeviceId, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700189}
190
Eric Laurent2d388ec2014-03-07 13:25:54 -0800191status_t AudioPolicyService::startOutput(audio_io_handle_t output,
192 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800193 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800194{
Eric Laurentdea15412014-10-28 15:46:45 -0700195 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
196 return BAD_VALUE;
197 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700198 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800199 return NO_INIT;
200 }
201 ALOGV("startOutput()");
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700202 sp<AudioPolicyEffects>audioPolicyEffects;
203 {
204 Mutex::Autolock _l(mLock);
205 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800206 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700207 if (audioPolicyEffects != 0) {
208 // create audio processors according to stream
209 status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session);
210 if (status != NO_ERROR && status != ALREADY_EXISTS) {
211 ALOGW("Failed to add effects on session %d", session);
212 }
213 }
214 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700215 return mAudioPolicyManager->startOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800216}
217
218status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
219 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800220 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800221{
Eric Laurentdea15412014-10-28 15:46:45 -0700222 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
223 return BAD_VALUE;
224 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700225 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800226 return NO_INIT;
227 }
228 ALOGV("stopOutput()");
229 mOutputCommandThread->stopOutputCommand(output, stream, session);
230 return NO_ERROR;
231}
232
233status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
234 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800235 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800236{
237 ALOGV("doStopOutput from tid %d", gettid());
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700238 sp<AudioPolicyEffects>audioPolicyEffects;
239 {
240 Mutex::Autolock _l(mLock);
241 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800242 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700243 if (audioPolicyEffects != 0) {
244 // release audio processors from the stream
245 status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
246 if (status != NO_ERROR && status != ALREADY_EXISTS) {
247 ALOGW("Failed to release effects on session %d", session);
248 }
249 }
250 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700251 return mAudioPolicyManager->stopOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800252}
253
Eric Laurente83b55d2014-11-14 10:06:21 -0800254void AudioPolicyService::releaseOutput(audio_io_handle_t output,
255 audio_stream_type_t stream,
256 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800257{
Eric Laurentdce54a12014-03-10 12:19:46 -0700258 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800259 return;
260 }
261 ALOGV("releaseOutput()");
Eric Laurente83b55d2014-11-14 10:06:21 -0800262 mOutputCommandThread->releaseOutputCommand(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800263}
264
Eric Laurente83b55d2014-11-14 10:06:21 -0800265void AudioPolicyService::doReleaseOutput(audio_io_handle_t output,
266 audio_stream_type_t stream,
267 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800268{
269 ALOGV("doReleaseOutput from tid %d", gettid());
270 Mutex::Autolock _l(mLock);
Eric Laurente83b55d2014-11-14 10:06:21 -0800271 mAudioPolicyManager->releaseOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800272}
273
Eric Laurentcaf7f482014-11-25 17:50:47 -0800274status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr,
275 audio_io_handle_t *input,
276 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700277 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700278 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800279 uint32_t samplingRate,
280 audio_format_t format,
281 audio_channel_mask_t channelMask,
Paul McLean466dc8e2015-04-17 13:15:36 -0600282 audio_input_flags_t flags,
283 audio_port_handle_t selectedDeviceId)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800284{
Eric Laurentdce54a12014-03-10 12:19:46 -0700285 if (mAudioPolicyManager == NULL) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800286 return NO_INIT;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800287 }
288 // already checked by client, but double-check in case the client wrapper is bypassed
Eric Laurentcaf7f482014-11-25 17:50:47 -0800289 if (attr->source >= AUDIO_SOURCE_CNT && attr->source != AUDIO_SOURCE_HOTWORD &&
290 attr->source != AUDIO_SOURCE_FM_TUNER) {
291 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800292 }
293
Eric Laurentab300c82015-04-13 13:47:33 -0700294 if ((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800295 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800296 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700297 sp<AudioPolicyEffects>audioPolicyEffects;
Eric Laurentcaf7f482014-11-25 17:50:47 -0800298 status_t status;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800299 AudioPolicyInterface::input_type_t inputType;
Eric Laurentb2379ba2016-05-23 17:42:12 -0700300
301 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -0700302 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurentb2379ba2016-05-23 17:42:12 -0700303 if (!isTrustedCallingUid(callingUid)) {
Eric Laurent9f39f8d2016-05-25 12:34:48 -0700304 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -0700305 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
306 uid = callingUid;
Eric Laurentb2379ba2016-05-23 17:42:12 -0700307 updatePid = true;
308 }
309
310 if (updatePid) {
311 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Eric Laurent9f39f8d2016-05-25 12:34:48 -0700312 ALOGW_IF(pid != (pid_t)-1 && pid != callingPid,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700313 "%s uid %d pid %d tried to pass itself off as pid %d",
314 __func__, callingUid, callingPid, pid);
315 pid = callingPid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700316 }
317
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700318 {
319 Mutex::Autolock _l(mLock);
320 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700321 status = mAudioPolicyManager->getInputForAttr(attr, input, session, uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800322 samplingRate, format, channelMask,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700323 flags, selectedDeviceId,
324 &inputType);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700325 audioPolicyEffects = mAudioPolicyEffects;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800326
327 if (status == NO_ERROR) {
328 // enforce permission (if any) required for each type of input
329 switch (inputType) {
330 case AudioPolicyInterface::API_INPUT_LEGACY:
331 break;
Eric Laurent82db2692015-08-07 13:59:42 -0700332 case AudioPolicyInterface::API_INPUT_TELEPHONY_RX:
333 // FIXME: use the same permission as for remote submix for now.
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800334 case AudioPolicyInterface::API_INPUT_MIX_CAPTURE:
Eric Laurentb2379ba2016-05-23 17:42:12 -0700335 if (!captureAudioOutputAllowed(pid, uid)) {
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800336 ALOGE("getInputForAttr() permission denied: capture not allowed");
337 status = PERMISSION_DENIED;
338 }
339 break;
340 case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE:
341 if (!modifyAudioRoutingAllowed()) {
342 ALOGE("getInputForAttr() permission denied: modify audio routing not allowed");
343 status = PERMISSION_DENIED;
344 }
345 break;
346 case AudioPolicyInterface::API_INPUT_INVALID:
347 default:
348 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d",
349 (int)inputType);
350 }
351 }
352
353 if (status != NO_ERROR) {
354 if (status == PERMISSION_DENIED) {
355 mAudioPolicyManager->releaseInput(*input, session);
356 }
357 return status;
358 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700359 }
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800360
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700361 if (audioPolicyEffects != 0) {
362 // create audio pre processors according to input source
Eric Laurentcaf7f482014-11-25 17:50:47 -0800363 status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700364 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800365 ALOGW("Failed to add effects on input %d", *input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700366 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800367 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800368 return NO_ERROR;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800369}
370
Eric Laurent4dc68062014-07-28 17:26:49 -0700371status_t AudioPolicyService::startInput(audio_io_handle_t input,
372 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800373{
Eric Laurentdce54a12014-03-10 12:19:46 -0700374 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800375 return NO_INIT;
376 }
377 Mutex::Autolock _l(mLock);
378
Eric Laurent232f26f2016-02-17 18:06:27 -0800379 return mAudioPolicyManager->startInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800380}
381
Eric Laurent4dc68062014-07-28 17:26:49 -0700382status_t AudioPolicyService::stopInput(audio_io_handle_t input,
383 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800384{
Eric Laurentdce54a12014-03-10 12:19:46 -0700385 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800386 return NO_INIT;
387 }
388 Mutex::Autolock _l(mLock);
389
Eric Laurent4dc68062014-07-28 17:26:49 -0700390 return mAudioPolicyManager->stopInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800391}
392
Eric Laurent4dc68062014-07-28 17:26:49 -0700393void AudioPolicyService::releaseInput(audio_io_handle_t input,
394 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800395{
Eric Laurentdce54a12014-03-10 12:19:46 -0700396 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800397 return;
398 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700399 sp<AudioPolicyEffects>audioPolicyEffects;
400 {
401 Mutex::Autolock _l(mLock);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700402 audioPolicyEffects = mAudioPolicyEffects;
403 }
404 if (audioPolicyEffects != 0) {
405 // release audio processors from the input
Eric Laurent232f26f2016-02-17 18:06:27 -0800406 status_t status = audioPolicyEffects->releaseInputEffects(input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700407 if(status != NO_ERROR) {
408 ALOGW("Failed to release effects on input %d", input);
409 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800410 }
Eric Laurentb378b732016-12-01 15:28:29 -0800411 {
412 Mutex::Autolock _l(mLock);
413 mAudioPolicyManager->releaseInput(input, session);
414 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800415}
416
417status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
418 int indexMin,
419 int indexMax)
420{
Eric Laurentdce54a12014-03-10 12:19:46 -0700421 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800422 return NO_INIT;
423 }
424 if (!settingsAllowed()) {
425 return PERMISSION_DENIED;
426 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800427 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800428 return BAD_VALUE;
429 }
430 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700431 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800432 return NO_ERROR;
433}
434
435status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
436 int index,
437 audio_devices_t device)
438{
Eric Laurentdce54a12014-03-10 12:19:46 -0700439 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800440 return NO_INIT;
441 }
442 if (!settingsAllowed()) {
443 return PERMISSION_DENIED;
444 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800445 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800446 return BAD_VALUE;
447 }
448 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700449 return mAudioPolicyManager->setStreamVolumeIndex(stream,
450 index,
451 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800452}
453
454status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
455 int *index,
456 audio_devices_t device)
457{
Eric Laurentdce54a12014-03-10 12:19:46 -0700458 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800459 return NO_INIT;
460 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800461 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800462 return BAD_VALUE;
463 }
464 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700465 return mAudioPolicyManager->getStreamVolumeIndex(stream,
466 index,
467 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800468}
469
470uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
471{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800472 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700473 return 0;
Eric Laurentdea15412014-10-28 15:46:45 -0700474 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700475 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800476 return 0;
477 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700478 return mAudioPolicyManager->getStrategyForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800479}
480
481//audio policy: use audio_device_t appropriately
482
483audio_devices_t AudioPolicyService::getDevicesForStream(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 AUDIO_DEVICE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700487 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700488 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700489 return AUDIO_DEVICE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800490 }
Haynes Mathew Georgedfb9f3b2015-10-26 18:22:13 -0700491 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700492 return mAudioPolicyManager->getDevicesForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800493}
494
495audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
496{
497 // FIXME change return type to status_t, and return NO_INIT here
Eric Laurentdce54a12014-03-10 12:19:46 -0700498 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800499 return 0;
500 }
501 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700502 return mAudioPolicyManager->getOutputForEffect(desc);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800503}
504
505status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
506 audio_io_handle_t io,
507 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800508 audio_session_t session,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800509 int id)
510{
Eric Laurentdce54a12014-03-10 12:19:46 -0700511 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800512 return NO_INIT;
513 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700514 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700515 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800516}
517
518status_t AudioPolicyService::unregisterEffect(int id)
519{
Eric Laurentdce54a12014-03-10 12:19:46 -0700520 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800521 return NO_INIT;
522 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700523 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700524 return mAudioPolicyManager->unregisterEffect(id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800525}
526
527status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
528{
Eric Laurentdce54a12014-03-10 12:19:46 -0700529 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800530 return NO_INIT;
531 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700532 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700533 return mAudioPolicyManager->setEffectEnabled(id, enabled);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800534}
535
536bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
537{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800538 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700539 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700540 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700541 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700542 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800543 }
544 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700545 return mAudioPolicyManager->isStreamActive(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800546}
547
548bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
549{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800550 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700551 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700552 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700553 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700554 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800555 }
556 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700557 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800558}
559
560bool AudioPolicyService::isSourceActive(audio_source_t source) const
561{
Eric Laurentdce54a12014-03-10 12:19:46 -0700562 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800563 return false;
564 }
565 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700566 return mAudioPolicyManager->isSourceActive(source);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800567}
568
Glenn Kastend848eb42016-03-08 13:42:11 -0800569status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800570 effect_descriptor_t *descriptors,
571 uint32_t *count)
572{
Eric Laurentdce54a12014-03-10 12:19:46 -0700573 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800574 *count = 0;
575 return NO_INIT;
576 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700577 sp<AudioPolicyEffects>audioPolicyEffects;
578 {
579 Mutex::Autolock _l(mLock);
580 audioPolicyEffects = mAudioPolicyEffects;
581 }
582 if (audioPolicyEffects == 0) {
583 *count = 0;
584 return NO_INIT;
585 }
Eric Laurent232f26f2016-02-17 18:06:27 -0800586 return audioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800587}
588
589bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
590{
Eric Laurentdce54a12014-03-10 12:19:46 -0700591 if (mAudioPolicyManager == NULL) {
592 ALOGV("mAudioPolicyManager == NULL");
Eric Laurent2d388ec2014-03-07 13:25:54 -0800593 return false;
594 }
Andy Hung2ddee192015-12-18 17:34:44 -0800595 Mutex::Autolock _l(mLock);
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700596 Mutex::Autolock _le(mEffectsLock); // isOffloadSupported queries for
597 // non-offloadable effects
Eric Laurentdce54a12014-03-10 12:19:46 -0700598 return mAudioPolicyManager->isOffloadSupported(info);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800599}
600
Eric Laurent6a94d692014-05-20 11:18:06 -0700601status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
602 audio_port_type_t type,
Eric Laurent203b1a12014-04-01 10:34:16 -0700603 unsigned int *num_ports,
Eric Laurent6a94d692014-05-20 11:18:06 -0700604 struct audio_port *ports,
605 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700606{
Eric Laurent6a94d692014-05-20 11:18:06 -0700607 Mutex::Autolock _l(mLock);
608 if (mAudioPolicyManager == NULL) {
609 return NO_INIT;
610 }
611
612 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700613}
614
Eric Laurent6a94d692014-05-20 11:18:06 -0700615status_t AudioPolicyService::getAudioPort(struct audio_port *port)
Eric Laurent203b1a12014-04-01 10:34:16 -0700616{
Eric Laurent6a94d692014-05-20 11:18:06 -0700617 Mutex::Autolock _l(mLock);
618 if (mAudioPolicyManager == NULL) {
619 return NO_INIT;
620 }
621
622 return mAudioPolicyManager->getAudioPort(port);
Eric Laurent203b1a12014-04-01 10:34:16 -0700623}
624
Eric Laurent6a94d692014-05-20 11:18:06 -0700625status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
626 audio_patch_handle_t *handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700627{
Eric Laurent6a94d692014-05-20 11:18:06 -0700628 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700629 if(!modifyAudioRoutingAllowed()) {
630 return PERMISSION_DENIED;
631 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700632 if (mAudioPolicyManager == NULL) {
633 return NO_INIT;
634 }
635 return mAudioPolicyManager->createAudioPatch(patch, handle,
636 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700637}
638
Eric Laurent6a94d692014-05-20 11:18:06 -0700639status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700640{
Eric Laurent6a94d692014-05-20 11:18:06 -0700641 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700642 if(!modifyAudioRoutingAllowed()) {
643 return PERMISSION_DENIED;
644 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700645 if (mAudioPolicyManager == NULL) {
646 return NO_INIT;
647 }
648
649 return mAudioPolicyManager->releaseAudioPatch(handle,
650 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700651}
652
653status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
Eric Laurent6a94d692014-05-20 11:18:06 -0700654 struct audio_patch *patches,
655 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700656{
Eric Laurent6a94d692014-05-20 11:18:06 -0700657 Mutex::Autolock _l(mLock);
658 if (mAudioPolicyManager == NULL) {
659 return NO_INIT;
660 }
661
662 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700663}
664
Eric Laurent6a94d692014-05-20 11:18:06 -0700665status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent203b1a12014-04-01 10:34:16 -0700666{
Eric Laurent6a94d692014-05-20 11:18:06 -0700667 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700668 if(!modifyAudioRoutingAllowed()) {
669 return PERMISSION_DENIED;
670 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700671 if (mAudioPolicyManager == NULL) {
672 return NO_INIT;
673 }
674
675 return mAudioPolicyManager->setAudioPortConfig(config);
Eric Laurent203b1a12014-04-01 10:34:16 -0700676}
Eric Laurent2d388ec2014-03-07 13:25:54 -0800677
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700678status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
679 audio_io_handle_t *ioHandle,
680 audio_devices_t *device)
681{
682 if (mAudioPolicyManager == NULL) {
683 return NO_INIT;
684 }
685
686 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
687}
688
689status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
690{
691 if (mAudioPolicyManager == NULL) {
692 return NO_INIT;
693 }
694
695 return mAudioPolicyManager->releaseSoundTriggerSession(session);
696}
697
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -0700698status_t AudioPolicyService::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800699{
700 Mutex::Autolock _l(mLock);
701 if(!modifyAudioRoutingAllowed()) {
702 return PERMISSION_DENIED;
703 }
704 if (mAudioPolicyManager == NULL) {
705 return NO_INIT;
706 }
707 if (registration) {
708 return mAudioPolicyManager->registerPolicyMixes(mixes);
709 } else {
710 return mAudioPolicyManager->unregisterPolicyMixes(mixes);
711 }
712}
713
Eric Laurent554a2772015-04-10 11:29:24 -0700714status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
715 const audio_attributes_t *attributes,
716 audio_io_handle_t *handle)
717{
718 Mutex::Autolock _l(mLock);
719 if (mAudioPolicyManager == NULL) {
720 return NO_INIT;
721 }
722
Eric Laurentd60560a2015-04-10 11:31:20 -0700723 return mAudioPolicyManager->startAudioSource(source, attributes, handle,
724 IPCThreadState::self()->getCallingUid());
Eric Laurent554a2772015-04-10 11:29:24 -0700725}
726
727status_t AudioPolicyService::stopAudioSource(audio_io_handle_t handle)
728{
729 Mutex::Autolock _l(mLock);
730 if (mAudioPolicyManager == NULL) {
731 return NO_INIT;
732 }
733
734 return mAudioPolicyManager->stopAudioSource(handle);
735}
736
Andy Hung2ddee192015-12-18 17:34:44 -0800737status_t AudioPolicyService::setMasterMono(bool mono)
738{
739 if (mAudioPolicyManager == NULL) {
740 return NO_INIT;
741 }
742 if (!settingsAllowed()) {
743 return PERMISSION_DENIED;
744 }
745 Mutex::Autolock _l(mLock);
746 return mAudioPolicyManager->setMasterMono(mono);
747}
748
749status_t AudioPolicyService::getMasterMono(bool *mono)
750{
751 if (mAudioPolicyManager == NULL) {
752 return NO_INIT;
753 }
754 Mutex::Autolock _l(mLock);
755 return mAudioPolicyManager->getMasterMono(mono);
756}
757
Eric Laurent2d388ec2014-03-07 13:25:54 -0800758}; // namespace android