blob: 0b3397f4cac1fa8b04f0c79160bc27c407ab2308 [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 }
40 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
41 return BAD_VALUE;
42 }
43 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
44 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
45 return BAD_VALUE;
46 }
47
48 ALOGV("setDeviceConnectionState()");
49 Mutex::Autolock _l(mLock);
Paul McLeane743a472015-01-28 11:07:31 -080050 return mAudioPolicyManager->setDeviceConnectionState(device, state,
51 device_address, device_name);
Eric Laurent2d388ec2014-03-07 13:25:54 -080052}
53
54audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
55 audio_devices_t device,
56 const char *device_address)
57{
Eric Laurentdce54a12014-03-10 12:19:46 -070058 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -080059 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
60 }
Eric Laurentdce54a12014-03-10 12:19:46 -070061 return mAudioPolicyManager->getDeviceConnectionState(device,
Eric Laurent2d388ec2014-03-07 13:25:54 -080062 device_address);
63}
64
Pavlin Radoslavovf862bc62016-12-26 18:57:22 -080065status_t AudioPolicyService::handleDeviceConfigChange(audio_devices_t device,
66 const char *device_address,
67 const char *device_name)
68{
69 if (mAudioPolicyManager == NULL) {
70 return NO_INIT;
71 }
72 if (!settingsAllowed()) {
73 return PERMISSION_DENIED;
74 }
75 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
76 return BAD_VALUE;
77 }
78
79 ALOGV("handleDeviceConfigChange()");
80 Mutex::Autolock _l(mLock);
81 return mAudioPolicyManager->handleDeviceConfigChange(device, device_address,
82 device_name);
83}
84
Eric Laurent2d388ec2014-03-07 13:25:54 -080085status_t AudioPolicyService::setPhoneState(audio_mode_t state)
86{
Eric Laurentdce54a12014-03-10 12:19:46 -070087 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -080088 return NO_INIT;
89 }
90 if (!settingsAllowed()) {
91 return PERMISSION_DENIED;
92 }
93 if (uint32_t(state) >= AUDIO_MODE_CNT) {
94 return BAD_VALUE;
95 }
96
97 ALOGV("setPhoneState()");
98
Eric Laurentbeb07fe2015-09-16 15:49:30 -070099 // acquire lock before calling setMode() so that setMode() + setPhoneState() are an atomic
100 // operation from policy manager standpoint (no other operation (e.g track start or stop)
101 // can be interleaved).
102 Mutex::Autolock _l(mLock);
103
Eric Laurent2d388ec2014-03-07 13:25:54 -0800104 // TODO: check if it is more appropriate to do it in platform specific policy manager
105 AudioSystem::setMode(state);
106
Eric Laurentdce54a12014-03-10 12:19:46 -0700107 mAudioPolicyManager->setPhoneState(state);
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700108 mPhoneState = state;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800109 return NO_ERROR;
110}
111
Eric Laurentbb6c9a02014-09-25 14:11:47 -0700112audio_mode_t AudioPolicyService::getPhoneState()
113{
114 Mutex::Autolock _l(mLock);
115 return mPhoneState;
116}
117
Eric Laurent2d388ec2014-03-07 13:25:54 -0800118status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
119 audio_policy_forced_cfg_t config)
120{
Eric Laurentdce54a12014-03-10 12:19:46 -0700121 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800122 return NO_INIT;
123 }
124 if (!settingsAllowed()) {
125 return PERMISSION_DENIED;
126 }
127 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
128 return BAD_VALUE;
129 }
130 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
131 return BAD_VALUE;
132 }
133 ALOGV("setForceUse()");
134 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700135 mAudioPolicyManager->setForceUse(usage, config);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800136 return NO_ERROR;
137}
138
139audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
140{
Eric Laurentdce54a12014-03-10 12:19:46 -0700141 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800142 return AUDIO_POLICY_FORCE_NONE;
143 }
144 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
145 return AUDIO_POLICY_FORCE_NONE;
146 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700147 return mAudioPolicyManager->getForceUse(usage);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800148}
149
150audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
151 uint32_t samplingRate,
152 audio_format_t format,
153 audio_channel_mask_t channelMask,
154 audio_output_flags_t flags,
155 const audio_offload_info_t *offloadInfo)
156{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800157 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700158 return AUDIO_IO_HANDLE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700159 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700160 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700161 return AUDIO_IO_HANDLE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800162 }
163 ALOGV("getOutput()");
164 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700165 return mAudioPolicyManager->getOutput(stream, samplingRate,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800166 format, channelMask, flags, offloadInfo);
167}
168
Eric Laurente83b55d2014-11-14 10:06:21 -0800169status_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr,
170 audio_io_handle_t *output,
171 audio_session_t session,
172 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700173 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800174 uint32_t samplingRate,
175 audio_format_t format,
176 audio_channel_mask_t channelMask,
177 audio_output_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -0600178 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800179 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700180{
181 if (mAudioPolicyManager == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800182 return NO_INIT;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700183 }
184 ALOGV("getOutput()");
185 Mutex::Autolock _l(mLock);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700186
Marco Nelissendcb346b2015-09-09 10:47:29 -0700187 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
188 if (!isTrustedCallingUid(callingUid) || uid == (uid_t)-1) {
189 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
190 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
191 uid = callingUid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700192 }
193 return mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid, samplingRate,
Paul McLean466dc8e2015-04-17 13:15:36 -0600194 format, channelMask, flags, selectedDeviceId, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700195}
196
Eric Laurent2d388ec2014-03-07 13:25:54 -0800197status_t AudioPolicyService::startOutput(audio_io_handle_t output,
198 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800199 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800200{
Eric Laurentdea15412014-10-28 15:46:45 -0700201 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
202 return BAD_VALUE;
203 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700204 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800205 return NO_INIT;
206 }
207 ALOGV("startOutput()");
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700208 sp<AudioPolicyEffects>audioPolicyEffects;
209 {
210 Mutex::Autolock _l(mLock);
211 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800212 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700213 if (audioPolicyEffects != 0) {
214 // create audio processors according to stream
215 status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session);
216 if (status != NO_ERROR && status != ALREADY_EXISTS) {
217 ALOGW("Failed to add effects on session %d", session);
218 }
219 }
220 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700221 return mAudioPolicyManager->startOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800222}
223
224status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
225 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800226 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800227{
Eric Laurentdea15412014-10-28 15:46:45 -0700228 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
229 return BAD_VALUE;
230 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700231 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800232 return NO_INIT;
233 }
234 ALOGV("stopOutput()");
235 mOutputCommandThread->stopOutputCommand(output, stream, session);
236 return NO_ERROR;
237}
238
239status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
240 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800241 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800242{
243 ALOGV("doStopOutput from tid %d", gettid());
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700244 sp<AudioPolicyEffects>audioPolicyEffects;
245 {
246 Mutex::Autolock _l(mLock);
247 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800248 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700249 if (audioPolicyEffects != 0) {
250 // release audio processors from the stream
251 status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
252 if (status != NO_ERROR && status != ALREADY_EXISTS) {
253 ALOGW("Failed to release effects on session %d", session);
254 }
255 }
256 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700257 return mAudioPolicyManager->stopOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800258}
259
Eric Laurente83b55d2014-11-14 10:06:21 -0800260void AudioPolicyService::releaseOutput(audio_io_handle_t output,
261 audio_stream_type_t stream,
262 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800263{
Eric Laurentdce54a12014-03-10 12:19:46 -0700264 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800265 return;
266 }
267 ALOGV("releaseOutput()");
Eric Laurente83b55d2014-11-14 10:06:21 -0800268 mOutputCommandThread->releaseOutputCommand(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800269}
270
Eric Laurente83b55d2014-11-14 10:06:21 -0800271void AudioPolicyService::doReleaseOutput(audio_io_handle_t output,
272 audio_stream_type_t stream,
273 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800274{
275 ALOGV("doReleaseOutput from tid %d", gettid());
276 Mutex::Autolock _l(mLock);
Eric Laurente83b55d2014-11-14 10:06:21 -0800277 mAudioPolicyManager->releaseOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800278}
279
Eric Laurentcaf7f482014-11-25 17:50:47 -0800280status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr,
281 audio_io_handle_t *input,
282 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700283 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700284 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800285 uint32_t samplingRate,
286 audio_format_t format,
287 audio_channel_mask_t channelMask,
Paul McLean466dc8e2015-04-17 13:15:36 -0600288 audio_input_flags_t flags,
289 audio_port_handle_t selectedDeviceId)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800290{
Eric Laurentdce54a12014-03-10 12:19:46 -0700291 if (mAudioPolicyManager == NULL) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800292 return NO_INIT;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800293 }
294 // already checked by client, but double-check in case the client wrapper is bypassed
Eric Laurentcaf7f482014-11-25 17:50:47 -0800295 if (attr->source >= AUDIO_SOURCE_CNT && attr->source != AUDIO_SOURCE_HOTWORD &&
296 attr->source != AUDIO_SOURCE_FM_TUNER) {
297 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800298 }
299
Eric Laurentab300c82015-04-13 13:47:33 -0700300 if ((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800301 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800302 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700303 sp<AudioPolicyEffects>audioPolicyEffects;
Eric Laurentcaf7f482014-11-25 17:50:47 -0800304 status_t status;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800305 AudioPolicyInterface::input_type_t inputType;
Eric Laurentb2379ba2016-05-23 17:42:12 -0700306
307 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -0700308 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurentb2379ba2016-05-23 17:42:12 -0700309 if (!isTrustedCallingUid(callingUid)) {
Eric Laurent9f39f8d2016-05-25 12:34:48 -0700310 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -0700311 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
312 uid = callingUid;
Eric Laurentb2379ba2016-05-23 17:42:12 -0700313 updatePid = true;
314 }
315
316 if (updatePid) {
317 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Eric Laurent9f39f8d2016-05-25 12:34:48 -0700318 ALOGW_IF(pid != (pid_t)-1 && pid != callingPid,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700319 "%s uid %d pid %d tried to pass itself off as pid %d",
320 __func__, callingUid, callingPid, pid);
321 pid = callingPid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700322 }
323
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700324 {
325 Mutex::Autolock _l(mLock);
326 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700327 status = mAudioPolicyManager->getInputForAttr(attr, input, session, uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800328 samplingRate, format, channelMask,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700329 flags, selectedDeviceId,
330 &inputType);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700331 audioPolicyEffects = mAudioPolicyEffects;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800332
333 if (status == NO_ERROR) {
334 // enforce permission (if any) required for each type of input
335 switch (inputType) {
336 case AudioPolicyInterface::API_INPUT_LEGACY:
337 break;
Eric Laurent82db2692015-08-07 13:59:42 -0700338 case AudioPolicyInterface::API_INPUT_TELEPHONY_RX:
339 // FIXME: use the same permission as for remote submix for now.
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800340 case AudioPolicyInterface::API_INPUT_MIX_CAPTURE:
Eric Laurentb2379ba2016-05-23 17:42:12 -0700341 if (!captureAudioOutputAllowed(pid, uid)) {
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800342 ALOGE("getInputForAttr() permission denied: capture not allowed");
343 status = PERMISSION_DENIED;
344 }
345 break;
346 case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE:
347 if (!modifyAudioRoutingAllowed()) {
348 ALOGE("getInputForAttr() permission denied: modify audio routing not allowed");
349 status = PERMISSION_DENIED;
350 }
351 break;
352 case AudioPolicyInterface::API_INPUT_INVALID:
353 default:
354 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d",
355 (int)inputType);
356 }
357 }
358
359 if (status != NO_ERROR) {
360 if (status == PERMISSION_DENIED) {
361 mAudioPolicyManager->releaseInput(*input, session);
362 }
363 return status;
364 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700365 }
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800366
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700367 if (audioPolicyEffects != 0) {
368 // create audio pre processors according to input source
Eric Laurentcaf7f482014-11-25 17:50:47 -0800369 status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700370 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800371 ALOGW("Failed to add effects on input %d", *input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700372 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800373 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800374 return NO_ERROR;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800375}
376
Eric Laurent4dc68062014-07-28 17:26:49 -0700377status_t AudioPolicyService::startInput(audio_io_handle_t input,
378 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800379{
Eric Laurentdce54a12014-03-10 12:19:46 -0700380 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800381 return NO_INIT;
382 }
383 Mutex::Autolock _l(mLock);
384
Eric Laurent232f26f2016-02-17 18:06:27 -0800385 return mAudioPolicyManager->startInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800386}
387
Eric Laurent4dc68062014-07-28 17:26:49 -0700388status_t AudioPolicyService::stopInput(audio_io_handle_t input,
389 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800390{
Eric Laurentdce54a12014-03-10 12:19:46 -0700391 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800392 return NO_INIT;
393 }
394 Mutex::Autolock _l(mLock);
395
Eric Laurent4dc68062014-07-28 17:26:49 -0700396 return mAudioPolicyManager->stopInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800397}
398
Eric Laurent4dc68062014-07-28 17:26:49 -0700399void AudioPolicyService::releaseInput(audio_io_handle_t input,
400 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800401{
Eric Laurentdce54a12014-03-10 12:19:46 -0700402 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800403 return;
404 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700405 sp<AudioPolicyEffects>audioPolicyEffects;
406 {
407 Mutex::Autolock _l(mLock);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700408 audioPolicyEffects = mAudioPolicyEffects;
409 }
410 if (audioPolicyEffects != 0) {
411 // release audio processors from the input
Eric Laurent232f26f2016-02-17 18:06:27 -0800412 status_t status = audioPolicyEffects->releaseInputEffects(input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700413 if(status != NO_ERROR) {
414 ALOGW("Failed to release effects on input %d", input);
415 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800416 }
Eric Laurentb378b732016-12-01 15:28:29 -0800417 {
418 Mutex::Autolock _l(mLock);
419 mAudioPolicyManager->releaseInput(input, session);
420 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800421}
422
423status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
424 int indexMin,
425 int indexMax)
426{
Eric Laurentdce54a12014-03-10 12:19:46 -0700427 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800428 return NO_INIT;
429 }
430 if (!settingsAllowed()) {
431 return PERMISSION_DENIED;
432 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800433 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800434 return BAD_VALUE;
435 }
436 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700437 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800438 return NO_ERROR;
439}
440
441status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
442 int index,
443 audio_devices_t device)
444{
Eric Laurentdce54a12014-03-10 12:19:46 -0700445 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800446 return NO_INIT;
447 }
448 if (!settingsAllowed()) {
449 return PERMISSION_DENIED;
450 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800451 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800452 return BAD_VALUE;
453 }
454 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700455 return mAudioPolicyManager->setStreamVolumeIndex(stream,
456 index,
457 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800458}
459
460status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
461 int *index,
462 audio_devices_t device)
463{
Eric Laurentdce54a12014-03-10 12:19:46 -0700464 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800465 return NO_INIT;
466 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800467 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800468 return BAD_VALUE;
469 }
470 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700471 return mAudioPolicyManager->getStreamVolumeIndex(stream,
472 index,
473 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800474}
475
476uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
477{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800478 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700479 return 0;
Eric Laurentdea15412014-10-28 15:46:45 -0700480 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700481 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800482 return 0;
483 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700484 return mAudioPolicyManager->getStrategyForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800485}
486
487//audio policy: use audio_device_t appropriately
488
489audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
490{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800491 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700492 return AUDIO_DEVICE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700493 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700494 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700495 return AUDIO_DEVICE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800496 }
Haynes Mathew Georgedfb9f3b2015-10-26 18:22:13 -0700497 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700498 return mAudioPolicyManager->getDevicesForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800499}
500
501audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
502{
503 // FIXME change return type to status_t, and return NO_INIT here
Eric Laurentdce54a12014-03-10 12:19:46 -0700504 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800505 return 0;
506 }
507 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700508 return mAudioPolicyManager->getOutputForEffect(desc);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800509}
510
511status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
512 audio_io_handle_t io,
513 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800514 audio_session_t session,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800515 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->registerEffect(desc, io, strategy, session, id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800522}
523
524status_t AudioPolicyService::unregisterEffect(int id)
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->unregisterEffect(id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800531}
532
533status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
534{
Eric Laurentdce54a12014-03-10 12:19:46 -0700535 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800536 return NO_INIT;
537 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700538 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700539 return mAudioPolicyManager->setEffectEnabled(id, enabled);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800540}
541
542bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
543{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800544 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700545 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700546 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700547 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700548 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800549 }
550 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700551 return mAudioPolicyManager->isStreamActive(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800552}
553
554bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
555{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800556 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700557 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700558 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700559 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700560 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800561 }
562 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700563 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800564}
565
566bool AudioPolicyService::isSourceActive(audio_source_t source) const
567{
Eric Laurentdce54a12014-03-10 12:19:46 -0700568 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800569 return false;
570 }
571 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700572 return mAudioPolicyManager->isSourceActive(source);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800573}
574
Glenn Kastend848eb42016-03-08 13:42:11 -0800575status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800576 effect_descriptor_t *descriptors,
577 uint32_t *count)
578{
Eric Laurentdce54a12014-03-10 12:19:46 -0700579 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800580 *count = 0;
581 return NO_INIT;
582 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700583 sp<AudioPolicyEffects>audioPolicyEffects;
584 {
585 Mutex::Autolock _l(mLock);
586 audioPolicyEffects = mAudioPolicyEffects;
587 }
588 if (audioPolicyEffects == 0) {
589 *count = 0;
590 return NO_INIT;
591 }
Eric Laurent232f26f2016-02-17 18:06:27 -0800592 return audioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800593}
594
595bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
596{
Eric Laurentdce54a12014-03-10 12:19:46 -0700597 if (mAudioPolicyManager == NULL) {
598 ALOGV("mAudioPolicyManager == NULL");
Eric Laurent2d388ec2014-03-07 13:25:54 -0800599 return false;
600 }
Andy Hung2ddee192015-12-18 17:34:44 -0800601 Mutex::Autolock _l(mLock);
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700602 Mutex::Autolock _le(mEffectsLock); // isOffloadSupported queries for
603 // non-offloadable effects
Eric Laurentdce54a12014-03-10 12:19:46 -0700604 return mAudioPolicyManager->isOffloadSupported(info);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800605}
606
Eric Laurent6a94d692014-05-20 11:18:06 -0700607status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
608 audio_port_type_t type,
Eric Laurent203b1a12014-04-01 10:34:16 -0700609 unsigned int *num_ports,
Eric Laurent6a94d692014-05-20 11:18:06 -0700610 struct audio_port *ports,
611 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700612{
Eric Laurent6a94d692014-05-20 11:18:06 -0700613 Mutex::Autolock _l(mLock);
614 if (mAudioPolicyManager == NULL) {
615 return NO_INIT;
616 }
617
618 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700619}
620
Eric Laurent6a94d692014-05-20 11:18:06 -0700621status_t AudioPolicyService::getAudioPort(struct audio_port *port)
Eric Laurent203b1a12014-04-01 10:34:16 -0700622{
Eric Laurent6a94d692014-05-20 11:18:06 -0700623 Mutex::Autolock _l(mLock);
624 if (mAudioPolicyManager == NULL) {
625 return NO_INIT;
626 }
627
628 return mAudioPolicyManager->getAudioPort(port);
Eric Laurent203b1a12014-04-01 10:34:16 -0700629}
630
Eric Laurent6a94d692014-05-20 11:18:06 -0700631status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
632 audio_patch_handle_t *handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700633{
Eric Laurent6a94d692014-05-20 11:18:06 -0700634 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700635 if(!modifyAudioRoutingAllowed()) {
636 return PERMISSION_DENIED;
637 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700638 if (mAudioPolicyManager == NULL) {
639 return NO_INIT;
640 }
641 return mAudioPolicyManager->createAudioPatch(patch, handle,
642 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700643}
644
Eric Laurent6a94d692014-05-20 11:18:06 -0700645status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700646{
Eric Laurent6a94d692014-05-20 11:18:06 -0700647 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700648 if(!modifyAudioRoutingAllowed()) {
649 return PERMISSION_DENIED;
650 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700651 if (mAudioPolicyManager == NULL) {
652 return NO_INIT;
653 }
654
655 return mAudioPolicyManager->releaseAudioPatch(handle,
656 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700657}
658
659status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
Eric Laurent6a94d692014-05-20 11:18:06 -0700660 struct audio_patch *patches,
661 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700662{
Eric Laurent6a94d692014-05-20 11:18:06 -0700663 Mutex::Autolock _l(mLock);
664 if (mAudioPolicyManager == NULL) {
665 return NO_INIT;
666 }
667
668 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700669}
670
Eric Laurent6a94d692014-05-20 11:18:06 -0700671status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent203b1a12014-04-01 10:34:16 -0700672{
Eric Laurent6a94d692014-05-20 11:18:06 -0700673 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700674 if(!modifyAudioRoutingAllowed()) {
675 return PERMISSION_DENIED;
676 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700677 if (mAudioPolicyManager == NULL) {
678 return NO_INIT;
679 }
680
681 return mAudioPolicyManager->setAudioPortConfig(config);
Eric Laurent203b1a12014-04-01 10:34:16 -0700682}
Eric Laurent2d388ec2014-03-07 13:25:54 -0800683
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700684status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
685 audio_io_handle_t *ioHandle,
686 audio_devices_t *device)
687{
688 if (mAudioPolicyManager == NULL) {
689 return NO_INIT;
690 }
691
692 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
693}
694
695status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
696{
697 if (mAudioPolicyManager == NULL) {
698 return NO_INIT;
699 }
700
701 return mAudioPolicyManager->releaseSoundTriggerSession(session);
702}
703
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -0700704status_t AudioPolicyService::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800705{
706 Mutex::Autolock _l(mLock);
707 if(!modifyAudioRoutingAllowed()) {
708 return PERMISSION_DENIED;
709 }
710 if (mAudioPolicyManager == NULL) {
711 return NO_INIT;
712 }
713 if (registration) {
714 return mAudioPolicyManager->registerPolicyMixes(mixes);
715 } else {
716 return mAudioPolicyManager->unregisterPolicyMixes(mixes);
717 }
718}
719
Eric Laurent554a2772015-04-10 11:29:24 -0700720status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
721 const audio_attributes_t *attributes,
722 audio_io_handle_t *handle)
723{
724 Mutex::Autolock _l(mLock);
725 if (mAudioPolicyManager == NULL) {
726 return NO_INIT;
727 }
728
Eric Laurentd60560a2015-04-10 11:31:20 -0700729 return mAudioPolicyManager->startAudioSource(source, attributes, handle,
730 IPCThreadState::self()->getCallingUid());
Eric Laurent554a2772015-04-10 11:29:24 -0700731}
732
733status_t AudioPolicyService::stopAudioSource(audio_io_handle_t handle)
734{
735 Mutex::Autolock _l(mLock);
736 if (mAudioPolicyManager == NULL) {
737 return NO_INIT;
738 }
739
740 return mAudioPolicyManager->stopAudioSource(handle);
741}
742
Andy Hung2ddee192015-12-18 17:34:44 -0800743status_t AudioPolicyService::setMasterMono(bool mono)
744{
745 if (mAudioPolicyManager == NULL) {
746 return NO_INIT;
747 }
748 if (!settingsAllowed()) {
749 return PERMISSION_DENIED;
750 }
751 Mutex::Autolock _l(mLock);
752 return mAudioPolicyManager->setMasterMono(mono);
753}
754
755status_t AudioPolicyService::getMasterMono(bool *mono)
756{
757 if (mAudioPolicyManager == NULL) {
758 return NO_INIT;
759 }
760 Mutex::Autolock _l(mLock);
761 return mAudioPolicyManager->getMasterMono(mono);
762}
763
Eric Laurent2d388ec2014-03-07 13:25:54 -0800764}; // namespace android