blob: 58ecb111446f92c86ec72b4b18a22b163eca54cd [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
79 // TODO: check if it is more appropriate to do it in platform specific policy manager
80 AudioSystem::setMode(state);
81
82 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -070083 mAudioPolicyManager->setPhoneState(state);
Eric Laurentbb6c9a02014-09-25 14:11:47 -070084 mPhoneState = state;
Eric Laurent2d388ec2014-03-07 13:25:54 -080085 return NO_ERROR;
86}
87
Eric Laurentbb6c9a02014-09-25 14:11:47 -070088audio_mode_t AudioPolicyService::getPhoneState()
89{
90 Mutex::Autolock _l(mLock);
91 return mPhoneState;
92}
93
Eric Laurent2d388ec2014-03-07 13:25:54 -080094status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
95 audio_policy_forced_cfg_t config)
96{
Eric Laurentdce54a12014-03-10 12:19:46 -070097 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -080098 return NO_INIT;
99 }
100 if (!settingsAllowed()) {
101 return PERMISSION_DENIED;
102 }
103 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
104 return BAD_VALUE;
105 }
106 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
107 return BAD_VALUE;
108 }
109 ALOGV("setForceUse()");
110 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700111 mAudioPolicyManager->setForceUse(usage, config);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800112 return NO_ERROR;
113}
114
115audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
116{
Eric Laurentdce54a12014-03-10 12:19:46 -0700117 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800118 return AUDIO_POLICY_FORCE_NONE;
119 }
120 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
121 return AUDIO_POLICY_FORCE_NONE;
122 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700123 return mAudioPolicyManager->getForceUse(usage);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800124}
125
126audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
127 uint32_t samplingRate,
128 audio_format_t format,
129 audio_channel_mask_t channelMask,
130 audio_output_flags_t flags,
131 const audio_offload_info_t *offloadInfo)
132{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800133 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700134 return AUDIO_IO_HANDLE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700135 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700136 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700137 return AUDIO_IO_HANDLE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800138 }
139 ALOGV("getOutput()");
140 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700141 return mAudioPolicyManager->getOutput(stream, samplingRate,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800142 format, channelMask, flags, offloadInfo);
143}
144
Eric Laurente83b55d2014-11-14 10:06:21 -0800145status_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr,
146 audio_io_handle_t *output,
147 audio_session_t session,
148 audio_stream_type_t *stream,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700149 uid_t uid,
Eric Laurente83b55d2014-11-14 10:06:21 -0800150 uint32_t samplingRate,
151 audio_format_t format,
152 audio_channel_mask_t channelMask,
153 audio_output_flags_t flags,
Paul McLean466dc8e2015-04-17 13:15:36 -0600154 audio_port_handle_t selectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800155 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700156{
157 if (mAudioPolicyManager == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800158 return NO_INIT;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700159 }
160 ALOGV("getOutput()");
161 Mutex::Autolock _l(mLock);
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700162
163 // if the caller is us, trust the specified uid
164 if (IPCThreadState::self()->getCallingPid() != getpid_cached || uid == (uid_t)-1) {
165 uid_t newclientUid = IPCThreadState::self()->getCallingUid();
166 if (uid != (uid_t)-1 && uid != newclientUid) {
167 ALOGW("%s uid %d tried to pass itself off as %d", __FUNCTION__, newclientUid, uid);
168 }
169 uid = newclientUid;
170 }
171 return mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid, samplingRate,
Paul McLean466dc8e2015-04-17 13:15:36 -0600172 format, channelMask, flags, selectedDeviceId, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700173}
174
Eric Laurent2d388ec2014-03-07 13:25:54 -0800175status_t AudioPolicyService::startOutput(audio_io_handle_t output,
176 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800177 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800178{
Eric Laurentdea15412014-10-28 15:46:45 -0700179 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
180 return BAD_VALUE;
181 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700182 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800183 return NO_INIT;
184 }
185 ALOGV("startOutput()");
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700186 sp<AudioPolicyEffects>audioPolicyEffects;
187 {
188 Mutex::Autolock _l(mLock);
189 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800190 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700191 if (audioPolicyEffects != 0) {
192 // create audio processors according to stream
193 status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session);
194 if (status != NO_ERROR && status != ALREADY_EXISTS) {
195 ALOGW("Failed to add effects on session %d", session);
196 }
197 }
198 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700199 return mAudioPolicyManager->startOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800200}
201
202status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
203 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800204 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800205{
Eric Laurentdea15412014-10-28 15:46:45 -0700206 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
207 return BAD_VALUE;
208 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700209 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800210 return NO_INIT;
211 }
212 ALOGV("stopOutput()");
213 mOutputCommandThread->stopOutputCommand(output, stream, session);
214 return NO_ERROR;
215}
216
217status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
218 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800219 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800220{
221 ALOGV("doStopOutput from tid %d", gettid());
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700222 sp<AudioPolicyEffects>audioPolicyEffects;
223 {
224 Mutex::Autolock _l(mLock);
225 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800226 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700227 if (audioPolicyEffects != 0) {
228 // release audio processors from the stream
229 status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
230 if (status != NO_ERROR && status != ALREADY_EXISTS) {
231 ALOGW("Failed to release effects on session %d", session);
232 }
233 }
234 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700235 return mAudioPolicyManager->stopOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800236}
237
Eric Laurente83b55d2014-11-14 10:06:21 -0800238void AudioPolicyService::releaseOutput(audio_io_handle_t output,
239 audio_stream_type_t stream,
240 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800241{
Eric Laurentdce54a12014-03-10 12:19:46 -0700242 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800243 return;
244 }
245 ALOGV("releaseOutput()");
Eric Laurente83b55d2014-11-14 10:06:21 -0800246 mOutputCommandThread->releaseOutputCommand(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800247}
248
Eric Laurente83b55d2014-11-14 10:06:21 -0800249void AudioPolicyService::doReleaseOutput(audio_io_handle_t output,
250 audio_stream_type_t stream,
251 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800252{
253 ALOGV("doReleaseOutput from tid %d", gettid());
254 Mutex::Autolock _l(mLock);
Eric Laurente83b55d2014-11-14 10:06:21 -0800255 mAudioPolicyManager->releaseOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800256}
257
Eric Laurentcaf7f482014-11-25 17:50:47 -0800258status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr,
259 audio_io_handle_t *input,
260 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700261 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800262 uint32_t samplingRate,
263 audio_format_t format,
264 audio_channel_mask_t channelMask,
Paul McLean466dc8e2015-04-17 13:15:36 -0600265 audio_input_flags_t flags,
266 audio_port_handle_t selectedDeviceId)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800267{
Eric Laurentdce54a12014-03-10 12:19:46 -0700268 if (mAudioPolicyManager == NULL) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800269 return NO_INIT;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800270 }
271 // already checked by client, but double-check in case the client wrapper is bypassed
Eric Laurentcaf7f482014-11-25 17:50:47 -0800272 if (attr->source >= AUDIO_SOURCE_CNT && attr->source != AUDIO_SOURCE_HOTWORD &&
273 attr->source != AUDIO_SOURCE_FM_TUNER) {
274 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800275 }
276
Eric Laurentab300c82015-04-13 13:47:33 -0700277 if ((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800278 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800279 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700280 sp<AudioPolicyEffects>audioPolicyEffects;
Eric Laurentcaf7f482014-11-25 17:50:47 -0800281 status_t status;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800282 AudioPolicyInterface::input_type_t inputType;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700283 // if the caller is us, trust the specified uid
284 if (IPCThreadState::self()->getCallingPid() != getpid_cached || uid == (uid_t)-1) {
285 uid_t newclientUid = IPCThreadState::self()->getCallingUid();
286 if (uid != (uid_t)-1 && uid != newclientUid) {
287 ALOGW("%s uid %d tried to pass itself off as %d", __FUNCTION__, newclientUid, uid);
288 }
289 uid = newclientUid;
290 }
291
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700292 {
293 Mutex::Autolock _l(mLock);
294 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700295 status = mAudioPolicyManager->getInputForAttr(attr, input, session, uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800296 samplingRate, format, channelMask,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700297 flags, selectedDeviceId,
298 &inputType);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700299 audioPolicyEffects = mAudioPolicyEffects;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800300
301 if (status == NO_ERROR) {
302 // enforce permission (if any) required for each type of input
303 switch (inputType) {
304 case AudioPolicyInterface::API_INPUT_LEGACY:
305 break;
Eric Laurent82db2692015-08-07 13:59:42 -0700306 case AudioPolicyInterface::API_INPUT_TELEPHONY_RX:
307 // FIXME: use the same permission as for remote submix for now.
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800308 case AudioPolicyInterface::API_INPUT_MIX_CAPTURE:
309 if (!captureAudioOutputAllowed()) {
310 ALOGE("getInputForAttr() permission denied: capture not allowed");
311 status = PERMISSION_DENIED;
312 }
313 break;
314 case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE:
315 if (!modifyAudioRoutingAllowed()) {
316 ALOGE("getInputForAttr() permission denied: modify audio routing not allowed");
317 status = PERMISSION_DENIED;
318 }
319 break;
320 case AudioPolicyInterface::API_INPUT_INVALID:
321 default:
322 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d",
323 (int)inputType);
324 }
325 }
326
327 if (status != NO_ERROR) {
328 if (status == PERMISSION_DENIED) {
329 mAudioPolicyManager->releaseInput(*input, session);
330 }
331 return status;
332 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700333 }
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800334
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700335 if (audioPolicyEffects != 0) {
336 // create audio pre processors according to input source
Eric Laurentcaf7f482014-11-25 17:50:47 -0800337 status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700338 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800339 ALOGW("Failed to add effects on input %d", *input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700340 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800341 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800342 return NO_ERROR;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800343}
344
Eric Laurent4dc68062014-07-28 17:26:49 -0700345status_t AudioPolicyService::startInput(audio_io_handle_t input,
346 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800347{
Eric Laurentdce54a12014-03-10 12:19:46 -0700348 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800349 return NO_INIT;
350 }
351 Mutex::Autolock _l(mLock);
352
Eric Laurent4dc68062014-07-28 17:26:49 -0700353 return mAudioPolicyManager->startInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800354}
355
Eric Laurent4dc68062014-07-28 17:26:49 -0700356status_t AudioPolicyService::stopInput(audio_io_handle_t input,
357 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800358{
Eric Laurentdce54a12014-03-10 12:19:46 -0700359 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800360 return NO_INIT;
361 }
362 Mutex::Autolock _l(mLock);
363
Eric Laurent4dc68062014-07-28 17:26:49 -0700364 return mAudioPolicyManager->stopInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800365}
366
Eric Laurent4dc68062014-07-28 17:26:49 -0700367void AudioPolicyService::releaseInput(audio_io_handle_t input,
368 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800369{
Eric Laurentdce54a12014-03-10 12:19:46 -0700370 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800371 return;
372 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700373 sp<AudioPolicyEffects>audioPolicyEffects;
374 {
375 Mutex::Autolock _l(mLock);
376 mAudioPolicyManager->releaseInput(input, session);
377 audioPolicyEffects = mAudioPolicyEffects;
378 }
379 if (audioPolicyEffects != 0) {
380 // release audio processors from the input
381 status_t status = audioPolicyEffects->releaseInputEffects(input);
382 if(status != NO_ERROR) {
383 ALOGW("Failed to release effects on input %d", input);
384 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800385 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800386}
387
388status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
389 int indexMin,
390 int indexMax)
391{
Eric Laurentdce54a12014-03-10 12:19:46 -0700392 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800393 return NO_INIT;
394 }
395 if (!settingsAllowed()) {
396 return PERMISSION_DENIED;
397 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800398 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800399 return BAD_VALUE;
400 }
401 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700402 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800403 return NO_ERROR;
404}
405
406status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
407 int index,
408 audio_devices_t device)
409{
Eric Laurentdce54a12014-03-10 12:19:46 -0700410 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800411 return NO_INIT;
412 }
413 if (!settingsAllowed()) {
414 return PERMISSION_DENIED;
415 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800416 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800417 return BAD_VALUE;
418 }
419 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700420 return mAudioPolicyManager->setStreamVolumeIndex(stream,
421 index,
422 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800423}
424
425status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
426 int *index,
427 audio_devices_t device)
428{
Eric Laurentdce54a12014-03-10 12:19:46 -0700429 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800430 return NO_INIT;
431 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800432 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800433 return BAD_VALUE;
434 }
435 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700436 return mAudioPolicyManager->getStreamVolumeIndex(stream,
437 index,
438 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800439}
440
441uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
442{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800443 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700444 return 0;
Eric Laurentdea15412014-10-28 15:46:45 -0700445 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700446 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800447 return 0;
448 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700449 return mAudioPolicyManager->getStrategyForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800450}
451
452//audio policy: use audio_device_t appropriately
453
454audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
455{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800456 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700457 return AUDIO_DEVICE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700458 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700459 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700460 return AUDIO_DEVICE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800461 }
Haynes Mathew Georgedfb9f3b2015-10-26 18:22:13 -0700462 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700463 return mAudioPolicyManager->getDevicesForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800464}
465
466audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
467{
468 // FIXME change return type to status_t, and return NO_INIT here
Eric Laurentdce54a12014-03-10 12:19:46 -0700469 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800470 return 0;
471 }
472 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700473 return mAudioPolicyManager->getOutputForEffect(desc);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800474}
475
476status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
477 audio_io_handle_t io,
478 uint32_t strategy,
479 int session,
480 int id)
481{
Eric Laurentdce54a12014-03-10 12:19:46 -0700482 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800483 return NO_INIT;
484 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700485 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800486}
487
488status_t AudioPolicyService::unregisterEffect(int id)
489{
Eric Laurentdce54a12014-03-10 12:19:46 -0700490 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800491 return NO_INIT;
492 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700493 return mAudioPolicyManager->unregisterEffect(id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800494}
495
496status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
497{
Eric Laurentdce54a12014-03-10 12:19:46 -0700498 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800499 return NO_INIT;
500 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700501 return mAudioPolicyManager->setEffectEnabled(id, enabled);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800502}
503
504bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
505{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800506 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700507 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700508 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700509 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700510 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800511 }
512 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700513 return mAudioPolicyManager->isStreamActive(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800514}
515
516bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
517{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800518 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700519 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700520 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700521 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700522 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800523 }
524 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700525 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800526}
527
528bool AudioPolicyService::isSourceActive(audio_source_t source) const
529{
Eric Laurentdce54a12014-03-10 12:19:46 -0700530 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800531 return false;
532 }
533 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700534 return mAudioPolicyManager->isSourceActive(source);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800535}
536
537status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
538 effect_descriptor_t *descriptors,
539 uint32_t *count)
540{
Eric Laurentdce54a12014-03-10 12:19:46 -0700541 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800542 *count = 0;
543 return NO_INIT;
544 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700545 sp<AudioPolicyEffects>audioPolicyEffects;
546 {
547 Mutex::Autolock _l(mLock);
548 audioPolicyEffects = mAudioPolicyEffects;
549 }
550 if (audioPolicyEffects == 0) {
551 *count = 0;
552 return NO_INIT;
553 }
554 return audioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800555}
556
557bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
558{
Eric Laurentdce54a12014-03-10 12:19:46 -0700559 if (mAudioPolicyManager == NULL) {
560 ALOGV("mAudioPolicyManager == NULL");
Eric Laurent2d388ec2014-03-07 13:25:54 -0800561 return false;
562 }
563
Eric Laurentdce54a12014-03-10 12:19:46 -0700564 return mAudioPolicyManager->isOffloadSupported(info);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800565}
566
Eric Laurent6a94d692014-05-20 11:18:06 -0700567status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
568 audio_port_type_t type,
Eric Laurent203b1a12014-04-01 10:34:16 -0700569 unsigned int *num_ports,
Eric Laurent6a94d692014-05-20 11:18:06 -0700570 struct audio_port *ports,
571 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700572{
Eric Laurent6a94d692014-05-20 11:18:06 -0700573 Mutex::Autolock _l(mLock);
574 if (mAudioPolicyManager == NULL) {
575 return NO_INIT;
576 }
577
578 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700579}
580
Eric Laurent6a94d692014-05-20 11:18:06 -0700581status_t AudioPolicyService::getAudioPort(struct audio_port *port)
Eric Laurent203b1a12014-04-01 10:34:16 -0700582{
Eric Laurent6a94d692014-05-20 11:18:06 -0700583 Mutex::Autolock _l(mLock);
584 if (mAudioPolicyManager == NULL) {
585 return NO_INIT;
586 }
587
588 return mAudioPolicyManager->getAudioPort(port);
Eric Laurent203b1a12014-04-01 10:34:16 -0700589}
590
Eric Laurent6a94d692014-05-20 11:18:06 -0700591status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
592 audio_patch_handle_t *handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700593{
Eric Laurent6a94d692014-05-20 11:18:06 -0700594 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700595 if(!modifyAudioRoutingAllowed()) {
596 return PERMISSION_DENIED;
597 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700598 if (mAudioPolicyManager == NULL) {
599 return NO_INIT;
600 }
601 return mAudioPolicyManager->createAudioPatch(patch, handle,
602 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700603}
604
Eric Laurent6a94d692014-05-20 11:18:06 -0700605status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700606{
Eric Laurent6a94d692014-05-20 11:18:06 -0700607 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700608 if(!modifyAudioRoutingAllowed()) {
609 return PERMISSION_DENIED;
610 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700611 if (mAudioPolicyManager == NULL) {
612 return NO_INIT;
613 }
614
615 return mAudioPolicyManager->releaseAudioPatch(handle,
616 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700617}
618
619status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
Eric Laurent6a94d692014-05-20 11:18:06 -0700620 struct audio_patch *patches,
621 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700622{
Eric Laurent6a94d692014-05-20 11:18:06 -0700623 Mutex::Autolock _l(mLock);
624 if (mAudioPolicyManager == NULL) {
625 return NO_INIT;
626 }
627
628 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700629}
630
Eric Laurent6a94d692014-05-20 11:18:06 -0700631status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent203b1a12014-04-01 10:34:16 -0700632{
Eric Laurent6a94d692014-05-20 11:18:06 -0700633 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700634 if(!modifyAudioRoutingAllowed()) {
635 return PERMISSION_DENIED;
636 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700637 if (mAudioPolicyManager == NULL) {
638 return NO_INIT;
639 }
640
641 return mAudioPolicyManager->setAudioPortConfig(config);
Eric Laurent203b1a12014-04-01 10:34:16 -0700642}
Eric Laurent2d388ec2014-03-07 13:25:54 -0800643
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700644status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
645 audio_io_handle_t *ioHandle,
646 audio_devices_t *device)
647{
648 if (mAudioPolicyManager == NULL) {
649 return NO_INIT;
650 }
651
652 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
653}
654
655status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
656{
657 if (mAudioPolicyManager == NULL) {
658 return NO_INIT;
659 }
660
661 return mAudioPolicyManager->releaseSoundTriggerSession(session);
662}
663
Eric Laurentbaac1832014-12-01 17:52:59 -0800664status_t AudioPolicyService::registerPolicyMixes(Vector<AudioMix> mixes, bool registration)
665{
666 Mutex::Autolock _l(mLock);
667 if(!modifyAudioRoutingAllowed()) {
668 return PERMISSION_DENIED;
669 }
670 if (mAudioPolicyManager == NULL) {
671 return NO_INIT;
672 }
673 if (registration) {
674 return mAudioPolicyManager->registerPolicyMixes(mixes);
675 } else {
676 return mAudioPolicyManager->unregisterPolicyMixes(mixes);
677 }
678}
679
Eric Laurent554a2772015-04-10 11:29:24 -0700680status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
681 const audio_attributes_t *attributes,
682 audio_io_handle_t *handle)
683{
684 Mutex::Autolock _l(mLock);
685 if (mAudioPolicyManager == NULL) {
686 return NO_INIT;
687 }
688
689 return mAudioPolicyManager->startAudioSource(source, attributes, handle);
690}
691
692status_t AudioPolicyService::stopAudioSource(audio_io_handle_t handle)
693{
694 Mutex::Autolock _l(mLock);
695 if (mAudioPolicyManager == NULL) {
696 return NO_INIT;
697 }
698
699 return mAudioPolicyManager->stopAudioSource(handle);
700}
701
Eric Laurent2d388ec2014-03-07 13:25:54 -0800702}; // namespace android