blob: 02603b83e61f7fe5c6ab0f89d29729ed98539aac [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 Laurente83b55d2014-11-14 10:06:21 -0800154 uint32_t samplingRate,
155 audio_format_t format,
156 audio_channel_mask_t channelMask,
157 audio_output_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -0600158 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800159 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700160{
161 if (mAudioPolicyManager == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800162 return NO_INIT;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700163 }
164 ALOGV("getOutput()");
165 Mutex::Autolock _l(mLock);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700166
Marco Nelissendcb346b2015-09-09 10:47:29 -0700167 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
168 if (!isTrustedCallingUid(callingUid) || uid == (uid_t)-1) {
169 ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
170 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
171 uid = callingUid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700172 }
173 return mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid, samplingRate,
Paul McLean466dc8e2015-04-17 13:15:36 -0600174 format, channelMask, flags, selectedDeviceId, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700175}
176
Eric Laurent2d388ec2014-03-07 13:25:54 -0800177status_t AudioPolicyService::startOutput(audio_io_handle_t output,
178 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800179 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800180{
Eric Laurentdea15412014-10-28 15:46:45 -0700181 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
182 return BAD_VALUE;
183 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700184 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800185 return NO_INIT;
186 }
187 ALOGV("startOutput()");
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700188 sp<AudioPolicyEffects>audioPolicyEffects;
189 {
190 Mutex::Autolock _l(mLock);
191 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800192 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700193 if (audioPolicyEffects != 0) {
194 // create audio processors according to stream
195 status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session);
196 if (status != NO_ERROR && status != ALREADY_EXISTS) {
197 ALOGW("Failed to add effects on session %d", session);
198 }
199 }
200 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700201 return mAudioPolicyManager->startOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800202}
203
204status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
205 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800206 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800207{
Eric Laurentdea15412014-10-28 15:46:45 -0700208 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
209 return BAD_VALUE;
210 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700211 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800212 return NO_INIT;
213 }
214 ALOGV("stopOutput()");
215 mOutputCommandThread->stopOutputCommand(output, stream, session);
216 return NO_ERROR;
217}
218
219status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
220 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800221 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800222{
223 ALOGV("doStopOutput from tid %d", gettid());
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700224 sp<AudioPolicyEffects>audioPolicyEffects;
225 {
226 Mutex::Autolock _l(mLock);
227 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800228 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700229 if (audioPolicyEffects != 0) {
230 // release audio processors from the stream
231 status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
232 if (status != NO_ERROR && status != ALREADY_EXISTS) {
233 ALOGW("Failed to release effects on session %d", session);
234 }
235 }
236 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700237 return mAudioPolicyManager->stopOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800238}
239
Eric Laurente83b55d2014-11-14 10:06:21 -0800240void AudioPolicyService::releaseOutput(audio_io_handle_t output,
241 audio_stream_type_t stream,
242 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800243{
Eric Laurentdce54a12014-03-10 12:19:46 -0700244 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800245 return;
246 }
247 ALOGV("releaseOutput()");
Eric Laurente83b55d2014-11-14 10:06:21 -0800248 mOutputCommandThread->releaseOutputCommand(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800249}
250
Eric Laurente83b55d2014-11-14 10:06:21 -0800251void AudioPolicyService::doReleaseOutput(audio_io_handle_t output,
252 audio_stream_type_t stream,
253 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800254{
255 ALOGV("doReleaseOutput from tid %d", gettid());
256 Mutex::Autolock _l(mLock);
Eric Laurente83b55d2014-11-14 10:06:21 -0800257 mAudioPolicyManager->releaseOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800258}
259
Eric Laurentcaf7f482014-11-25 17:50:47 -0800260status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr,
261 audio_io_handle_t *input,
262 audio_session_t session,
Eric Laurentb2379ba2016-05-23 17:42:12 -0700263 pid_t pid,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700264 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800265 uint32_t samplingRate,
266 audio_format_t format,
267 audio_channel_mask_t channelMask,
Paul McLean466dc8e2015-04-17 13:15:36 -0600268 audio_input_flags_t flags,
269 audio_port_handle_t selectedDeviceId)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800270{
Eric Laurentdce54a12014-03-10 12:19:46 -0700271 if (mAudioPolicyManager == NULL) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800272 return NO_INIT;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800273 }
274 // already checked by client, but double-check in case the client wrapper is bypassed
Eric Laurentcaf7f482014-11-25 17:50:47 -0800275 if (attr->source >= AUDIO_SOURCE_CNT && attr->source != AUDIO_SOURCE_HOTWORD &&
276 attr->source != AUDIO_SOURCE_FM_TUNER) {
277 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800278 }
279
Eric Laurentab300c82015-04-13 13:47:33 -0700280 if ((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800281 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800282 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700283 sp<AudioPolicyEffects>audioPolicyEffects;
Eric Laurentcaf7f482014-11-25 17:50:47 -0800284 status_t status;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800285 AudioPolicyInterface::input_type_t inputType;
Eric Laurentb2379ba2016-05-23 17:42:12 -0700286
287 bool updatePid = (pid == -1);
Marco Nelissendcb346b2015-09-09 10:47:29 -0700288 const uid_t callingUid = IPCThreadState::self()->getCallingUid();
Eric Laurentb2379ba2016-05-23 17:42:12 -0700289 if (!isTrustedCallingUid(callingUid)) {
290 ALOGW_IF(uid != -1 && uid != (int)callingUid,
Marco Nelissendcb346b2015-09-09 10:47:29 -0700291 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
292 uid = callingUid;
Eric Laurentb2379ba2016-05-23 17:42:12 -0700293 updatePid = true;
294 }
295
296 if (updatePid) {
297 const pid_t callingPid = IPCThreadState::self()->getCallingPid();
298 ALOGW_IF(pid != -1 && pid != callingPid,
299 "%s uid %d pid %d tried to pass itself off as pid %d",
300 __func__, callingUid, callingPid, pid);
301 pid = callingPid;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700302 }
303
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700304 {
305 Mutex::Autolock _l(mLock);
306 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700307 status = mAudioPolicyManager->getInputForAttr(attr, input, session, uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800308 samplingRate, format, channelMask,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700309 flags, selectedDeviceId,
310 &inputType);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700311 audioPolicyEffects = mAudioPolicyEffects;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800312
313 if (status == NO_ERROR) {
314 // enforce permission (if any) required for each type of input
315 switch (inputType) {
316 case AudioPolicyInterface::API_INPUT_LEGACY:
317 break;
Eric Laurent82db2692015-08-07 13:59:42 -0700318 case AudioPolicyInterface::API_INPUT_TELEPHONY_RX:
319 // FIXME: use the same permission as for remote submix for now.
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800320 case AudioPolicyInterface::API_INPUT_MIX_CAPTURE:
Eric Laurentb2379ba2016-05-23 17:42:12 -0700321 if (!captureAudioOutputAllowed(pid, uid)) {
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800322 ALOGE("getInputForAttr() permission denied: capture not allowed");
323 status = PERMISSION_DENIED;
324 }
325 break;
326 case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE:
327 if (!modifyAudioRoutingAllowed()) {
328 ALOGE("getInputForAttr() permission denied: modify audio routing not allowed");
329 status = PERMISSION_DENIED;
330 }
331 break;
332 case AudioPolicyInterface::API_INPUT_INVALID:
333 default:
334 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d",
335 (int)inputType);
336 }
337 }
338
339 if (status != NO_ERROR) {
340 if (status == PERMISSION_DENIED) {
341 mAudioPolicyManager->releaseInput(*input, session);
342 }
343 return status;
344 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700345 }
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800346
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700347 if (audioPolicyEffects != 0) {
348 // create audio pre processors according to input source
Eric Laurentcaf7f482014-11-25 17:50:47 -0800349 status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700350 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800351 ALOGW("Failed to add effects on input %d", *input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700352 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800353 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800354 return NO_ERROR;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800355}
356
Eric Laurent4dc68062014-07-28 17:26:49 -0700357status_t AudioPolicyService::startInput(audio_io_handle_t input,
358 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800359{
Eric Laurentdce54a12014-03-10 12:19:46 -0700360 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800361 return NO_INIT;
362 }
363 Mutex::Autolock _l(mLock);
364
Eric Laurent232f26f2016-02-17 18:06:27 -0800365 return mAudioPolicyManager->startInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800366}
367
Eric Laurent4dc68062014-07-28 17:26:49 -0700368status_t AudioPolicyService::stopInput(audio_io_handle_t input,
369 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800370{
Eric Laurentdce54a12014-03-10 12:19:46 -0700371 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800372 return NO_INIT;
373 }
374 Mutex::Autolock _l(mLock);
375
Eric Laurent4dc68062014-07-28 17:26:49 -0700376 return mAudioPolicyManager->stopInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800377}
378
Eric Laurent4dc68062014-07-28 17:26:49 -0700379void AudioPolicyService::releaseInput(audio_io_handle_t input,
380 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800381{
Eric Laurentdce54a12014-03-10 12:19:46 -0700382 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800383 return;
384 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700385 sp<AudioPolicyEffects>audioPolicyEffects;
386 {
387 Mutex::Autolock _l(mLock);
388 mAudioPolicyManager->releaseInput(input, session);
389 audioPolicyEffects = mAudioPolicyEffects;
390 }
391 if (audioPolicyEffects != 0) {
392 // release audio processors from the input
Eric Laurent232f26f2016-02-17 18:06:27 -0800393 status_t status = audioPolicyEffects->releaseInputEffects(input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700394 if(status != NO_ERROR) {
395 ALOGW("Failed to release effects on input %d", input);
396 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800397 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800398}
399
400status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
401 int indexMin,
402 int indexMax)
403{
Eric Laurentdce54a12014-03-10 12:19:46 -0700404 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800405 return NO_INIT;
406 }
407 if (!settingsAllowed()) {
408 return PERMISSION_DENIED;
409 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800410 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800411 return BAD_VALUE;
412 }
413 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700414 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800415 return NO_ERROR;
416}
417
418status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
419 int index,
420 audio_devices_t device)
421{
Eric Laurentdce54a12014-03-10 12:19:46 -0700422 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800423 return NO_INIT;
424 }
425 if (!settingsAllowed()) {
426 return PERMISSION_DENIED;
427 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800428 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800429 return BAD_VALUE;
430 }
431 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700432 return mAudioPolicyManager->setStreamVolumeIndex(stream,
433 index,
434 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800435}
436
437status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
438 int *index,
439 audio_devices_t device)
440{
Eric Laurentdce54a12014-03-10 12:19:46 -0700441 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800442 return NO_INIT;
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->getStreamVolumeIndex(stream,
449 index,
450 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800451}
452
453uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
454{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800455 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700456 return 0;
Eric Laurentdea15412014-10-28 15:46:45 -0700457 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700458 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800459 return 0;
460 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700461 return mAudioPolicyManager->getStrategyForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800462}
463
464//audio policy: use audio_device_t appropriately
465
466audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
467{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800468 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700469 return AUDIO_DEVICE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700470 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700471 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700472 return AUDIO_DEVICE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800473 }
Haynes Mathew Georgedfb9f3b2015-10-26 18:22:13 -0700474 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700475 return mAudioPolicyManager->getDevicesForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800476}
477
478audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
479{
480 // FIXME change return type to status_t, and return NO_INIT here
Eric Laurentdce54a12014-03-10 12:19:46 -0700481 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800482 return 0;
483 }
484 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700485 return mAudioPolicyManager->getOutputForEffect(desc);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800486}
487
488status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
489 audio_io_handle_t io,
490 uint32_t strategy,
Glenn Kastend848eb42016-03-08 13:42:11 -0800491 audio_session_t session,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800492 int id)
493{
Eric Laurentdce54a12014-03-10 12:19:46 -0700494 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800495 return NO_INIT;
496 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700497 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700498 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800499}
500
501status_t AudioPolicyService::unregisterEffect(int id)
502{
Eric Laurentdce54a12014-03-10 12:19:46 -0700503 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800504 return NO_INIT;
505 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700506 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700507 return mAudioPolicyManager->unregisterEffect(id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800508}
509
510status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
511{
Eric Laurentdce54a12014-03-10 12:19:46 -0700512 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800513 return NO_INIT;
514 }
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700515 Mutex::Autolock _l(mEffectsLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700516 return mAudioPolicyManager->setEffectEnabled(id, enabled);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800517}
518
519bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
520{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800521 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700522 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700523 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700524 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700525 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800526 }
527 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700528 return mAudioPolicyManager->isStreamActive(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800529}
530
531bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
532{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800533 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700534 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700535 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700536 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700537 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800538 }
539 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700540 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800541}
542
543bool AudioPolicyService::isSourceActive(audio_source_t source) const
544{
Eric Laurentdce54a12014-03-10 12:19:46 -0700545 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800546 return false;
547 }
548 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700549 return mAudioPolicyManager->isSourceActive(source);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800550}
551
Glenn Kastend848eb42016-03-08 13:42:11 -0800552status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800553 effect_descriptor_t *descriptors,
554 uint32_t *count)
555{
Eric Laurentdce54a12014-03-10 12:19:46 -0700556 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800557 *count = 0;
558 return NO_INIT;
559 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700560 sp<AudioPolicyEffects>audioPolicyEffects;
561 {
562 Mutex::Autolock _l(mLock);
563 audioPolicyEffects = mAudioPolicyEffects;
564 }
565 if (audioPolicyEffects == 0) {
566 *count = 0;
567 return NO_INIT;
568 }
Eric Laurent232f26f2016-02-17 18:06:27 -0800569 return audioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800570}
571
572bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
573{
Eric Laurentdce54a12014-03-10 12:19:46 -0700574 if (mAudioPolicyManager == NULL) {
575 ALOGV("mAudioPolicyManager == NULL");
Eric Laurent2d388ec2014-03-07 13:25:54 -0800576 return false;
577 }
Andy Hung2ddee192015-12-18 17:34:44 -0800578 Mutex::Autolock _l(mLock);
Haynes Mathew Georgebab7bf42015-10-30 18:02:23 -0700579 Mutex::Autolock _le(mEffectsLock); // isOffloadSupported queries for
580 // non-offloadable effects
Eric Laurentdce54a12014-03-10 12:19:46 -0700581 return mAudioPolicyManager->isOffloadSupported(info);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800582}
583
Eric Laurent6a94d692014-05-20 11:18:06 -0700584status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
585 audio_port_type_t type,
Eric Laurent203b1a12014-04-01 10:34:16 -0700586 unsigned int *num_ports,
Eric Laurent6a94d692014-05-20 11:18:06 -0700587 struct audio_port *ports,
588 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700589{
Eric Laurent6a94d692014-05-20 11:18:06 -0700590 Mutex::Autolock _l(mLock);
591 if (mAudioPolicyManager == NULL) {
592 return NO_INIT;
593 }
594
595 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700596}
597
Eric Laurent6a94d692014-05-20 11:18:06 -0700598status_t AudioPolicyService::getAudioPort(struct audio_port *port)
Eric Laurent203b1a12014-04-01 10:34:16 -0700599{
Eric Laurent6a94d692014-05-20 11:18:06 -0700600 Mutex::Autolock _l(mLock);
601 if (mAudioPolicyManager == NULL) {
602 return NO_INIT;
603 }
604
605 return mAudioPolicyManager->getAudioPort(port);
Eric Laurent203b1a12014-04-01 10:34:16 -0700606}
607
Eric Laurent6a94d692014-05-20 11:18:06 -0700608status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
609 audio_patch_handle_t *handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700610{
Eric Laurent6a94d692014-05-20 11:18:06 -0700611 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700612 if(!modifyAudioRoutingAllowed()) {
613 return PERMISSION_DENIED;
614 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700615 if (mAudioPolicyManager == NULL) {
616 return NO_INIT;
617 }
618 return mAudioPolicyManager->createAudioPatch(patch, handle,
619 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700620}
621
Eric Laurent6a94d692014-05-20 11:18:06 -0700622status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700623{
Eric Laurent6a94d692014-05-20 11:18:06 -0700624 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700625 if(!modifyAudioRoutingAllowed()) {
626 return PERMISSION_DENIED;
627 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700628 if (mAudioPolicyManager == NULL) {
629 return NO_INIT;
630 }
631
632 return mAudioPolicyManager->releaseAudioPatch(handle,
633 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700634}
635
636status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
Eric Laurent6a94d692014-05-20 11:18:06 -0700637 struct audio_patch *patches,
638 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700639{
Eric Laurent6a94d692014-05-20 11:18:06 -0700640 Mutex::Autolock _l(mLock);
641 if (mAudioPolicyManager == NULL) {
642 return NO_INIT;
643 }
644
645 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700646}
647
Eric Laurent6a94d692014-05-20 11:18:06 -0700648status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent203b1a12014-04-01 10:34:16 -0700649{
Eric Laurent6a94d692014-05-20 11:18:06 -0700650 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700651 if(!modifyAudioRoutingAllowed()) {
652 return PERMISSION_DENIED;
653 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700654 if (mAudioPolicyManager == NULL) {
655 return NO_INIT;
656 }
657
658 return mAudioPolicyManager->setAudioPortConfig(config);
Eric Laurent203b1a12014-04-01 10:34:16 -0700659}
Eric Laurent2d388ec2014-03-07 13:25:54 -0800660
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700661status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
662 audio_io_handle_t *ioHandle,
663 audio_devices_t *device)
664{
665 if (mAudioPolicyManager == NULL) {
666 return NO_INIT;
667 }
668
669 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
670}
671
672status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
673{
674 if (mAudioPolicyManager == NULL) {
675 return NO_INIT;
676 }
677
678 return mAudioPolicyManager->releaseSoundTriggerSession(session);
679}
680
Eric Laurentbaac1832014-12-01 17:52:59 -0800681status_t AudioPolicyService::registerPolicyMixes(Vector<AudioMix> mixes, bool registration)
682{
683 Mutex::Autolock _l(mLock);
684 if(!modifyAudioRoutingAllowed()) {
685 return PERMISSION_DENIED;
686 }
687 if (mAudioPolicyManager == NULL) {
688 return NO_INIT;
689 }
690 if (registration) {
691 return mAudioPolicyManager->registerPolicyMixes(mixes);
692 } else {
693 return mAudioPolicyManager->unregisterPolicyMixes(mixes);
694 }
695}
696
Eric Laurent554a2772015-04-10 11:29:24 -0700697status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
698 const audio_attributes_t *attributes,
699 audio_io_handle_t *handle)
700{
701 Mutex::Autolock _l(mLock);
702 if (mAudioPolicyManager == NULL) {
703 return NO_INIT;
704 }
705
Eric Laurentd60560a2015-04-10 11:31:20 -0700706 return mAudioPolicyManager->startAudioSource(source, attributes, handle,
707 IPCThreadState::self()->getCallingUid());
Eric Laurent554a2772015-04-10 11:29:24 -0700708}
709
710status_t AudioPolicyService::stopAudioSource(audio_io_handle_t handle)
711{
712 Mutex::Autolock _l(mLock);
713 if (mAudioPolicyManager == NULL) {
714 return NO_INIT;
715 }
716
717 return mAudioPolicyManager->stopAudioSource(handle);
718}
719
Andy Hung2ddee192015-12-18 17:34:44 -0800720status_t AudioPolicyService::setMasterMono(bool mono)
721{
722 if (mAudioPolicyManager == NULL) {
723 return NO_INIT;
724 }
725 if (!settingsAllowed()) {
726 return PERMISSION_DENIED;
727 }
728 Mutex::Autolock _l(mLock);
729 return mAudioPolicyManager->setMasterMono(mono);
730}
731
732status_t AudioPolicyService::getMasterMono(bool *mono)
733{
734 if (mAudioPolicyManager == NULL) {
735 return NO_INIT;
736 }
737 Mutex::Autolock _l(mLock);
738 return mAudioPolicyManager->getMasterMono(mono);
739}
740
Eric Laurent2d388ec2014-03-07 13:25:54 -0800741}; // namespace android