blob: 52aa143893ea900ebebb8e7784dbe909b89d7f04 [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 Laurent20b9ef02016-12-05 11:03:16 -0800174 const audio_config_t *config,
Eric Laurente83b55d2014-11-14 10:06:21 -0800175 audio_output_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -0600176 audio_port_handle_t selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800177 audio_port_handle_t *portId)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700178{
179 if (mAudioPolicyManager == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800180 return NO_INIT;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700181 }
182 ALOGV("getOutput()");
183 Mutex::Autolock _l(mLock);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700184
Marco Nelissendcb346b2015-09-09 10:47:29 -0700185 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
186 if (!isTrustedCallingUid(callingUid) || uid == (uid_t)-1) {
187 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
188 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
189 uid = callingUid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700190 }
Eric Laurent20b9ef02016-12-05 11:03:16 -0800191 return mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid,
192 config,
193 flags, selectedDeviceId, portId);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700194}
195
Eric Laurent2d388ec2014-03-07 13:25:54 -0800196status_t AudioPolicyService::startOutput(audio_io_handle_t output,
197 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800198 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800199{
Eric Laurentdea15412014-10-28 15:46:45 -0700200 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
201 return BAD_VALUE;
202 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700203 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800204 return NO_INIT;
205 }
206 ALOGV("startOutput()");
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700207 sp<AudioPolicyEffects>audioPolicyEffects;
208 {
209 Mutex::Autolock _l(mLock);
210 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800211 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700212 if (audioPolicyEffects != 0) {
213 // create audio processors according to stream
214 status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session);
215 if (status != NO_ERROR && status != ALREADY_EXISTS) {
216 ALOGW("Failed to add effects on session %d", session);
217 }
218 }
219 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700220 return mAudioPolicyManager->startOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800221}
222
223status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
224 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800225 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800226{
Eric Laurentdea15412014-10-28 15:46:45 -0700227 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
228 return BAD_VALUE;
229 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700230 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800231 return NO_INIT;
232 }
233 ALOGV("stopOutput()");
234 mOutputCommandThread->stopOutputCommand(output, stream, session);
235 return NO_ERROR;
236}
237
238status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
239 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800240 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800241{
242 ALOGV("doStopOutput from tid %d", gettid());
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700243 sp<AudioPolicyEffects>audioPolicyEffects;
244 {
245 Mutex::Autolock _l(mLock);
246 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800247 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700248 if (audioPolicyEffects != 0) {
249 // release audio processors from the stream
250 status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
251 if (status != NO_ERROR && status != ALREADY_EXISTS) {
252 ALOGW("Failed to release effects on session %d", session);
253 }
254 }
255 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700256 return mAudioPolicyManager->stopOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800257}
258
Eric Laurente83b55d2014-11-14 10:06:21 -0800259void AudioPolicyService::releaseOutput(audio_io_handle_t output,
260 audio_stream_type_t stream,
261 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800262{
Eric Laurentdce54a12014-03-10 12:19:46 -0700263 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800264 return;
265 }
266 ALOGV("releaseOutput()");
Eric Laurente83b55d2014-11-14 10:06:21 -0800267 mOutputCommandThread->releaseOutputCommand(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800268}
269
Eric Laurente83b55d2014-11-14 10:06:21 -0800270void AudioPolicyService::doReleaseOutput(audio_io_handle_t output,
271 audio_stream_type_t stream,
272 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800273{
274 ALOGV("doReleaseOutput from tid %d", gettid());
275 Mutex::Autolock _l(mLock);
Eric Laurente83b55d2014-11-14 10:06:21 -0800276 mAudioPolicyManager->releaseOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800277}
278
Eric Laurentcaf7f482014-11-25 17:50:47 -0800279status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr,
280 audio_io_handle_t *input,
281 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700282 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700283 uid_t uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800284 const audio_config_base_t *config,
Paul McLean466dc8e2015-04-17 13:15:36 -0600285 audio_input_flags_t flags,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800286 audio_port_handle_t selectedDeviceId,
287 audio_port_handle_t *portId)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800288{
Eric Laurentdce54a12014-03-10 12:19:46 -0700289 if (mAudioPolicyManager == NULL) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800290 return NO_INIT;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800291 }
292 // already checked by client, but double-check in case the client wrapper is bypassed
Eric Laurentcaf7f482014-11-25 17:50:47 -0800293 if (attr->source >= AUDIO_SOURCE_CNT && attr->source != AUDIO_SOURCE_HOTWORD &&
294 attr->source != AUDIO_SOURCE_FM_TUNER) {
295 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800296 }
297
Eric Laurentab300c82015-04-13 13:47:33 -0700298 if ((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800299 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800300 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700301 sp<AudioPolicyEffects>audioPolicyEffects;
Eric Laurentcaf7f482014-11-25 17:50:47 -0800302 status_t status;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800303 AudioPolicyInterface::input_type_t inputType;
Eric Laurentb2379ba2016-05-23 17:42:12 -0700304
305 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -0700306 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurentb2379ba2016-05-23 17:42:12 -0700307 if (!isTrustedCallingUid(callingUid)) {
Eric Laurent9f39f8d2016-05-25 12:34:48 -0700308 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -0700309 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
310 uid = callingUid;
Eric Laurentb2379ba2016-05-23 17:42:12 -0700311 updatePid = true;
312 }
313
314 if (updatePid) {
315 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
Eric Laurent9f39f8d2016-05-25 12:34:48 -0700316 ALOGW_IF(pid != (pid_t)-1 && pid != callingPid,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700317 "%s uid %d pid %d tried to pass itself off as pid %d",
318 __func__, callingUid, callingPid, pid);
319 pid = callingPid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700320 }
321
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700322 {
323 Mutex::Autolock _l(mLock);
324 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700325 status = mAudioPolicyManager->getInputForAttr(attr, input, session, uid,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800326 config,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700327 flags, selectedDeviceId,
Eric Laurent20b9ef02016-12-05 11:03:16 -0800328 &inputType, portId);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700329 audioPolicyEffects = mAudioPolicyEffects;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800330
331 if (status == NO_ERROR) {
332 // enforce permission (if any) required for each type of input
333 switch (inputType) {
334 case AudioPolicyInterface::API_INPUT_LEGACY:
335 break;
Eric Laurent82db2692015-08-07 13:59:42 -0700336 case AudioPolicyInterface::API_INPUT_TELEPHONY_RX:
337 // FIXME: use the same permission as for remote submix for now.
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800338 case AudioPolicyInterface::API_INPUT_MIX_CAPTURE:
Eric Laurentb2379ba2016-05-23 17:42:12 -0700339 if (!captureAudioOutputAllowed(pid, uid)) {
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800340 ALOGE("getInputForAttr() permission denied: capture not allowed");
341 status = PERMISSION_DENIED;
342 }
343 break;
344 case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE:
345 if (!modifyAudioRoutingAllowed()) {
346 ALOGE("getInputForAttr() permission denied: modify audio routing not allowed");
347 status = PERMISSION_DENIED;
348 }
349 break;
350 case AudioPolicyInterface::API_INPUT_INVALID:
351 default:
352 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d",
353 (int)inputType);
354 }
355 }
356
357 if (status != NO_ERROR) {
358 if (status == PERMISSION_DENIED) {
359 mAudioPolicyManager->releaseInput(*input, session);
360 }
361 return status;
362 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700363 }
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800364
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700365 if (audioPolicyEffects != 0) {
366 // create audio pre processors according to input source
Eric Laurentcaf7f482014-11-25 17:50:47 -0800367 status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700368 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800369 ALOGW("Failed to add effects on input %d", *input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700370 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800371 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800372 return NO_ERROR;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800373}
374
Eric Laurent4dc68062014-07-28 17:26:49 -0700375status_t AudioPolicyService::startInput(audio_io_handle_t input,
376 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800377{
Eric Laurentdce54a12014-03-10 12:19:46 -0700378 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800379 return NO_INIT;
380 }
381 Mutex::Autolock _l(mLock);
Eric Laurentfb66dd92016-01-28 18:32:03 -0800382 AudioPolicyInterface::concurrency_type__mask_t concurrency;
383 status_t status = mAudioPolicyManager->startInput(input, session, &concurrency);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800384
Eric Laurentfb66dd92016-01-28 18:32:03 -0800385 if (status == NO_ERROR) {
Eric Laurent43423352016-02-05 11:57:57 -0800386 LOG_ALWAYS_FATAL_IF(concurrency & ~AudioPolicyInterface::API_INPUT_CONCURRENCY_ALL,
387 "startInput(): invalid concurrency type %d", (int)concurrency);
388
Eric Laurentfb66dd92016-01-28 18:32:03 -0800389 // enforce permission (if any) required for each type of concurrency
Eric Laurent43423352016-02-05 11:57:57 -0800390 if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CALL) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800391 //TODO: check incall capture permission
Eric Laurent43423352016-02-05 11:57:57 -0800392 }
393 if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CAPTURE) {
Eric Laurentfb66dd92016-01-28 18:32:03 -0800394 //TODO: check concurrent capture permission
Eric Laurentfb66dd92016-01-28 18:32:03 -0800395 }
396 }
397
398 return status;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800399}
400
Eric Laurent4dc68062014-07-28 17:26:49 -0700401status_t AudioPolicyService::stopInput(audio_io_handle_t input,
402 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800403{
Eric Laurentdce54a12014-03-10 12:19:46 -0700404 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800405 return NO_INIT;
406 }
407 Mutex::Autolock _l(mLock);
408
Eric Laurent4dc68062014-07-28 17:26:49 -0700409 return mAudioPolicyManager->stopInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800410}
411
Eric Laurent4dc68062014-07-28 17:26:49 -0700412void AudioPolicyService::releaseInput(audio_io_handle_t input,
413 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800414{
Eric Laurentdce54a12014-03-10 12:19:46 -0700415 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800416 return;
417 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700418 sp<AudioPolicyEffects>audioPolicyEffects;
419 {
420 Mutex::Autolock _l(mLock);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700421 audioPolicyEffects = mAudioPolicyEffects;
422 }
423 if (audioPolicyEffects != 0) {
424 // release audio processors from the input
Eric Laurentfb66dd92016-01-28 18:32:03 -0800425 status_t status = audioPolicyEffects->releaseInputEffects(input, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700426 if(status != NO_ERROR) {
427 ALOGW("Failed to release effects on input %d", input);
428 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800429 }
Eric Laurentf10c7092016-12-06 17:09:56 -0800430 {
431 Mutex::Autolock _l(mLock);
432 mAudioPolicyManager->releaseInput(input, session);
433 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800434}
435
436status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
437 int indexMin,
438 int indexMax)
439{
Eric Laurentdce54a12014-03-10 12:19:46 -0700440 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800441 return NO_INIT;
442 }
443 if (!settingsAllowed()) {
444 return PERMISSION_DENIED;
445 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800446 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800447 return BAD_VALUE;
448 }
449 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700450 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800451 return NO_ERROR;
452}
453
454status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
455 int index,
456 audio_devices_t device)
457{
Eric Laurentdce54a12014-03-10 12:19:46 -0700458 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800459 return NO_INIT;
460 }
461 if (!settingsAllowed()) {
462 return PERMISSION_DENIED;
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->setStreamVolumeIndex(stream,
469 index,
470 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800471}
472
473status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
474 int *index,
475 audio_devices_t device)
476{
Eric Laurentdce54a12014-03-10 12:19:46 -0700477 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800478 return NO_INIT;
479 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800480 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800481 return BAD_VALUE;
482 }
483 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700484 return mAudioPolicyManager->getStreamVolumeIndex(stream,
485 index,
486 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800487}
488
489uint32_t AudioPolicyService::getStrategyForStream(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 0;
Eric Laurentdea15412014-10-28 15:46:45 -0700493 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700494 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800495 return 0;
496 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700497 return mAudioPolicyManager->getStrategyForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800498}
499
500//audio policy: use audio_device_t appropriately
501
502audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
503{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800504 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700505 return AUDIO_DEVICE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700506 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700507 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700508 return AUDIO_DEVICE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800509 }
Haynes Mathew Georgedfb9f3b2015-10-26 18:22:13 -0700510 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700511 return mAudioPolicyManager->getDevicesForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800512}
513
514audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
515{
516 // FIXME change return type to status_t, and return NO_INIT here
Eric Laurentdce54a12014-03-10 12:19:46 -0700517 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800518 return 0;
519 }
520 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700521 return mAudioPolicyManager->getOutputForEffect(desc);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800522}
523
524status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
525 audio_io_handle_t io,
526 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800527 audio_session_t session,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800528 int id)
529{
Eric Laurentdce54a12014-03-10 12:19:46 -0700530 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800531 return NO_INIT;
532 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700533 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700534 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800535}
536
537status_t AudioPolicyService::unregisterEffect(int id)
538{
Eric Laurentdce54a12014-03-10 12:19:46 -0700539 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800540 return NO_INIT;
541 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700542 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700543 return mAudioPolicyManager->unregisterEffect(id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800544}
545
546status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
547{
Eric Laurentdce54a12014-03-10 12:19:46 -0700548 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800549 return NO_INIT;
550 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700551 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700552 return mAudioPolicyManager->setEffectEnabled(id, enabled);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800553}
554
555bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
556{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800557 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700558 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700559 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700560 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700561 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800562 }
563 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700564 return mAudioPolicyManager->isStreamActive(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800565}
566
567bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
568{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800569 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700570 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700571 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700572 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700573 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800574 }
575 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700576 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800577}
578
579bool AudioPolicyService::isSourceActive(audio_source_t source) const
580{
Eric Laurentdce54a12014-03-10 12:19:46 -0700581 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800582 return false;
583 }
584 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700585 return mAudioPolicyManager->isSourceActive(source);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800586}
587
Glenn Kastend848eb42016-03-08 13:42:11 -0800588status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800589 effect_descriptor_t *descriptors,
590 uint32_t *count)
591{
Eric Laurentdce54a12014-03-10 12:19:46 -0700592 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800593 *count = 0;
594 return NO_INIT;
595 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700596 sp<AudioPolicyEffects>audioPolicyEffects;
597 {
598 Mutex::Autolock _l(mLock);
599 audioPolicyEffects = mAudioPolicyEffects;
600 }
601 if (audioPolicyEffects == 0) {
602 *count = 0;
603 return NO_INIT;
604 }
Eric Laurentfb66dd92016-01-28 18:32:03 -0800605 return audioPolicyEffects->queryDefaultInputEffects(
606 (audio_session_t)audioSession, descriptors, count);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800607}
608
609bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
610{
Eric Laurentdce54a12014-03-10 12:19:46 -0700611 if (mAudioPolicyManager == NULL) {
612 ALOGV("mAudioPolicyManager == NULL");
Eric Laurent2d388ec2014-03-07 13:25:54 -0800613 return false;
614 }
Andy Hung2ddee192015-12-18 17:34:44 -0800615 Mutex::Autolock _l(mLock);
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700616 Mutex::Autolock _le(mEffectsLock); // isOffloadSupported queries for
617 // non-offloadable effects
Eric Laurentdce54a12014-03-10 12:19:46 -0700618 return mAudioPolicyManager->isOffloadSupported(info);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800619}
620
Eric Laurent6a94d692014-05-20 11:18:06 -0700621status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
622 audio_port_type_t type,
Eric Laurent203b1a12014-04-01 10:34:16 -0700623 unsigned int *num_ports,
Eric Laurent6a94d692014-05-20 11:18:06 -0700624 struct audio_port *ports,
625 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700626{
Eric Laurent6a94d692014-05-20 11:18:06 -0700627 Mutex::Autolock _l(mLock);
628 if (mAudioPolicyManager == NULL) {
629 return NO_INIT;
630 }
631
632 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700633}
634
Eric Laurent6a94d692014-05-20 11:18:06 -0700635status_t AudioPolicyService::getAudioPort(struct audio_port *port)
Eric Laurent203b1a12014-04-01 10:34:16 -0700636{
Eric Laurent6a94d692014-05-20 11:18:06 -0700637 Mutex::Autolock _l(mLock);
638 if (mAudioPolicyManager == NULL) {
639 return NO_INIT;
640 }
641
642 return mAudioPolicyManager->getAudioPort(port);
Eric Laurent203b1a12014-04-01 10:34:16 -0700643}
644
Eric Laurent6a94d692014-05-20 11:18:06 -0700645status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
646 audio_patch_handle_t *handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700647{
Eric Laurent6a94d692014-05-20 11:18:06 -0700648 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700649 if(!modifyAudioRoutingAllowed()) {
650 return PERMISSION_DENIED;
651 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700652 if (mAudioPolicyManager == NULL) {
653 return NO_INIT;
654 }
655 return mAudioPolicyManager->createAudioPatch(patch, handle,
656 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700657}
658
Eric Laurent6a94d692014-05-20 11:18:06 -0700659status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700660{
Eric Laurent6a94d692014-05-20 11:18:06 -0700661 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700662 if(!modifyAudioRoutingAllowed()) {
663 return PERMISSION_DENIED;
664 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700665 if (mAudioPolicyManager == NULL) {
666 return NO_INIT;
667 }
668
669 return mAudioPolicyManager->releaseAudioPatch(handle,
670 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700671}
672
673status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
Eric Laurent6a94d692014-05-20 11:18:06 -0700674 struct audio_patch *patches,
675 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700676{
Eric Laurent6a94d692014-05-20 11:18:06 -0700677 Mutex::Autolock _l(mLock);
678 if (mAudioPolicyManager == NULL) {
679 return NO_INIT;
680 }
681
682 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700683}
684
Eric Laurent6a94d692014-05-20 11:18:06 -0700685status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent203b1a12014-04-01 10:34:16 -0700686{
Eric Laurent6a94d692014-05-20 11:18:06 -0700687 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700688 if(!modifyAudioRoutingAllowed()) {
689 return PERMISSION_DENIED;
690 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700691 if (mAudioPolicyManager == NULL) {
692 return NO_INIT;
693 }
694
695 return mAudioPolicyManager->setAudioPortConfig(config);
Eric Laurent203b1a12014-04-01 10:34:16 -0700696}
Eric Laurent2d388ec2014-03-07 13:25:54 -0800697
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700698status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
699 audio_io_handle_t *ioHandle,
700 audio_devices_t *device)
701{
702 if (mAudioPolicyManager == NULL) {
703 return NO_INIT;
704 }
705
706 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
707}
708
709status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
710{
711 if (mAudioPolicyManager == NULL) {
712 return NO_INIT;
713 }
714
715 return mAudioPolicyManager->releaseSoundTriggerSession(session);
716}
717
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700718status_t AudioPolicyService::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
Eric Laurentbaac1832014-12-01 17:52:59 -0800719{
720 Mutex::Autolock _l(mLock);
721 if(!modifyAudioRoutingAllowed()) {
722 return PERMISSION_DENIED;
723 }
724 if (mAudioPolicyManager == NULL) {
725 return NO_INIT;
726 }
727 if (registration) {
728 return mAudioPolicyManager->registerPolicyMixes(mixes);
729 } else {
730 return mAudioPolicyManager->unregisterPolicyMixes(mixes);
731 }
732}
733
Eric Laurent554a2772015-04-10 11:29:24 -0700734status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
735 const audio_attributes_t *attributes,
Glenn Kasten559d4392016-03-29 13:42:57 -0700736 audio_patch_handle_t *handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700737{
738 Mutex::Autolock _l(mLock);
739 if (mAudioPolicyManager == NULL) {
740 return NO_INIT;
741 }
742
Eric Laurentd60560a2015-04-10 11:31:20 -0700743 return mAudioPolicyManager->startAudioSource(source, attributes, handle,
744 IPCThreadState::self()->getCallingUid());
Eric Laurent554a2772015-04-10 11:29:24 -0700745}
746
Glenn Kasten559d4392016-03-29 13:42:57 -0700747status_t AudioPolicyService::stopAudioSource(audio_patch_handle_t handle)
Eric Laurent554a2772015-04-10 11:29:24 -0700748{
749 Mutex::Autolock _l(mLock);
750 if (mAudioPolicyManager == NULL) {
751 return NO_INIT;
752 }
753
754 return mAudioPolicyManager->stopAudioSource(handle);
755}
756
Andy Hung2ddee192015-12-18 17:34:44 -0800757status_t AudioPolicyService::setMasterMono(bool mono)
758{
759 if (mAudioPolicyManager == NULL) {
760 return NO_INIT;
761 }
762 if (!settingsAllowed()) {
763 return PERMISSION_DENIED;
764 }
765 Mutex::Autolock _l(mLock);
766 return mAudioPolicyManager->setMasterMono(mono);
767}
768
769status_t AudioPolicyService::getMasterMono(bool *mono)
770{
771 if (mAudioPolicyManager == NULL) {
772 return NO_INIT;
773 }
774 Mutex::Autolock _l(mLock);
775 return mAudioPolicyManager->getMasterMono(mono);
776}
777
Eric Laurent2d388ec2014-03-07 13:25:54 -0800778}; // namespace android