blob: a45dbb3ef608e29dc1a8347c5f013835823c75bf [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,
31 const char *device_address)
32{
Eric Laurentdce54a12014-03-10 12:19:46 -070033 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -080034 return NO_INIT;
35 }
36 if (!settingsAllowed()) {
37 return PERMISSION_DENIED;
38 }
39 if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
40 return BAD_VALUE;
41 }
42 if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
43 state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
44 return BAD_VALUE;
45 }
46
47 ALOGV("setDeviceConnectionState()");
48 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -070049 return mAudioPolicyManager->setDeviceConnectionState(device,
Eric Laurent2d388ec2014-03-07 13:25:54 -080050 state, device_address);
51}
52
53audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
54 audio_devices_t device,
55 const char *device_address)
56{
Eric Laurentdce54a12014-03-10 12:19:46 -070057 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -080058 return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
59 }
Eric Laurentdce54a12014-03-10 12:19:46 -070060 return mAudioPolicyManager->getDeviceConnectionState(device,
Eric Laurent2d388ec2014-03-07 13:25:54 -080061 device_address);
62}
63
64status_t AudioPolicyService::setPhoneState(audio_mode_t state)
65{
Eric Laurentdce54a12014-03-10 12:19:46 -070066 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -080067 return NO_INIT;
68 }
69 if (!settingsAllowed()) {
70 return PERMISSION_DENIED;
71 }
72 if (uint32_t(state) >= AUDIO_MODE_CNT) {
73 return BAD_VALUE;
74 }
75
76 ALOGV("setPhoneState()");
77
78 // TODO: check if it is more appropriate to do it in platform specific policy manager
79 AudioSystem::setMode(state);
80
81 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -070082 mAudioPolicyManager->setPhoneState(state);
Eric Laurentbb6c9a02014-09-25 14:11:47 -070083 mPhoneState = state;
Eric Laurent2d388ec2014-03-07 13:25:54 -080084 return NO_ERROR;
85}
86
Eric Laurentbb6c9a02014-09-25 14:11:47 -070087audio_mode_t AudioPolicyService::getPhoneState()
88{
89 Mutex::Autolock _l(mLock);
90 return mPhoneState;
91}
92
Eric Laurent2d388ec2014-03-07 13:25:54 -080093status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
94 audio_policy_forced_cfg_t config)
95{
Eric Laurentdce54a12014-03-10 12:19:46 -070096 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -080097 return NO_INIT;
98 }
99 if (!settingsAllowed()) {
100 return PERMISSION_DENIED;
101 }
102 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
103 return BAD_VALUE;
104 }
105 if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
106 return BAD_VALUE;
107 }
108 ALOGV("setForceUse()");
109 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700110 mAudioPolicyManager->setForceUse(usage, config);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800111 return NO_ERROR;
112}
113
114audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
115{
Eric Laurentdce54a12014-03-10 12:19:46 -0700116 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800117 return AUDIO_POLICY_FORCE_NONE;
118 }
119 if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
120 return AUDIO_POLICY_FORCE_NONE;
121 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700122 return mAudioPolicyManager->getForceUse(usage);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800123}
124
125audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
126 uint32_t samplingRate,
127 audio_format_t format,
128 audio_channel_mask_t channelMask,
129 audio_output_flags_t flags,
130 const audio_offload_info_t *offloadInfo)
131{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800132 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700133 return AUDIO_IO_HANDLE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700134 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700135 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700136 return AUDIO_IO_HANDLE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800137 }
138 ALOGV("getOutput()");
139 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700140 return mAudioPolicyManager->getOutput(stream, samplingRate,
Eric Laurent2d388ec2014-03-07 13:25:54 -0800141 format, channelMask, flags, offloadInfo);
142}
143
Eric Laurente83b55d2014-11-14 10:06:21 -0800144status_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr,
145 audio_io_handle_t *output,
146 audio_session_t session,
147 audio_stream_type_t *stream,
148 uint32_t samplingRate,
149 audio_format_t format,
150 audio_channel_mask_t channelMask,
151 audio_output_flags_t flags,
152 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700153{
154 if (mAudioPolicyManager == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800155 return NO_INIT;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700156 }
157 ALOGV("getOutput()");
158 Mutex::Autolock _l(mLock);
Eric Laurente83b55d2014-11-14 10:06:21 -0800159 return mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, samplingRate,
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700160 format, channelMask, flags, offloadInfo);
161}
162
Eric Laurent2d388ec2014-03-07 13:25:54 -0800163status_t AudioPolicyService::startOutput(audio_io_handle_t output,
164 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800165 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800166{
Eric Laurentdea15412014-10-28 15:46:45 -0700167 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
168 return BAD_VALUE;
169 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700170 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800171 return NO_INIT;
172 }
173 ALOGV("startOutput()");
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700174 sp<AudioPolicyEffects>audioPolicyEffects;
175 {
176 Mutex::Autolock _l(mLock);
177 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800178 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700179 if (audioPolicyEffects != 0) {
180 // create audio processors according to stream
181 status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session);
182 if (status != NO_ERROR && status != ALREADY_EXISTS) {
183 ALOGW("Failed to add effects on session %d", session);
184 }
185 }
186 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700187 return mAudioPolicyManager->startOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800188}
189
190status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
191 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800192 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800193{
Eric Laurentdea15412014-10-28 15:46:45 -0700194 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
195 return BAD_VALUE;
196 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700197 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800198 return NO_INIT;
199 }
200 ALOGV("stopOutput()");
201 mOutputCommandThread->stopOutputCommand(output, stream, session);
202 return NO_ERROR;
203}
204
205status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
206 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800207 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800208{
209 ALOGV("doStopOutput from tid %d", gettid());
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700210 sp<AudioPolicyEffects>audioPolicyEffects;
211 {
212 Mutex::Autolock _l(mLock);
213 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800214 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700215 if (audioPolicyEffects != 0) {
216 // release audio processors from the stream
217 status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
218 if (status != NO_ERROR && status != ALREADY_EXISTS) {
219 ALOGW("Failed to release effects on session %d", session);
220 }
221 }
222 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700223 return mAudioPolicyManager->stopOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800224}
225
Eric Laurente83b55d2014-11-14 10:06:21 -0800226void AudioPolicyService::releaseOutput(audio_io_handle_t output,
227 audio_stream_type_t stream,
228 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800229{
Eric Laurentdce54a12014-03-10 12:19:46 -0700230 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800231 return;
232 }
233 ALOGV("releaseOutput()");
Eric Laurente83b55d2014-11-14 10:06:21 -0800234 mOutputCommandThread->releaseOutputCommand(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800235}
236
Eric Laurente83b55d2014-11-14 10:06:21 -0800237void AudioPolicyService::doReleaseOutput(audio_io_handle_t output,
238 audio_stream_type_t stream,
239 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800240{
241 ALOGV("doReleaseOutput from tid %d", gettid());
242 Mutex::Autolock _l(mLock);
Eric Laurente83b55d2014-11-14 10:06:21 -0800243 mAudioPolicyManager->releaseOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800244}
245
Eric Laurentcaf7f482014-11-25 17:50:47 -0800246status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr,
247 audio_io_handle_t *input,
248 audio_session_t session,
249 uint32_t samplingRate,
250 audio_format_t format,
251 audio_channel_mask_t channelMask,
252 audio_input_flags_t flags)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800253{
Eric Laurentdce54a12014-03-10 12:19:46 -0700254 if (mAudioPolicyManager == NULL) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800255 return NO_INIT;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800256 }
257 // already checked by client, but double-check in case the client wrapper is bypassed
Eric Laurentcaf7f482014-11-25 17:50:47 -0800258 if (attr->source >= AUDIO_SOURCE_CNT && attr->source != AUDIO_SOURCE_HOTWORD &&
259 attr->source != AUDIO_SOURCE_FM_TUNER) {
260 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800261 }
262
Eric Laurentcaf7f482014-11-25 17:50:47 -0800263 if (((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) ||
264 ((attr->source == AUDIO_SOURCE_FM_TUNER) && !captureFmTunerAllowed())) {
265 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800266 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700267 sp<AudioPolicyEffects>audioPolicyEffects;
Eric Laurentcaf7f482014-11-25 17:50:47 -0800268 status_t status;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800269 AudioPolicyInterface::input_type_t inputType;
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700270 {
271 Mutex::Autolock _l(mLock);
272 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurentcaf7f482014-11-25 17:50:47 -0800273 status = mAudioPolicyManager->getInputForAttr(attr, input, session,
274 samplingRate, format, channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800275 flags, &inputType);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700276 audioPolicyEffects = mAudioPolicyEffects;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800277
278 if (status == NO_ERROR) {
279 // enforce permission (if any) required for each type of input
280 switch (inputType) {
281 case AudioPolicyInterface::API_INPUT_LEGACY:
282 break;
283 case AudioPolicyInterface::API_INPUT_MIX_CAPTURE:
284 if (!captureAudioOutputAllowed()) {
285 ALOGE("getInputForAttr() permission denied: capture not allowed");
286 status = PERMISSION_DENIED;
287 }
288 break;
289 case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE:
290 if (!modifyAudioRoutingAllowed()) {
291 ALOGE("getInputForAttr() permission denied: modify audio routing not allowed");
292 status = PERMISSION_DENIED;
293 }
294 break;
295 case AudioPolicyInterface::API_INPUT_INVALID:
296 default:
297 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d",
298 (int)inputType);
299 }
300 }
301
302 if (status != NO_ERROR) {
303 if (status == PERMISSION_DENIED) {
304 mAudioPolicyManager->releaseInput(*input, session);
305 }
306 return status;
307 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700308 }
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800309
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700310 if (audioPolicyEffects != 0) {
311 // create audio pre processors according to input source
Eric Laurentcaf7f482014-11-25 17:50:47 -0800312 status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700313 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800314 ALOGW("Failed to add effects on input %d", *input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700315 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800316 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800317 return NO_ERROR;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800318}
319
Eric Laurent4dc68062014-07-28 17:26:49 -0700320status_t AudioPolicyService::startInput(audio_io_handle_t input,
321 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800322{
Eric Laurentdce54a12014-03-10 12:19:46 -0700323 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800324 return NO_INIT;
325 }
326 Mutex::Autolock _l(mLock);
327
Eric Laurent4dc68062014-07-28 17:26:49 -0700328 return mAudioPolicyManager->startInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800329}
330
Eric Laurent4dc68062014-07-28 17:26:49 -0700331status_t AudioPolicyService::stopInput(audio_io_handle_t input,
332 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800333{
Eric Laurentdce54a12014-03-10 12:19:46 -0700334 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800335 return NO_INIT;
336 }
337 Mutex::Autolock _l(mLock);
338
Eric Laurent4dc68062014-07-28 17:26:49 -0700339 return mAudioPolicyManager->stopInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800340}
341
Eric Laurent4dc68062014-07-28 17:26:49 -0700342void AudioPolicyService::releaseInput(audio_io_handle_t input,
343 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800344{
Eric Laurentdce54a12014-03-10 12:19:46 -0700345 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800346 return;
347 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700348 sp<AudioPolicyEffects>audioPolicyEffects;
349 {
350 Mutex::Autolock _l(mLock);
351 mAudioPolicyManager->releaseInput(input, session);
352 audioPolicyEffects = mAudioPolicyEffects;
353 }
354 if (audioPolicyEffects != 0) {
355 // release audio processors from the input
356 status_t status = audioPolicyEffects->releaseInputEffects(input);
357 if(status != NO_ERROR) {
358 ALOGW("Failed to release effects on input %d", input);
359 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800360 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800361}
362
363status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
364 int indexMin,
365 int indexMax)
366{
Eric Laurentdce54a12014-03-10 12:19:46 -0700367 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800368 return NO_INIT;
369 }
370 if (!settingsAllowed()) {
371 return PERMISSION_DENIED;
372 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800373 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800374 return BAD_VALUE;
375 }
376 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700377 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800378 return NO_ERROR;
379}
380
381status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
382 int index,
383 audio_devices_t device)
384{
Eric Laurentdce54a12014-03-10 12:19:46 -0700385 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800386 return NO_INIT;
387 }
388 if (!settingsAllowed()) {
389 return PERMISSION_DENIED;
390 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800391 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800392 return BAD_VALUE;
393 }
394 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700395 return mAudioPolicyManager->setStreamVolumeIndex(stream,
396 index,
397 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800398}
399
400status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
401 int *index,
402 audio_devices_t device)
403{
Eric Laurentdce54a12014-03-10 12:19:46 -0700404 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800405 return NO_INIT;
406 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800407 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800408 return BAD_VALUE;
409 }
410 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700411 return mAudioPolicyManager->getStreamVolumeIndex(stream,
412 index,
413 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800414}
415
416uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
417{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800418 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700419 return 0;
Eric Laurentdea15412014-10-28 15:46:45 -0700420 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700421 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800422 return 0;
423 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700424 return mAudioPolicyManager->getStrategyForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800425}
426
427//audio policy: use audio_device_t appropriately
428
429audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
430{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800431 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700432 return AUDIO_DEVICE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700433 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700434 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700435 return AUDIO_DEVICE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800436 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700437 return mAudioPolicyManager->getDevicesForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800438}
439
440audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
441{
442 // FIXME change return type to status_t, and return NO_INIT here
Eric Laurentdce54a12014-03-10 12:19:46 -0700443 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800444 return 0;
445 }
446 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700447 return mAudioPolicyManager->getOutputForEffect(desc);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800448}
449
450status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
451 audio_io_handle_t io,
452 uint32_t strategy,
453 int session,
454 int id)
455{
Eric Laurentdce54a12014-03-10 12:19:46 -0700456 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800457 return NO_INIT;
458 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700459 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800460}
461
462status_t AudioPolicyService::unregisterEffect(int id)
463{
Eric Laurentdce54a12014-03-10 12:19:46 -0700464 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800465 return NO_INIT;
466 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700467 return mAudioPolicyManager->unregisterEffect(id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800468}
469
470status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
471{
Eric Laurentdce54a12014-03-10 12:19:46 -0700472 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800473 return NO_INIT;
474 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700475 return mAudioPolicyManager->setEffectEnabled(id, enabled);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800476}
477
478bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
479{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800480 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700481 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700482 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700483 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700484 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800485 }
486 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700487 return mAudioPolicyManager->isStreamActive(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800488}
489
490bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
491{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800492 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700493 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700494 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700495 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700496 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800497 }
498 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700499 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800500}
501
502bool AudioPolicyService::isSourceActive(audio_source_t source) const
503{
Eric Laurentdce54a12014-03-10 12:19:46 -0700504 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800505 return false;
506 }
507 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700508 return mAudioPolicyManager->isSourceActive(source);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800509}
510
511status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
512 effect_descriptor_t *descriptors,
513 uint32_t *count)
514{
Eric Laurentdce54a12014-03-10 12:19:46 -0700515 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800516 *count = 0;
517 return NO_INIT;
518 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700519 sp<AudioPolicyEffects>audioPolicyEffects;
520 {
521 Mutex::Autolock _l(mLock);
522 audioPolicyEffects = mAudioPolicyEffects;
523 }
524 if (audioPolicyEffects == 0) {
525 *count = 0;
526 return NO_INIT;
527 }
528 return audioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800529}
530
531bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
532{
Eric Laurentdce54a12014-03-10 12:19:46 -0700533 if (mAudioPolicyManager == NULL) {
534 ALOGV("mAudioPolicyManager == NULL");
Eric Laurent2d388ec2014-03-07 13:25:54 -0800535 return false;
536 }
537
Eric Laurentdce54a12014-03-10 12:19:46 -0700538 return mAudioPolicyManager->isOffloadSupported(info);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800539}
540
Eric Laurent6a94d692014-05-20 11:18:06 -0700541status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
542 audio_port_type_t type,
Eric Laurent203b1a12014-04-01 10:34:16 -0700543 unsigned int *num_ports,
Eric Laurent6a94d692014-05-20 11:18:06 -0700544 struct audio_port *ports,
545 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700546{
Eric Laurent6a94d692014-05-20 11:18:06 -0700547 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700548 if(!modifyAudioRoutingAllowed()) {
549 return PERMISSION_DENIED;
550 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700551 if (mAudioPolicyManager == NULL) {
552 return NO_INIT;
553 }
554
555 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700556}
557
Eric Laurent6a94d692014-05-20 11:18:06 -0700558status_t AudioPolicyService::getAudioPort(struct audio_port *port)
Eric Laurent203b1a12014-04-01 10:34:16 -0700559{
Eric Laurent6a94d692014-05-20 11:18:06 -0700560 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700561 if(!modifyAudioRoutingAllowed()) {
562 return PERMISSION_DENIED;
563 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700564 if (mAudioPolicyManager == NULL) {
565 return NO_INIT;
566 }
567
568 return mAudioPolicyManager->getAudioPort(port);
Eric Laurent203b1a12014-04-01 10:34:16 -0700569}
570
Eric Laurent6a94d692014-05-20 11:18:06 -0700571status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
572 audio_patch_handle_t *handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700573{
Eric Laurent6a94d692014-05-20 11:18:06 -0700574 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700575 if(!modifyAudioRoutingAllowed()) {
576 return PERMISSION_DENIED;
577 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700578 if (mAudioPolicyManager == NULL) {
579 return NO_INIT;
580 }
581 return mAudioPolicyManager->createAudioPatch(patch, handle,
582 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700583}
584
Eric Laurent6a94d692014-05-20 11:18:06 -0700585status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700586{
Eric Laurent6a94d692014-05-20 11:18:06 -0700587 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700588 if(!modifyAudioRoutingAllowed()) {
589 return PERMISSION_DENIED;
590 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700591 if (mAudioPolicyManager == NULL) {
592 return NO_INIT;
593 }
594
595 return mAudioPolicyManager->releaseAudioPatch(handle,
596 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700597}
598
599status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
Eric Laurent6a94d692014-05-20 11:18:06 -0700600 struct audio_patch *patches,
601 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700602{
Eric Laurent6a94d692014-05-20 11:18:06 -0700603 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700604 if(!modifyAudioRoutingAllowed()) {
605 return PERMISSION_DENIED;
606 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700607 if (mAudioPolicyManager == NULL) {
608 return NO_INIT;
609 }
610
611 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700612}
613
Eric Laurent6a94d692014-05-20 11:18:06 -0700614status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent203b1a12014-04-01 10:34:16 -0700615{
Eric Laurent6a94d692014-05-20 11:18:06 -0700616 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700617 if(!modifyAudioRoutingAllowed()) {
618 return PERMISSION_DENIED;
619 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700620 if (mAudioPolicyManager == NULL) {
621 return NO_INIT;
622 }
623
624 return mAudioPolicyManager->setAudioPortConfig(config);
Eric Laurent203b1a12014-04-01 10:34:16 -0700625}
Eric Laurent2d388ec2014-03-07 13:25:54 -0800626
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700627status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
628 audio_io_handle_t *ioHandle,
629 audio_devices_t *device)
630{
631 if (mAudioPolicyManager == NULL) {
632 return NO_INIT;
633 }
634
635 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
636}
637
638status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
639{
640 if (mAudioPolicyManager == NULL) {
641 return NO_INIT;
642 }
643
644 return mAudioPolicyManager->releaseSoundTriggerSession(session);
645}
646
Eric Laurentbaac1832014-12-01 17:52:59 -0800647status_t AudioPolicyService::registerPolicyMixes(Vector<AudioMix> mixes, bool registration)
648{
649 Mutex::Autolock _l(mLock);
650 if(!modifyAudioRoutingAllowed()) {
651 return PERMISSION_DENIED;
652 }
653 if (mAudioPolicyManager == NULL) {
654 return NO_INIT;
655 }
656 if (registration) {
657 return mAudioPolicyManager->registerPolicyMixes(mixes);
658 } else {
659 return mAudioPolicyManager->unregisterPolicyMixes(mixes);
660 }
661}
662
Eric Laurent2d388ec2014-03-07 13:25:54 -0800663}; // namespace android