blob: b4470c0ae8f77aa99f8824b22321072e6410fb33 [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);
408 mAudioPolicyManager->releaseInput(input, session);
409 audioPolicyEffects = mAudioPolicyEffects;
410 }
411 if (audioPolicyEffects != 0) {
412 // release audio processors from the input
Eric Laurent232f26f2016-02-17 18:06:27 -0800413 status_t status = audioPolicyEffects->releaseInputEffects(input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700414 if(status != NO_ERROR) {
415 ALOGW("Failed to release effects on input %d", input);
416 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800417 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800418}
419
420status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
421 int indexMin,
422 int indexMax)
423{
Eric Laurentdce54a12014-03-10 12:19:46 -0700424 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800425 return NO_INIT;
426 }
427 if (!settingsAllowed()) {
428 return PERMISSION_DENIED;
429 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800430 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800431 return BAD_VALUE;
432 }
433 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700434 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800435 return NO_ERROR;
436}
437
438status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
439 int index,
440 audio_devices_t device)
441{
Eric Laurentdce54a12014-03-10 12:19:46 -0700442 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800443 return NO_INIT;
444 }
445 if (!settingsAllowed()) {
446 return PERMISSION_DENIED;
447 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800448 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800449 return BAD_VALUE;
450 }
451 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700452 return mAudioPolicyManager->setStreamVolumeIndex(stream,
453 index,
454 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800455}
456
457status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
458 int *index,
459 audio_devices_t device)
460{
Eric Laurentdce54a12014-03-10 12:19:46 -0700461 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800462 return NO_INIT;
463 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800464 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800465 return BAD_VALUE;
466 }
467 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700468 return mAudioPolicyManager->getStreamVolumeIndex(stream,
469 index,
470 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800471}
472
473uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
474{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800475 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700476 return 0;
Eric Laurentdea15412014-10-28 15:46:45 -0700477 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700478 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800479 return 0;
480 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700481 return mAudioPolicyManager->getStrategyForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800482}
483
484//audio policy: use audio_device_t appropriately
485
486audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
487{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800488 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700489 return AUDIO_DEVICE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700490 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700491 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700492 return AUDIO_DEVICE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800493 }
Haynes Mathew Georgedfb9f3b2015-10-26 18:22:13 -0700494 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700495 return mAudioPolicyManager->getDevicesForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800496}
497
498audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
499{
500 // FIXME change return type to status_t, and return NO_INIT here
Eric Laurentdce54a12014-03-10 12:19:46 -0700501 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800502 return 0;
503 }
504 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700505 return mAudioPolicyManager->getOutputForEffect(desc);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800506}
507
508status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
509 audio_io_handle_t io,
510 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800511 audio_session_t session,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800512 int id)
513{
Eric Laurentdce54a12014-03-10 12:19:46 -0700514 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800515 return NO_INIT;
516 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700517 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700518 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800519}
520
521status_t AudioPolicyService::unregisterEffect(int id)
522{
Eric Laurentdce54a12014-03-10 12:19:46 -0700523 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800524 return NO_INIT;
525 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700526 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700527 return mAudioPolicyManager->unregisterEffect(id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800528}
529
530status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
531{
Eric Laurentdce54a12014-03-10 12:19:46 -0700532 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800533 return NO_INIT;
534 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700535 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700536 return mAudioPolicyManager->setEffectEnabled(id, enabled);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800537}
538
539bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
540{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800541 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700542 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700543 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700544 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700545 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800546 }
547 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700548 return mAudioPolicyManager->isStreamActive(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800549}
550
551bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
552{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800553 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700554 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700555 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700556 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700557 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800558 }
559 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700560 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800561}
562
563bool AudioPolicyService::isSourceActive(audio_source_t source) const
564{
Eric Laurentdce54a12014-03-10 12:19:46 -0700565 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800566 return false;
567 }
568 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700569 return mAudioPolicyManager->isSourceActive(source);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800570}
571
Glenn Kastend848eb42016-03-08 13:42:11 -0800572status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800573 effect_descriptor_t *descriptors,
574 uint32_t *count)
575{
Eric Laurentdce54a12014-03-10 12:19:46 -0700576 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800577 *count = 0;
578 return NO_INIT;
579 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700580 sp<AudioPolicyEffects>audioPolicyEffects;
581 {
582 Mutex::Autolock _l(mLock);
583 audioPolicyEffects = mAudioPolicyEffects;
584 }
585 if (audioPolicyEffects == 0) {
586 *count = 0;
587 return NO_INIT;
588 }
Eric Laurent232f26f2016-02-17 18:06:27 -0800589 return audioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800590}
591
592bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
593{
Eric Laurentdce54a12014-03-10 12:19:46 -0700594 if (mAudioPolicyManager == NULL) {
595 ALOGV("mAudioPolicyManager == NULL");
Eric Laurent2d388ec2014-03-07 13:25:54 -0800596 return false;
597 }
Andy Hung2ddee192015-12-18 17:34:44 -0800598 Mutex::Autolock _l(mLock);
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700599 Mutex::Autolock _le(mEffectsLock); // isOffloadSupported queries for
600 // non-offloadable effects
Eric Laurentdce54a12014-03-10 12:19:46 -0700601 return mAudioPolicyManager->isOffloadSupported(info);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800602}
603
Eric Laurent6a94d692014-05-20 11:18:06 -0700604status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
605 audio_port_type_t type,
Eric Laurent203b1a12014-04-01 10:34:16 -0700606 unsigned int *num_ports,
Eric Laurent6a94d692014-05-20 11:18:06 -0700607 struct audio_port *ports,
608 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700609{
Eric Laurent6a94d692014-05-20 11:18:06 -0700610 Mutex::Autolock _l(mLock);
611 if (mAudioPolicyManager == NULL) {
612 return NO_INIT;
613 }
614
615 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700616}
617
Eric Laurent6a94d692014-05-20 11:18:06 -0700618status_t AudioPolicyService::getAudioPort(struct audio_port *port)
Eric Laurent203b1a12014-04-01 10:34:16 -0700619{
Eric Laurent6a94d692014-05-20 11:18:06 -0700620 Mutex::Autolock _l(mLock);
621 if (mAudioPolicyManager == NULL) {
622 return NO_INIT;
623 }
624
625 return mAudioPolicyManager->getAudioPort(port);
Eric Laurent203b1a12014-04-01 10:34:16 -0700626}
627
Eric Laurent6a94d692014-05-20 11:18:06 -0700628status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
629 audio_patch_handle_t *handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700630{
Eric Laurent6a94d692014-05-20 11:18:06 -0700631 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700632 if(!modifyAudioRoutingAllowed()) {
633 return PERMISSION_DENIED;
634 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700635 if (mAudioPolicyManager == NULL) {
636 return NO_INIT;
637 }
638 return mAudioPolicyManager->createAudioPatch(patch, handle,
639 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700640}
641
Eric Laurent6a94d692014-05-20 11:18:06 -0700642status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700643{
Eric Laurent6a94d692014-05-20 11:18:06 -0700644 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700645 if(!modifyAudioRoutingAllowed()) {
646 return PERMISSION_DENIED;
647 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700648 if (mAudioPolicyManager == NULL) {
649 return NO_INIT;
650 }
651
652 return mAudioPolicyManager->releaseAudioPatch(handle,
653 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700654}
655
656status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
Eric Laurent6a94d692014-05-20 11:18:06 -0700657 struct audio_patch *patches,
658 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700659{
Eric Laurent6a94d692014-05-20 11:18:06 -0700660 Mutex::Autolock _l(mLock);
661 if (mAudioPolicyManager == NULL) {
662 return NO_INIT;
663 }
664
665 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700666}
667
Eric Laurent6a94d692014-05-20 11:18:06 -0700668status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent203b1a12014-04-01 10:34:16 -0700669{
Eric Laurent6a94d692014-05-20 11:18:06 -0700670 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700671 if(!modifyAudioRoutingAllowed()) {
672 return PERMISSION_DENIED;
673 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700674 if (mAudioPolicyManager == NULL) {
675 return NO_INIT;
676 }
677
678 return mAudioPolicyManager->setAudioPortConfig(config);
Eric Laurent203b1a12014-04-01 10:34:16 -0700679}
Eric Laurent2d388ec2014-03-07 13:25:54 -0800680
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700681status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
682 audio_io_handle_t *ioHandle,
683 audio_devices_t *device)
684{
685 if (mAudioPolicyManager == NULL) {
686 return NO_INIT;
687 }
688
689 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
690}
691
692status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
693{
694 if (mAudioPolicyManager == NULL) {
695 return NO_INIT;
696 }
697
698 return mAudioPolicyManager->releaseSoundTriggerSession(session);
699}
700
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -0700701status_t AudioPolicyService::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800702{
703 Mutex::Autolock _l(mLock);
704 if(!modifyAudioRoutingAllowed()) {
705 return PERMISSION_DENIED;
706 }
707 if (mAudioPolicyManager == NULL) {
708 return NO_INIT;
709 }
710 if (registration) {
711 return mAudioPolicyManager->registerPolicyMixes(mixes);
712 } else {
713 return mAudioPolicyManager->unregisterPolicyMixes(mixes);
714 }
715}
716
Eric Laurent554a2772015-04-10 11:29:24 -0700717status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
718 const audio_attributes_t *attributes,
719 audio_io_handle_t *handle)
720{
721 Mutex::Autolock _l(mLock);
722 if (mAudioPolicyManager == NULL) {
723 return NO_INIT;
724 }
725
Eric Laurentd60560a2015-04-10 11:31:20 -0700726 return mAudioPolicyManager->startAudioSource(source, attributes, handle,
727 IPCThreadState::self()->getCallingUid());
Eric Laurent554a2772015-04-10 11:29:24 -0700728}
729
730status_t AudioPolicyService::stopAudioSource(audio_io_handle_t handle)
731{
732 Mutex::Autolock _l(mLock);
733 if (mAudioPolicyManager == NULL) {
734 return NO_INIT;
735 }
736
737 return mAudioPolicyManager->stopAudioSource(handle);
738}
739
Andy Hung2ddee192015-12-18 17:34:44 -0800740status_t AudioPolicyService::setMasterMono(bool mono)
741{
742 if (mAudioPolicyManager == NULL) {
743 return NO_INIT;
744 }
745 if (!settingsAllowed()) {
746 return PERMISSION_DENIED;
747 }
748 Mutex::Autolock _l(mLock);
749 return mAudioPolicyManager->setMasterMono(mono);
750}
751
752status_t AudioPolicyService::getMasterMono(bool *mono)
753{
754 if (mAudioPolicyManager == NULL) {
755 return NO_INIT;
756 }
757 Mutex::Autolock _l(mLock);
758 return mAudioPolicyManager->getMasterMono(mono);
759}
760
Eric Laurent2d388ec2014-03-07 13:25:54 -0800761}; // namespace android