blob: be86cfa27b5aafac6994b01aac031a8de6d51e8b [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
65status_t AudioPolicyService::setPhoneState(audio_mode_t state)
66{
Eric Laurentdce54a12014-03-10 12:19:46 -070067 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -080068 return NO_INIT;
69 }
70 if (!settingsAllowed()) {
71 return PERMISSION_DENIED;
72 }
73 if (uint32_t(state) >= AUDIO_MODE_CNT) {
74 return BAD_VALUE;
75 }
76
77 ALOGV("setPhoneState()");
78
Eric Laurentbeb07fe2015-09-16 15:49:30 -070079 // acquire lock before calling setMode() so that setMode() + setPhoneState() are an atomic
80 // operation from policy manager standpoint (no other operation (e.g track start or stop)
81 // can be interleaved).
82 Mutex::Autolock _l(mLock);
83
Eric Laurent2d388ec2014-03-07 13:25:54 -080084 // TODO: check if it is more appropriate to do it in platform specific policy manager
85 AudioSystem::setMode(state);
86
Eric Laurentdce54a12014-03-10 12:19:46 -070087 mAudioPolicyManager->setPhoneState(state);
Eric Laurentbb6c9a02014-09-25 14:11:47 -070088 mPhoneState = state;
Eric Laurent2d388ec2014-03-07 13:25:54 -080089 return NO_ERROR;
90}
91
Eric Laurentbb6c9a02014-09-25 14:11:47 -070092audio_mode_t AudioPolicyService::getPhoneState()
93{
94 Mutex::Autolock _l(mLock);
95 return mPhoneState;
96}
97
Eric Laurent2d388ec2014-03-07 13:25:54 -080098status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
99 audio_policy_forced_cfg_t config)
100{
Eric Laurentdce54a12014-03-10 12:19:46 -0700101 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800102 return NO_INIT;
103 }
104 if (!settingsAllowed()) {
105 return PERMISSION_DENIED;
106 }
107 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
108 return BAD_VALUE;
109 }
110 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
111 return BAD_VALUE;
112 }
113 ALOGV("setForceUse()");
114 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700115 mAudioPolicyManager->setForceUse(usage, config);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800116 return NO_ERROR;
117}
118
119audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
120{
Eric Laurentdce54a12014-03-10 12:19:46 -0700121 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800122 return AUDIO_POLICY_FORCE_NONE;
123 }
124 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
125 return AUDIO_POLICY_FORCE_NONE;
126 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700127 return mAudioPolicyManager->getForceUse(usage);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800128}
129
130audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
131 uint32_t samplingRate,
132 audio_format_t format,
133 audio_channel_mask_t channelMask,
134 audio_output_flags_t flags,
135 const audio_offload_info_t *offloadInfo)
136{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800137 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700138 return AUDIO_IO_HANDLE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700139 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700140 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700141 return AUDIO_IO_HANDLE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800142 }
143 ALOGV("getOutput()");
144 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700145 return mAudioPolicyManager->getOutput(stream, samplingRate,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800146 format, channelMask, flags, offloadInfo);
147}
148
Eric Laurente83b55d2014-11-14 10:06:21 -0800149status_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr,
150 audio_io_handle_t *output,
151 audio_session_t session,
152 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700153 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800154 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800155 audio_output_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -0600156 audio_port_handle_t selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800157 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700158{
159 if (mAudioPolicyManager == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800160 return NO_INIT;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700161 }
162 ALOGV("getOutput()");
163 Mutex::Autolock _l(mLock);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700164
Marco Nelissendcb346b2015-09-09 10:47:29 -0700165 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
166 if (!isTrustedCallingUid(callingUid) || uid == (uid_t)-1) {
167 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
168 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
169 uid = callingUid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700170 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800171 return mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid,
172 config,
173 flags, selectedDeviceId, portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700174}
175
Eric Laurent2d388ec2014-03-07 13:25:54 -0800176status_t AudioPolicyService::startOutput(audio_io_handle_t output,
177 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800178 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800179{
Eric Laurentdea15412014-10-28 15:46:45 -0700180 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
181 return BAD_VALUE;
182 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700183 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800184 return NO_INIT;
185 }
186 ALOGV("startOutput()");
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700187 sp<AudioPolicyEffects>audioPolicyEffects;
188 {
189 Mutex::Autolock _l(mLock);
190 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800191 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700192 if (audioPolicyEffects != 0) {
193 // create audio processors according to stream
194 status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session);
195 if (status != NO_ERROR && status != ALREADY_EXISTS) {
196 ALOGW("Failed to add effects on session %d", session);
197 }
198 }
199 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700200 return mAudioPolicyManager->startOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800201}
202
203status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
204 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800205 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800206{
Eric Laurentdea15412014-10-28 15:46:45 -0700207 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
208 return BAD_VALUE;
209 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700210 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800211 return NO_INIT;
212 }
213 ALOGV("stopOutput()");
214 mOutputCommandThread->stopOutputCommand(output, stream, session);
215 return NO_ERROR;
216}
217
218status_t AudioPolicyService::doStopOutput(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{
222 ALOGV("doStopOutput from tid %d", gettid());
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700223 sp<AudioPolicyEffects>audioPolicyEffects;
224 {
225 Mutex::Autolock _l(mLock);
226 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800227 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700228 if (audioPolicyEffects != 0) {
229 // release audio processors from the stream
230 status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
231 if (status != NO_ERROR && status != ALREADY_EXISTS) {
232 ALOGW("Failed to release effects on session %d", session);
233 }
234 }
235 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700236 return mAudioPolicyManager->stopOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800237}
238
Eric Laurente83b55d2014-11-14 10:06:21 -0800239void AudioPolicyService::releaseOutput(audio_io_handle_t output,
240 audio_stream_type_t stream,
241 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800242{
Eric Laurentdce54a12014-03-10 12:19:46 -0700243 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800244 return;
245 }
246 ALOGV("releaseOutput()");
Eric Laurente83b55d2014-11-14 10:06:21 -0800247 mOutputCommandThread->releaseOutputCommand(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800248}
249
Eric Laurente83b55d2014-11-14 10:06:21 -0800250void AudioPolicyService::doReleaseOutput(audio_io_handle_t output,
251 audio_stream_type_t stream,
252 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800253{
254 ALOGV("doReleaseOutput from tid %d", gettid());
255 Mutex::Autolock _l(mLock);
Eric Laurente83b55d2014-11-14 10:06:21 -0800256 mAudioPolicyManager->releaseOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800257}
258
Eric Laurentcaf7f482014-11-25 17:50:47 -0800259status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr,
260 audio_io_handle_t *input,
261 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700262 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700263 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800264 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600265 audio_input_flags_t flags,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800266 audio_port_handle_t selectedDeviceId,
267 audio_port_handle_t *portId)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800268{
Eric Laurentdce54a12014-03-10 12:19:46 -0700269 if (mAudioPolicyManager == NULL) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800270 return NO_INIT;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800271 }
272 // already checked by client, but double-check in case the client wrapper is bypassed
Eric Laurentcaf7f482014-11-25 17:50:47 -0800273 if (attr->source >= AUDIO_SOURCE_CNT && attr->source != AUDIO_SOURCE_HOTWORD &&
274 attr->source != AUDIO_SOURCE_FM_TUNER) {
275 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800276 }
277
Eric Laurentab300c82015-04-13 13:47:33 -0700278 if ((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800279 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800280 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700281 sp<AudioPolicyEffects>audioPolicyEffects;
Eric Laurentcaf7f482014-11-25 17:50:47 -0800282 status_t status;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800283 AudioPolicyInterface::input_type_t inputType;
Eric Laurentb2379ba2016-05-23 17:42:12 -0700284
285 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -0700286 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurentb2379ba2016-05-23 17:42:12 -0700287 if (!isTrustedCallingUid(callingUid)) {
Eric Laurent9f39f8d2016-05-25 12:34:48 -0700288 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -0700289 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
290 uid = callingUid;
Eric Laurentb2379ba2016-05-23 17:42:12 -0700291 updatePid = true;
292 }
293
294 if (updatePid) {
295 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Eric Laurent9f39f8d2016-05-25 12:34:48 -0700296 ALOGW_IF(pid != (pid_t)-1 && pid != callingPid,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700297 "%s uid %d pid %d tried to pass itself off as pid %d",
298 __func__, callingUid, callingPid, pid);
299 pid = callingPid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700300 }
301
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700302 {
303 Mutex::Autolock _l(mLock);
304 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700305 status = mAudioPolicyManager->getInputForAttr(attr, input, session, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800306 config,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700307 flags, selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800308 &inputType, portId);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700309 audioPolicyEffects = mAudioPolicyEffects;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800310
311 if (status == NO_ERROR) {
312 // enforce permission (if any) required for each type of input
313 switch (inputType) {
314 case AudioPolicyInterface::API_INPUT_LEGACY:
315 break;
Eric Laurent82db2692015-08-07 13:59:42 -0700316 case AudioPolicyInterface::API_INPUT_TELEPHONY_RX:
317 // FIXME: use the same permission as for remote submix for now.
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800318 case AudioPolicyInterface::API_INPUT_MIX_CAPTURE:
Eric Laurentb2379ba2016-05-23 17:42:12 -0700319 if (!captureAudioOutputAllowed(pid, uid)) {
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800320 ALOGE("getInputForAttr() permission denied: capture not allowed");
321 status = PERMISSION_DENIED;
322 }
323 break;
324 case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE:
325 if (!modifyAudioRoutingAllowed()) {
326 ALOGE("getInputForAttr() permission denied: modify audio routing not allowed");
327 status = PERMISSION_DENIED;
328 }
329 break;
330 case AudioPolicyInterface::API_INPUT_INVALID:
331 default:
332 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d",
333 (int)inputType);
334 }
335 }
336
337 if (status != NO_ERROR) {
338 if (status == PERMISSION_DENIED) {
339 mAudioPolicyManager->releaseInput(*input, session);
340 }
341 return status;
342 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700343 }
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800344
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700345 if (audioPolicyEffects != 0) {
346 // create audio pre processors according to input source
Eric Laurentcaf7f482014-11-25 17:50:47 -0800347 status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700348 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800349 ALOGW("Failed to add effects on input %d", *input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700350 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800351 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800352 return NO_ERROR;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800353}
354
Eric Laurent4dc68062014-07-28 17:26:49 -0700355status_t AudioPolicyService::startInput(audio_io_handle_t input,
356 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800357{
Eric Laurentdce54a12014-03-10 12:19:46 -0700358 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800359 return NO_INIT;
360 }
361 Mutex::Autolock _l(mLock);
Eric Laurentfb66dd92016-01-28 18:32:03 -0800362 AudioPolicyInterface::concurrency_type__mask_t concurrency;
363 status_t status = mAudioPolicyManager->startInput(input, session, &concurrency);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800364
Eric Laurentfb66dd92016-01-28 18:32:03 -0800365 if (status == NO_ERROR) {
Eric Laurent43423352016-02-05 11:57:57 -0800366 LOG_ALWAYS_FATAL_IF(concurrency & ~AudioPolicyInterface::API_INPUT_CONCURRENCY_ALL,
367 "startInput(): invalid concurrency type %d", (int)concurrency);
368
Eric Laurentfb66dd92016-01-28 18:32:03 -0800369 // enforce permission (if any) required for each type of concurrency
Eric Laurent43423352016-02-05 11:57:57 -0800370 if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CALL) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800371 //TODO: check incall capture permission
Eric Laurent43423352016-02-05 11:57:57 -0800372 }
373 if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CAPTURE) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800374 //TODO: check concurrent capture permission
Eric Laurentfb66dd92016-01-28 18:32:03 -0800375 }
376 }
377
378 return status;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800379}
380
Eric Laurent4dc68062014-07-28 17:26:49 -0700381status_t AudioPolicyService::stopInput(audio_io_handle_t input,
382 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800383{
Eric Laurentdce54a12014-03-10 12:19:46 -0700384 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800385 return NO_INIT;
386 }
387 Mutex::Autolock _l(mLock);
388
Eric Laurent4dc68062014-07-28 17:26:49 -0700389 return mAudioPolicyManager->stopInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800390}
391
Eric Laurent4dc68062014-07-28 17:26:49 -0700392void AudioPolicyService::releaseInput(audio_io_handle_t input,
393 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800394{
Eric Laurentdce54a12014-03-10 12:19:46 -0700395 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800396 return;
397 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700398 sp<AudioPolicyEffects>audioPolicyEffects;
399 {
400 Mutex::Autolock _l(mLock);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700401 audioPolicyEffects = mAudioPolicyEffects;
402 }
403 if (audioPolicyEffects != 0) {
404 // release audio processors from the input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800405 status_t status = audioPolicyEffects->releaseInputEffects(input, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700406 if(status != NO_ERROR) {
407 ALOGW("Failed to release effects on input %d", input);
408 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800409 }
Eric Laurentf10c7092016-12-06 17:09:56 -0800410 {
411 Mutex::Autolock _l(mLock);
412 mAudioPolicyManager->releaseInput(input, session);
413 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800414}
415
416status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
417 int indexMin,
418 int indexMax)
419{
Eric Laurentdce54a12014-03-10 12:19:46 -0700420 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800421 return NO_INIT;
422 }
423 if (!settingsAllowed()) {
424 return PERMISSION_DENIED;
425 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800426 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800427 return BAD_VALUE;
428 }
429 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700430 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800431 return NO_ERROR;
432}
433
434status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
435 int index,
436 audio_devices_t device)
437{
Eric Laurentdce54a12014-03-10 12:19:46 -0700438 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800439 return NO_INIT;
440 }
441 if (!settingsAllowed()) {
442 return PERMISSION_DENIED;
443 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800444 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800445 return BAD_VALUE;
446 }
447 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700448 return mAudioPolicyManager->setStreamVolumeIndex(stream,
449 index,
450 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800451}
452
453status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
454 int *index,
455 audio_devices_t device)
456{
Eric Laurentdce54a12014-03-10 12:19:46 -0700457 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800458 return NO_INIT;
459 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800460 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800461 return BAD_VALUE;
462 }
463 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700464 return mAudioPolicyManager->getStreamVolumeIndex(stream,
465 index,
466 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800467}
468
469uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
470{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800471 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700472 return 0;
Eric Laurentdea15412014-10-28 15:46:45 -0700473 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700474 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800475 return 0;
476 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700477 return mAudioPolicyManager->getStrategyForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800478}
479
480//audio policy: use audio_device_t appropriately
481
482audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
483{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800484 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700485 return AUDIO_DEVICE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700486 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700487 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700488 return AUDIO_DEVICE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800489 }
Haynes Mathew Georgedfb9f3b2015-10-26 18:22:13 -0700490 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700491 return mAudioPolicyManager->getDevicesForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800492}
493
494audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
495{
496 // FIXME change return type to status_t, and return NO_INIT here
Eric Laurentdce54a12014-03-10 12:19:46 -0700497 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800498 return 0;
499 }
500 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700501 return mAudioPolicyManager->getOutputForEffect(desc);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800502}
503
504status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
505 audio_io_handle_t io,
506 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800507 audio_session_t session,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800508 int id)
509{
Eric Laurentdce54a12014-03-10 12:19:46 -0700510 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800511 return NO_INIT;
512 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700513 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700514 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800515}
516
517status_t AudioPolicyService::unregisterEffect(int id)
518{
Eric Laurentdce54a12014-03-10 12:19:46 -0700519 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800520 return NO_INIT;
521 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700522 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700523 return mAudioPolicyManager->unregisterEffect(id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800524}
525
526status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
527{
Eric Laurentdce54a12014-03-10 12:19:46 -0700528 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800529 return NO_INIT;
530 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700531 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700532 return mAudioPolicyManager->setEffectEnabled(id, enabled);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800533}
534
535bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
536{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800537 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700538 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700539 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700540 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700541 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800542 }
543 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700544 return mAudioPolicyManager->isStreamActive(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800545}
546
547bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
548{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800549 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700550 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700551 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700552 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700553 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800554 }
555 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700556 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800557}
558
559bool AudioPolicyService::isSourceActive(audio_source_t source) const
560{
Eric Laurentdce54a12014-03-10 12:19:46 -0700561 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800562 return false;
563 }
564 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700565 return mAudioPolicyManager->isSourceActive(source);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800566}
567
Glenn Kastend848eb42016-03-08 13:42:11 -0800568status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800569 effect_descriptor_t *descriptors,
570 uint32_t *count)
571{
Eric Laurentdce54a12014-03-10 12:19:46 -0700572 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800573 *count = 0;
574 return NO_INIT;
575 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700576 sp<AudioPolicyEffects>audioPolicyEffects;
577 {
578 Mutex::Autolock _l(mLock);
579 audioPolicyEffects = mAudioPolicyEffects;
580 }
581 if (audioPolicyEffects == 0) {
582 *count = 0;
583 return NO_INIT;
584 }
Eric Laurentfb66dd92016-01-28 18:32:03 -0800585 return audioPolicyEffects->queryDefaultInputEffects(
586 (audio_session_t)audioSession, descriptors, count);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800587}
588
589bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
590{
Eric Laurentdce54a12014-03-10 12:19:46 -0700591 if (mAudioPolicyManager == NULL) {
592 ALOGV("mAudioPolicyManager == NULL");
Eric Laurent2d388ec2014-03-07 13:25:54 -0800593 return false;
594 }
Andy Hung2ddee192015-12-18 17:34:44 -0800595 Mutex::Autolock _l(mLock);
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700596 Mutex::Autolock _le(mEffectsLock); // isOffloadSupported queries for
597 // non-offloadable effects
Eric Laurentdce54a12014-03-10 12:19:46 -0700598 return mAudioPolicyManager->isOffloadSupported(info);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800599}
600
Eric Laurent6a94d692014-05-20 11:18:06 -0700601status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
602 audio_port_type_t type,
Eric Laurent203b1a12014-04-01 10:34:16 -0700603 unsigned int *num_ports,
Eric Laurent6a94d692014-05-20 11:18:06 -0700604 struct audio_port *ports,
605 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700606{
Eric Laurent6a94d692014-05-20 11:18:06 -0700607 Mutex::Autolock _l(mLock);
608 if (mAudioPolicyManager == NULL) {
609 return NO_INIT;
610 }
611
612 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700613}
614
Eric Laurent6a94d692014-05-20 11:18:06 -0700615status_t AudioPolicyService::getAudioPort(struct audio_port *port)
Eric Laurent203b1a12014-04-01 10:34:16 -0700616{
Eric Laurent6a94d692014-05-20 11:18:06 -0700617 Mutex::Autolock _l(mLock);
618 if (mAudioPolicyManager == NULL) {
619 return NO_INIT;
620 }
621
622 return mAudioPolicyManager->getAudioPort(port);
Eric Laurent203b1a12014-04-01 10:34:16 -0700623}
624
Eric Laurent6a94d692014-05-20 11:18:06 -0700625status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
626 audio_patch_handle_t *handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700627{
Eric Laurent6a94d692014-05-20 11:18:06 -0700628 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700629 if(!modifyAudioRoutingAllowed()) {
630 return PERMISSION_DENIED;
631 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700632 if (mAudioPolicyManager == NULL) {
633 return NO_INIT;
634 }
635 return mAudioPolicyManager->createAudioPatch(patch, handle,
636 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700637}
638
Eric Laurent6a94d692014-05-20 11:18:06 -0700639status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700640{
Eric Laurent6a94d692014-05-20 11:18:06 -0700641 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700642 if(!modifyAudioRoutingAllowed()) {
643 return PERMISSION_DENIED;
644 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700645 if (mAudioPolicyManager == NULL) {
646 return NO_INIT;
647 }
648
649 return mAudioPolicyManager->releaseAudioPatch(handle,
650 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700651}
652
653status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
Eric Laurent6a94d692014-05-20 11:18:06 -0700654 struct audio_patch *patches,
655 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700656{
Eric Laurent6a94d692014-05-20 11:18:06 -0700657 Mutex::Autolock _l(mLock);
658 if (mAudioPolicyManager == NULL) {
659 return NO_INIT;
660 }
661
662 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700663}
664
Eric Laurent6a94d692014-05-20 11:18:06 -0700665status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent203b1a12014-04-01 10:34:16 -0700666{
Eric Laurent6a94d692014-05-20 11:18:06 -0700667 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700668 if(!modifyAudioRoutingAllowed()) {
669 return PERMISSION_DENIED;
670 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700671 if (mAudioPolicyManager == NULL) {
672 return NO_INIT;
673 }
674
675 return mAudioPolicyManager->setAudioPortConfig(config);
Eric Laurent203b1a12014-04-01 10:34:16 -0700676}
Eric Laurent2d388ec2014-03-07 13:25:54 -0800677
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700678status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
679 audio_io_handle_t *ioHandle,
680 audio_devices_t *device)
681{
682 if (mAudioPolicyManager == NULL) {
683 return NO_INIT;
684 }
685
686 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
687}
688
689status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
690{
691 if (mAudioPolicyManager == NULL) {
692 return NO_INIT;
693 }
694
695 return mAudioPolicyManager->releaseSoundTriggerSession(session);
696}
697
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700698status_t AudioPolicyService::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800699{
700 Mutex::Autolock _l(mLock);
701 if(!modifyAudioRoutingAllowed()) {
702 return PERMISSION_DENIED;
703 }
704 if (mAudioPolicyManager == NULL) {
705 return NO_INIT;
706 }
707 if (registration) {
708 return mAudioPolicyManager->registerPolicyMixes(mixes);
709 } else {
710 return mAudioPolicyManager->unregisterPolicyMixes(mixes);
711 }
712}
713
Eric Laurent554a2772015-04-10 11:29:24 -0700714status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
715 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -0700716 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700717{
718 Mutex::Autolock _l(mLock);
719 if (mAudioPolicyManager == NULL) {
720 return NO_INIT;
721 }
722
Eric Laurentd60560a2015-04-10 11:31:20 -0700723 return mAudioPolicyManager->startAudioSource(source, attributes, handle,
724 IPCThreadState::self()->getCallingUid());
Eric Laurent554a2772015-04-10 11:29:24 -0700725}
726
Glenn Kasten559d4392016-03-29 13:42:57 -0700727status_t AudioPolicyService::stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700728{
729 Mutex::Autolock _l(mLock);
730 if (mAudioPolicyManager == NULL) {
731 return NO_INIT;
732 }
733
734 return mAudioPolicyManager->stopAudioSource(handle);
735}
736
Andy Hung2ddee192015-12-18 17:34:44 -0800737status_t AudioPolicyService::setMasterMono(bool mono)
738{
739 if (mAudioPolicyManager == NULL) {
740 return NO_INIT;
741 }
742 if (!settingsAllowed()) {
743 return PERMISSION_DENIED;
744 }
745 Mutex::Autolock _l(mLock);
746 return mAudioPolicyManager->setMasterMono(mono);
747}
748
749status_t AudioPolicyService::getMasterMono(bool *mono)
750{
751 if (mAudioPolicyManager == NULL) {
752 return NO_INIT;
753 }
754 Mutex::Autolock _l(mLock);
755 return mAudioPolicyManager->getMasterMono(mono);
756}
757
Eric Laurent2d388ec2014-03-07 13:25:54 -0800758}; // namespace android