blob: c5323d11c05502d779a8abf6a012b4ec59ce7985 [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);
402 mAudioPolicyManager->releaseInput(input, session);
403 audioPolicyEffects = mAudioPolicyEffects;
404 }
405 if (audioPolicyEffects != 0) {
406 // release audio processors from the input
Eric Laurent232f26f2016-02-17 18:06:27 -0800407 status_t status = audioPolicyEffects->releaseInputEffects(input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700408 if(status != NO_ERROR) {
409 ALOGW("Failed to release effects on input %d", input);
410 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800411 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800412}
413
414status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
415 int indexMin,
416 int indexMax)
417{
Eric Laurentdce54a12014-03-10 12:19:46 -0700418 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800419 return NO_INIT;
420 }
421 if (!settingsAllowed()) {
422 return PERMISSION_DENIED;
423 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800424 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800425 return BAD_VALUE;
426 }
427 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700428 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800429 return NO_ERROR;
430}
431
432status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
433 int index,
434 audio_devices_t device)
435{
Eric Laurentdce54a12014-03-10 12:19:46 -0700436 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800437 return NO_INIT;
438 }
439 if (!settingsAllowed()) {
440 return PERMISSION_DENIED;
441 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800442 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800443 return BAD_VALUE;
444 }
445 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700446 return mAudioPolicyManager->setStreamVolumeIndex(stream,
447 index,
448 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800449}
450
451status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
452 int *index,
453 audio_devices_t device)
454{
Eric Laurentdce54a12014-03-10 12:19:46 -0700455 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800456 return NO_INIT;
457 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800458 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800459 return BAD_VALUE;
460 }
461 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700462 return mAudioPolicyManager->getStreamVolumeIndex(stream,
463 index,
464 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800465}
466
467uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
468{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800469 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700470 return 0;
Eric Laurentdea15412014-10-28 15:46:45 -0700471 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700472 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800473 return 0;
474 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700475 return mAudioPolicyManager->getStrategyForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800476}
477
478//audio policy: use audio_device_t appropriately
479
480audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
481{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800482 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700483 return AUDIO_DEVICE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700484 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700485 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700486 return AUDIO_DEVICE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800487 }
Haynes Mathew Georgedfb9f3b2015-10-26 18:22:13 -0700488 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700489 return mAudioPolicyManager->getDevicesForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800490}
491
492audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
493{
494 // FIXME change return type to status_t, and return NO_INIT here
Eric Laurentdce54a12014-03-10 12:19:46 -0700495 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800496 return 0;
497 }
498 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700499 return mAudioPolicyManager->getOutputForEffect(desc);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800500}
501
502status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
503 audio_io_handle_t io,
504 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800505 audio_session_t session,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800506 int id)
507{
Eric Laurentdce54a12014-03-10 12:19:46 -0700508 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800509 return NO_INIT;
510 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700511 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700512 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800513}
514
515status_t AudioPolicyService::unregisterEffect(int id)
516{
Eric Laurentdce54a12014-03-10 12:19:46 -0700517 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800518 return NO_INIT;
519 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700520 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700521 return mAudioPolicyManager->unregisterEffect(id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800522}
523
524status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
525{
Eric Laurentdce54a12014-03-10 12:19:46 -0700526 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800527 return NO_INIT;
528 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700529 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700530 return mAudioPolicyManager->setEffectEnabled(id, enabled);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800531}
532
533bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
534{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800535 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700536 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700537 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700538 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700539 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800540 }
541 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700542 return mAudioPolicyManager->isStreamActive(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800543}
544
545bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
546{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800547 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700548 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700549 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700550 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700551 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800552 }
553 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700554 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800555}
556
557bool AudioPolicyService::isSourceActive(audio_source_t source) const
558{
Eric Laurentdce54a12014-03-10 12:19:46 -0700559 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800560 return false;
561 }
562 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700563 return mAudioPolicyManager->isSourceActive(source);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800564}
565
Glenn Kastend848eb42016-03-08 13:42:11 -0800566status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800567 effect_descriptor_t *descriptors,
568 uint32_t *count)
569{
Eric Laurentdce54a12014-03-10 12:19:46 -0700570 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800571 *count = 0;
572 return NO_INIT;
573 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700574 sp<AudioPolicyEffects>audioPolicyEffects;
575 {
576 Mutex::Autolock _l(mLock);
577 audioPolicyEffects = mAudioPolicyEffects;
578 }
579 if (audioPolicyEffects == 0) {
580 *count = 0;
581 return NO_INIT;
582 }
Eric Laurent232f26f2016-02-17 18:06:27 -0800583 return audioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800584}
585
586bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
587{
Eric Laurentdce54a12014-03-10 12:19:46 -0700588 if (mAudioPolicyManager == NULL) {
589 ALOGV("mAudioPolicyManager == NULL");
Eric Laurent2d388ec2014-03-07 13:25:54 -0800590 return false;
591 }
Andy Hung2ddee192015-12-18 17:34:44 -0800592 Mutex::Autolock _l(mLock);
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700593 Mutex::Autolock _le(mEffectsLock); // isOffloadSupported queries for
594 // non-offloadable effects
Eric Laurentdce54a12014-03-10 12:19:46 -0700595 return mAudioPolicyManager->isOffloadSupported(info);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800596}
597
Eric Laurent6a94d692014-05-20 11:18:06 -0700598status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
599 audio_port_type_t type,
Eric Laurent203b1a12014-04-01 10:34:16 -0700600 unsigned int *num_ports,
Eric Laurent6a94d692014-05-20 11:18:06 -0700601 struct audio_port *ports,
602 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700603{
Eric Laurent6a94d692014-05-20 11:18:06 -0700604 Mutex::Autolock _l(mLock);
605 if (mAudioPolicyManager == NULL) {
606 return NO_INIT;
607 }
608
609 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700610}
611
Eric Laurent6a94d692014-05-20 11:18:06 -0700612status_t AudioPolicyService::getAudioPort(struct audio_port *port)
Eric Laurent203b1a12014-04-01 10:34:16 -0700613{
Eric Laurent6a94d692014-05-20 11:18:06 -0700614 Mutex::Autolock _l(mLock);
615 if (mAudioPolicyManager == NULL) {
616 return NO_INIT;
617 }
618
619 return mAudioPolicyManager->getAudioPort(port);
Eric Laurent203b1a12014-04-01 10:34:16 -0700620}
621
Eric Laurent6a94d692014-05-20 11:18:06 -0700622status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
623 audio_patch_handle_t *handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700624{
Eric Laurent6a94d692014-05-20 11:18:06 -0700625 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700626 if(!modifyAudioRoutingAllowed()) {
627 return PERMISSION_DENIED;
628 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700629 if (mAudioPolicyManager == NULL) {
630 return NO_INIT;
631 }
632 return mAudioPolicyManager->createAudioPatch(patch, handle,
633 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700634}
635
Eric Laurent6a94d692014-05-20 11:18:06 -0700636status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700637{
Eric Laurent6a94d692014-05-20 11:18:06 -0700638 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700639 if(!modifyAudioRoutingAllowed()) {
640 return PERMISSION_DENIED;
641 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700642 if (mAudioPolicyManager == NULL) {
643 return NO_INIT;
644 }
645
646 return mAudioPolicyManager->releaseAudioPatch(handle,
647 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700648}
649
650status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
Eric Laurent6a94d692014-05-20 11:18:06 -0700651 struct audio_patch *patches,
652 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700653{
Eric Laurent6a94d692014-05-20 11:18:06 -0700654 Mutex::Autolock _l(mLock);
655 if (mAudioPolicyManager == NULL) {
656 return NO_INIT;
657 }
658
659 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700660}
661
Eric Laurent6a94d692014-05-20 11:18:06 -0700662status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent203b1a12014-04-01 10:34:16 -0700663{
Eric Laurent6a94d692014-05-20 11:18:06 -0700664 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700665 if(!modifyAudioRoutingAllowed()) {
666 return PERMISSION_DENIED;
667 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700668 if (mAudioPolicyManager == NULL) {
669 return NO_INIT;
670 }
671
672 return mAudioPolicyManager->setAudioPortConfig(config);
Eric Laurent203b1a12014-04-01 10:34:16 -0700673}
Eric Laurent2d388ec2014-03-07 13:25:54 -0800674
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700675status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
676 audio_io_handle_t *ioHandle,
677 audio_devices_t *device)
678{
679 if (mAudioPolicyManager == NULL) {
680 return NO_INIT;
681 }
682
683 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
684}
685
686status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
687{
688 if (mAudioPolicyManager == NULL) {
689 return NO_INIT;
690 }
691
692 return mAudioPolicyManager->releaseSoundTriggerSession(session);
693}
694
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -0700695status_t AudioPolicyService::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800696{
697 Mutex::Autolock _l(mLock);
698 if(!modifyAudioRoutingAllowed()) {
699 return PERMISSION_DENIED;
700 }
701 if (mAudioPolicyManager == NULL) {
702 return NO_INIT;
703 }
704 if (registration) {
705 return mAudioPolicyManager->registerPolicyMixes(mixes);
706 } else {
707 return mAudioPolicyManager->unregisterPolicyMixes(mixes);
708 }
709}
710
Eric Laurent554a2772015-04-10 11:29:24 -0700711status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
712 const audio_attributes_t *attributes,
713 audio_io_handle_t *handle)
714{
715 Mutex::Autolock _l(mLock);
716 if (mAudioPolicyManager == NULL) {
717 return NO_INIT;
718 }
719
Eric Laurentd60560a2015-04-10 11:31:20 -0700720 return mAudioPolicyManager->startAudioSource(source, attributes, handle,
721 IPCThreadState::self()->getCallingUid());
Eric Laurent554a2772015-04-10 11:29:24 -0700722}
723
724status_t AudioPolicyService::stopAudioSource(audio_io_handle_t handle)
725{
726 Mutex::Autolock _l(mLock);
727 if (mAudioPolicyManager == NULL) {
728 return NO_INIT;
729 }
730
731 return mAudioPolicyManager->stopAudioSource(handle);
732}
733
Andy Hung2ddee192015-12-18 17:34:44 -0800734status_t AudioPolicyService::setMasterMono(bool mono)
735{
736 if (mAudioPolicyManager == NULL) {
737 return NO_INIT;
738 }
739 if (!settingsAllowed()) {
740 return PERMISSION_DENIED;
741 }
742 Mutex::Autolock _l(mLock);
743 return mAudioPolicyManager->setMasterMono(mono);
744}
745
746status_t AudioPolicyService::getMasterMono(bool *mono)
747{
748 if (mAudioPolicyManager == NULL) {
749 return NO_INIT;
750 }
751 Mutex::Autolock _l(mLock);
752 return mAudioPolicyManager->getMasterMono(mono);
753}
754
Eric Laurent2d388ec2014-03-07 13:25:54 -0800755}; // namespace android