blob: 5f501a5bc936d2435331c117983f842a3c38bfb4 [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,
149 uint32_t samplingRate,
150 audio_format_t format,
151 audio_channel_mask_t channelMask,
152 audio_output_flags_t flags,
Paul McLeanaa981192015-03-21 09:55:15 -0700153 int mSelectedDeviceId,
Eric Laurente83b55d2014-11-14 10:06:21 -0800154 const audio_offload_info_t *offloadInfo)
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700155{
156 if (mAudioPolicyManager == NULL) {
Eric Laurente83b55d2014-11-14 10:06:21 -0800157 return NO_INIT;
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700158 }
159 ALOGV("getOutput()");
160 Mutex::Autolock _l(mLock);
Eric Laurente83b55d2014-11-14 10:06:21 -0800161 return mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, samplingRate,
Paul McLeanaa981192015-03-21 09:55:15 -0700162 format, channelMask, flags, mSelectedDeviceId, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700163}
164
Eric Laurent2d388ec2014-03-07 13:25:54 -0800165status_t AudioPolicyService::startOutput(audio_io_handle_t output,
166 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800167 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800168{
Eric Laurentdea15412014-10-28 15:46:45 -0700169 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
170 return BAD_VALUE;
171 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700172 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800173 return NO_INIT;
174 }
175 ALOGV("startOutput()");
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700176 sp<AudioPolicyEffects>audioPolicyEffects;
177 {
178 Mutex::Autolock _l(mLock);
179 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800180 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700181 if (audioPolicyEffects != 0) {
182 // create audio processors according to stream
183 status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session);
184 if (status != NO_ERROR && status != ALREADY_EXISTS) {
185 ALOGW("Failed to add effects on session %d", session);
186 }
187 }
188 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700189 return mAudioPolicyManager->startOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800190}
191
192status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
193 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800194 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800195{
Eric Laurentdea15412014-10-28 15:46:45 -0700196 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
197 return BAD_VALUE;
198 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700199 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800200 return NO_INIT;
201 }
202 ALOGV("stopOutput()");
203 mOutputCommandThread->stopOutputCommand(output, stream, session);
204 return NO_ERROR;
205}
206
207status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
208 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800209 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800210{
211 ALOGV("doStopOutput from tid %d", gettid());
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700212 sp<AudioPolicyEffects>audioPolicyEffects;
213 {
214 Mutex::Autolock _l(mLock);
215 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800216 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700217 if (audioPolicyEffects != 0) {
218 // release audio processors from the stream
219 status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
220 if (status != NO_ERROR && status != ALREADY_EXISTS) {
221 ALOGW("Failed to release effects on session %d", session);
222 }
223 }
224 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700225 return mAudioPolicyManager->stopOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800226}
227
Eric Laurente83b55d2014-11-14 10:06:21 -0800228void AudioPolicyService::releaseOutput(audio_io_handle_t output,
229 audio_stream_type_t stream,
230 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800231{
Eric Laurentdce54a12014-03-10 12:19:46 -0700232 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800233 return;
234 }
235 ALOGV("releaseOutput()");
Eric Laurente83b55d2014-11-14 10:06:21 -0800236 mOutputCommandThread->releaseOutputCommand(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800237}
238
Eric Laurente83b55d2014-11-14 10:06:21 -0800239void AudioPolicyService::doReleaseOutput(audio_io_handle_t output,
240 audio_stream_type_t stream,
241 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800242{
243 ALOGV("doReleaseOutput from tid %d", gettid());
244 Mutex::Autolock _l(mLock);
Eric Laurente83b55d2014-11-14 10:06:21 -0800245 mAudioPolicyManager->releaseOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800246}
247
Eric Laurentcaf7f482014-11-25 17:50:47 -0800248status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr,
249 audio_io_handle_t *input,
250 audio_session_t session,
251 uint32_t samplingRate,
252 audio_format_t format,
253 audio_channel_mask_t channelMask,
254 audio_input_flags_t flags)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800255{
Eric Laurentdce54a12014-03-10 12:19:46 -0700256 if (mAudioPolicyManager == NULL) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800257 return NO_INIT;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800258 }
259 // already checked by client, but double-check in case the client wrapper is bypassed
Eric Laurentcaf7f482014-11-25 17:50:47 -0800260 if (attr->source >= AUDIO_SOURCE_CNT && attr->source != AUDIO_SOURCE_HOTWORD &&
261 attr->source != AUDIO_SOURCE_FM_TUNER) {
262 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800263 }
264
Eric Laurentab300c82015-04-13 13:47:33 -0700265 if ((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800266 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800267 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700268 sp<AudioPolicyEffects>audioPolicyEffects;
Eric Laurentcaf7f482014-11-25 17:50:47 -0800269 status_t status;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800270 AudioPolicyInterface::input_type_t inputType;
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700271 {
272 Mutex::Autolock _l(mLock);
273 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurentcaf7f482014-11-25 17:50:47 -0800274 status = mAudioPolicyManager->getInputForAttr(attr, input, session,
275 samplingRate, format, channelMask,
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800276 flags, &inputType);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700277 audioPolicyEffects = mAudioPolicyEffects;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800278
279 if (status == NO_ERROR) {
280 // enforce permission (if any) required for each type of input
281 switch (inputType) {
282 case AudioPolicyInterface::API_INPUT_LEGACY:
283 break;
284 case AudioPolicyInterface::API_INPUT_MIX_CAPTURE:
285 if (!captureAudioOutputAllowed()) {
286 ALOGE("getInputForAttr() permission denied: capture not allowed");
287 status = PERMISSION_DENIED;
288 }
289 break;
290 case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE:
291 if (!modifyAudioRoutingAllowed()) {
292 ALOGE("getInputForAttr() permission denied: modify audio routing not allowed");
293 status = PERMISSION_DENIED;
294 }
295 break;
296 case AudioPolicyInterface::API_INPUT_INVALID:
297 default:
298 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d",
299 (int)inputType);
300 }
301 }
302
303 if (status != NO_ERROR) {
304 if (status == PERMISSION_DENIED) {
305 mAudioPolicyManager->releaseInput(*input, session);
306 }
307 return status;
308 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700309 }
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800310
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700311 if (audioPolicyEffects != 0) {
312 // create audio pre processors according to input source
Eric Laurentcaf7f482014-11-25 17:50:47 -0800313 status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700314 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800315 ALOGW("Failed to add effects on input %d", *input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700316 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800317 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800318 return NO_ERROR;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800319}
320
Eric Laurent4dc68062014-07-28 17:26:49 -0700321status_t AudioPolicyService::startInput(audio_io_handle_t input,
322 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800323{
Eric Laurentdce54a12014-03-10 12:19:46 -0700324 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800325 return NO_INIT;
326 }
327 Mutex::Autolock _l(mLock);
328
Eric Laurent4dc68062014-07-28 17:26:49 -0700329 return mAudioPolicyManager->startInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800330}
331
Eric Laurent4dc68062014-07-28 17:26:49 -0700332status_t AudioPolicyService::stopInput(audio_io_handle_t input,
333 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800334{
Eric Laurentdce54a12014-03-10 12:19:46 -0700335 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800336 return NO_INIT;
337 }
338 Mutex::Autolock _l(mLock);
339
Eric Laurent4dc68062014-07-28 17:26:49 -0700340 return mAudioPolicyManager->stopInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800341}
342
Eric Laurent4dc68062014-07-28 17:26:49 -0700343void AudioPolicyService::releaseInput(audio_io_handle_t input,
344 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800345{
Eric Laurentdce54a12014-03-10 12:19:46 -0700346 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800347 return;
348 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700349 sp<AudioPolicyEffects>audioPolicyEffects;
350 {
351 Mutex::Autolock _l(mLock);
352 mAudioPolicyManager->releaseInput(input, session);
353 audioPolicyEffects = mAudioPolicyEffects;
354 }
355 if (audioPolicyEffects != 0) {
356 // release audio processors from the input
357 status_t status = audioPolicyEffects->releaseInputEffects(input);
358 if(status != NO_ERROR) {
359 ALOGW("Failed to release effects on input %d", input);
360 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800361 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800362}
363
364status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
365 int indexMin,
366 int indexMax)
367{
Eric Laurentdce54a12014-03-10 12:19:46 -0700368 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800369 return NO_INIT;
370 }
371 if (!settingsAllowed()) {
372 return PERMISSION_DENIED;
373 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800374 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800375 return BAD_VALUE;
376 }
377 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700378 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800379 return NO_ERROR;
380}
381
382status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
383 int index,
384 audio_devices_t device)
385{
Eric Laurentdce54a12014-03-10 12:19:46 -0700386 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800387 return NO_INIT;
388 }
389 if (!settingsAllowed()) {
390 return PERMISSION_DENIED;
391 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800392 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800393 return BAD_VALUE;
394 }
395 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700396 return mAudioPolicyManager->setStreamVolumeIndex(stream,
397 index,
398 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800399}
400
401status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
402 int *index,
403 audio_devices_t device)
404{
Eric Laurentdce54a12014-03-10 12:19:46 -0700405 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800406 return NO_INIT;
407 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800408 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800409 return BAD_VALUE;
410 }
411 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700412 return mAudioPolicyManager->getStreamVolumeIndex(stream,
413 index,
414 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800415}
416
417uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
418{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800419 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700420 return 0;
Eric Laurentdea15412014-10-28 15:46:45 -0700421 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700422 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800423 return 0;
424 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700425 return mAudioPolicyManager->getStrategyForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800426}
427
428//audio policy: use audio_device_t appropriately
429
430audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
431{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800432 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700433 return AUDIO_DEVICE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700434 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700435 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700436 return AUDIO_DEVICE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800437 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700438 return mAudioPolicyManager->getDevicesForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800439}
440
441audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
442{
443 // FIXME change return type to status_t, and return NO_INIT here
Eric Laurentdce54a12014-03-10 12:19:46 -0700444 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800445 return 0;
446 }
447 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700448 return mAudioPolicyManager->getOutputForEffect(desc);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800449}
450
451status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
452 audio_io_handle_t io,
453 uint32_t strategy,
454 int session,
455 int id)
456{
Eric Laurentdce54a12014-03-10 12:19:46 -0700457 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800458 return NO_INIT;
459 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700460 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800461}
462
463status_t AudioPolicyService::unregisterEffect(int id)
464{
Eric Laurentdce54a12014-03-10 12:19:46 -0700465 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800466 return NO_INIT;
467 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700468 return mAudioPolicyManager->unregisterEffect(id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800469}
470
471status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
472{
Eric Laurentdce54a12014-03-10 12:19:46 -0700473 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800474 return NO_INIT;
475 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700476 return mAudioPolicyManager->setEffectEnabled(id, enabled);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800477}
478
479bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
480{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800481 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700482 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700483 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700484 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700485 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800486 }
487 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700488 return mAudioPolicyManager->isStreamActive(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800489}
490
491bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
492{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800493 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700494 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700495 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700496 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700497 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800498 }
499 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700500 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800501}
502
503bool AudioPolicyService::isSourceActive(audio_source_t source) const
504{
Eric Laurentdce54a12014-03-10 12:19:46 -0700505 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800506 return false;
507 }
508 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700509 return mAudioPolicyManager->isSourceActive(source);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800510}
511
512status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
513 effect_descriptor_t *descriptors,
514 uint32_t *count)
515{
Eric Laurentdce54a12014-03-10 12:19:46 -0700516 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800517 *count = 0;
518 return NO_INIT;
519 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700520 sp<AudioPolicyEffects>audioPolicyEffects;
521 {
522 Mutex::Autolock _l(mLock);
523 audioPolicyEffects = mAudioPolicyEffects;
524 }
525 if (audioPolicyEffects == 0) {
526 *count = 0;
527 return NO_INIT;
528 }
529 return audioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800530}
531
532bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
533{
Eric Laurentdce54a12014-03-10 12:19:46 -0700534 if (mAudioPolicyManager == NULL) {
535 ALOGV("mAudioPolicyManager == NULL");
Eric Laurent2d388ec2014-03-07 13:25:54 -0800536 return false;
537 }
538
Eric Laurentdce54a12014-03-10 12:19:46 -0700539 return mAudioPolicyManager->isOffloadSupported(info);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800540}
541
Eric Laurent6a94d692014-05-20 11:18:06 -0700542status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
543 audio_port_type_t type,
Eric Laurent203b1a12014-04-01 10:34:16 -0700544 unsigned int *num_ports,
Eric Laurent6a94d692014-05-20 11:18:06 -0700545 struct audio_port *ports,
546 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700547{
Eric Laurent6a94d692014-05-20 11:18:06 -0700548 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700549 if(!modifyAudioRoutingAllowed()) {
550 return PERMISSION_DENIED;
551 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700552 if (mAudioPolicyManager == NULL) {
553 return NO_INIT;
554 }
555
556 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700557}
558
Eric Laurent6a94d692014-05-20 11:18:06 -0700559status_t AudioPolicyService::getAudioPort(struct audio_port *port)
Eric Laurent203b1a12014-04-01 10:34:16 -0700560{
Eric Laurent6a94d692014-05-20 11:18:06 -0700561 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700562 if(!modifyAudioRoutingAllowed()) {
563 return PERMISSION_DENIED;
564 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700565 if (mAudioPolicyManager == NULL) {
566 return NO_INIT;
567 }
568
569 return mAudioPolicyManager->getAudioPort(port);
Eric Laurent203b1a12014-04-01 10:34:16 -0700570}
571
Eric Laurent6a94d692014-05-20 11:18:06 -0700572status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
573 audio_patch_handle_t *handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700574{
Eric Laurent6a94d692014-05-20 11:18:06 -0700575 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700576 if(!modifyAudioRoutingAllowed()) {
577 return PERMISSION_DENIED;
578 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700579 if (mAudioPolicyManager == NULL) {
580 return NO_INIT;
581 }
582 return mAudioPolicyManager->createAudioPatch(patch, handle,
583 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700584}
585
Eric Laurent6a94d692014-05-20 11:18:06 -0700586status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700587{
Eric Laurent6a94d692014-05-20 11:18:06 -0700588 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700589 if(!modifyAudioRoutingAllowed()) {
590 return PERMISSION_DENIED;
591 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700592 if (mAudioPolicyManager == NULL) {
593 return NO_INIT;
594 }
595
596 return mAudioPolicyManager->releaseAudioPatch(handle,
597 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700598}
599
600status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
Eric Laurent6a94d692014-05-20 11:18:06 -0700601 struct audio_patch *patches,
602 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700603{
Eric Laurent6a94d692014-05-20 11:18:06 -0700604 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700605 if(!modifyAudioRoutingAllowed()) {
606 return PERMISSION_DENIED;
607 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700608 if (mAudioPolicyManager == NULL) {
609 return NO_INIT;
610 }
611
612 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700613}
614
Eric Laurent6a94d692014-05-20 11:18:06 -0700615status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent203b1a12014-04-01 10:34:16 -0700616{
Eric Laurent6a94d692014-05-20 11:18:06 -0700617 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700618 if(!modifyAudioRoutingAllowed()) {
619 return PERMISSION_DENIED;
620 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700621 if (mAudioPolicyManager == NULL) {
622 return NO_INIT;
623 }
624
625 return mAudioPolicyManager->setAudioPortConfig(config);
Eric Laurent203b1a12014-04-01 10:34:16 -0700626}
Eric Laurent2d388ec2014-03-07 13:25:54 -0800627
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700628status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
629 audio_io_handle_t *ioHandle,
630 audio_devices_t *device)
631{
632 if (mAudioPolicyManager == NULL) {
633 return NO_INIT;
634 }
635
636 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
637}
638
639status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
640{
641 if (mAudioPolicyManager == NULL) {
642 return NO_INIT;
643 }
644
645 return mAudioPolicyManager->releaseSoundTriggerSession(session);
646}
647
Eric Laurentbaac1832014-12-01 17:52:59 -0800648status_t AudioPolicyService::registerPolicyMixes(Vector<AudioMix> mixes, bool registration)
649{
650 Mutex::Autolock _l(mLock);
651 if(!modifyAudioRoutingAllowed()) {
652 return PERMISSION_DENIED;
653 }
654 if (mAudioPolicyManager == NULL) {
655 return NO_INIT;
656 }
657 if (registration) {
658 return mAudioPolicyManager->registerPolicyMixes(mixes);
659 } else {
660 return mAudioPolicyManager->unregisterPolicyMixes(mixes);
661 }
662}
663
Eric Laurent554a2772015-04-10 11:29:24 -0700664status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
665 const audio_attributes_t *attributes,
666 audio_io_handle_t *handle)
667{
668 Mutex::Autolock _l(mLock);
669 if (mAudioPolicyManager == NULL) {
670 return NO_INIT;
671 }
672
673 return mAudioPolicyManager->startAudioSource(source, attributes, handle);
674}
675
676status_t AudioPolicyService::stopAudioSource(audio_io_handle_t handle)
677{
678 Mutex::Autolock _l(mLock);
679 if (mAudioPolicyManager == NULL) {
680 return NO_INIT;
681 }
682
683 return mAudioPolicyManager->stopAudioSource(handle);
684}
685
Eric Laurent2d388ec2014-03-07 13:25:54 -0800686}; // namespace android