blob: 41a3fcb5d188bcfc525057c0da31afe7d63430aa [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
167 // if the caller is us, trust the specified uid
168 if (IPCThreadState::self()->getCallingPid() != getpid_cached || uid == (uid_t)-1) {
169 uid_t newclientUid = IPCThreadState::self()->getCallingUid();
170 if (uid != (uid_t)-1 && uid != newclientUid) {
171 ALOGW("%s uid %d tried to pass itself off as %d", __FUNCTION__, newclientUid, uid);
172 }
173 uid = newclientUid;
174 }
175 return mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid, samplingRate,
Paul McLean466dc8e2015-04-17 13:15:36 -0600176 format, channelMask, flags, selectedDeviceId, offloadInfo);
Jean-Michel Trivi5bd3f382014-06-13 16:06:54 -0700177}
178
Eric Laurent2d388ec2014-03-07 13:25:54 -0800179status_t AudioPolicyService::startOutput(audio_io_handle_t output,
180 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800181 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800182{
Eric Laurentdea15412014-10-28 15:46:45 -0700183 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
184 return BAD_VALUE;
185 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700186 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800187 return NO_INIT;
188 }
189 ALOGV("startOutput()");
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700190 sp<AudioPolicyEffects>audioPolicyEffects;
191 {
192 Mutex::Autolock _l(mLock);
193 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800194 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700195 if (audioPolicyEffects != 0) {
196 // create audio processors according to stream
197 status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session);
198 if (status != NO_ERROR && status != ALREADY_EXISTS) {
199 ALOGW("Failed to add effects on session %d", session);
200 }
201 }
202 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700203 return mAudioPolicyManager->startOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800204}
205
206status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
207 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800208 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800209{
Eric Laurentdea15412014-10-28 15:46:45 -0700210 if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
211 return BAD_VALUE;
212 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700213 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800214 return NO_INIT;
215 }
216 ALOGV("stopOutput()");
217 mOutputCommandThread->stopOutputCommand(output, stream, session);
218 return NO_ERROR;
219}
220
221status_t AudioPolicyService::doStopOutput(audio_io_handle_t output,
222 audio_stream_type_t stream,
Eric Laurente83b55d2014-11-14 10:06:21 -0800223 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800224{
225 ALOGV("doStopOutput from tid %d", gettid());
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700226 sp<AudioPolicyEffects>audioPolicyEffects;
227 {
228 Mutex::Autolock _l(mLock);
229 audioPolicyEffects = mAudioPolicyEffects;
bryant_liuba2b4392014-06-11 16:49:30 +0800230 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700231 if (audioPolicyEffects != 0) {
232 // release audio processors from the stream
233 status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
234 if (status != NO_ERROR && status != ALREADY_EXISTS) {
235 ALOGW("Failed to release effects on session %d", session);
236 }
237 }
238 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700239 return mAudioPolicyManager->stopOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800240}
241
Eric Laurente83b55d2014-11-14 10:06:21 -0800242void AudioPolicyService::releaseOutput(audio_io_handle_t output,
243 audio_stream_type_t stream,
244 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800245{
Eric Laurentdce54a12014-03-10 12:19:46 -0700246 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800247 return;
248 }
249 ALOGV("releaseOutput()");
Eric Laurente83b55d2014-11-14 10:06:21 -0800250 mOutputCommandThread->releaseOutputCommand(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800251}
252
Eric Laurente83b55d2014-11-14 10:06:21 -0800253void AudioPolicyService::doReleaseOutput(audio_io_handle_t output,
254 audio_stream_type_t stream,
255 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800256{
257 ALOGV("doReleaseOutput from tid %d", gettid());
258 Mutex::Autolock _l(mLock);
Eric Laurente83b55d2014-11-14 10:06:21 -0800259 mAudioPolicyManager->releaseOutput(output, stream, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800260}
261
Eric Laurentcaf7f482014-11-25 17:50:47 -0800262status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr,
263 audio_io_handle_t *input,
264 audio_session_t session,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700265 uid_t uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800266 uint32_t samplingRate,
267 audio_format_t format,
268 audio_channel_mask_t channelMask,
Paul McLean466dc8e2015-04-17 13:15:36 -0600269 audio_input_flags_t flags,
270 audio_port_handle_t selectedDeviceId)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800271{
Eric Laurentdce54a12014-03-10 12:19:46 -0700272 if (mAudioPolicyManager == NULL) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800273 return NO_INIT;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800274 }
275 // already checked by client, but double-check in case the client wrapper is bypassed
Eric Laurentcaf7f482014-11-25 17:50:47 -0800276 if (attr->source >= AUDIO_SOURCE_CNT && attr->source != AUDIO_SOURCE_HOTWORD &&
277 attr->source != AUDIO_SOURCE_FM_TUNER) {
278 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800279 }
280
Eric Laurentab300c82015-04-13 13:47:33 -0700281 if ((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800282 return BAD_VALUE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800283 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700284 sp<AudioPolicyEffects>audioPolicyEffects;
Eric Laurentcaf7f482014-11-25 17:50:47 -0800285 status_t status;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800286 AudioPolicyInterface::input_type_t inputType;
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700287 // if the caller is us, trust the specified uid
288 if (IPCThreadState::self()->getCallingPid() != getpid_cached || uid == (uid_t)-1) {
289 uid_t newclientUid = IPCThreadState::self()->getCallingUid();
290 if (uid != (uid_t)-1 && uid != newclientUid) {
291 ALOGW("%s uid %d tried to pass itself off as %d", __FUNCTION__, newclientUid, uid);
292 }
293 uid = newclientUid;
294 }
295
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700296 {
297 Mutex::Autolock _l(mLock);
298 // the audio_in_acoustics_t parameter is ignored by get_input()
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700299 status = mAudioPolicyManager->getInputForAttr(attr, input, session, uid,
Eric Laurentcaf7f482014-11-25 17:50:47 -0800300 samplingRate, format, channelMask,
Eric Laurent8c7e6da2015-04-21 17:37:00 -0700301 flags, selectedDeviceId,
302 &inputType);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700303 audioPolicyEffects = mAudioPolicyEffects;
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800304
305 if (status == NO_ERROR) {
306 // enforce permission (if any) required for each type of input
307 switch (inputType) {
308 case AudioPolicyInterface::API_INPUT_LEGACY:
309 break;
Eric Laurent82db2692015-08-07 13:59:42 -0700310 case AudioPolicyInterface::API_INPUT_TELEPHONY_RX:
311 // FIXME: use the same permission as for remote submix for now.
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800312 case AudioPolicyInterface::API_INPUT_MIX_CAPTURE:
313 if (!captureAudioOutputAllowed()) {
314 ALOGE("getInputForAttr() permission denied: capture not allowed");
315 status = PERMISSION_DENIED;
316 }
317 break;
318 case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE:
319 if (!modifyAudioRoutingAllowed()) {
320 ALOGE("getInputForAttr() permission denied: modify audio routing not allowed");
321 status = PERMISSION_DENIED;
322 }
323 break;
324 case AudioPolicyInterface::API_INPUT_INVALID:
325 default:
326 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d",
327 (int)inputType);
328 }
329 }
330
331 if (status != NO_ERROR) {
332 if (status == PERMISSION_DENIED) {
333 mAudioPolicyManager->releaseInput(*input, session);
334 }
335 return status;
336 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700337 }
Jean-Michel Trivi97bb33f2014-12-12 16:23:43 -0800338
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700339 if (audioPolicyEffects != 0) {
340 // create audio pre processors according to input source
Eric Laurentcaf7f482014-11-25 17:50:47 -0800341 status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700342 if (status != NO_ERROR && status != ALREADY_EXISTS) {
Eric Laurentcaf7f482014-11-25 17:50:47 -0800343 ALOGW("Failed to add effects on input %d", *input);
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700344 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800345 }
Eric Laurentcaf7f482014-11-25 17:50:47 -0800346 return NO_ERROR;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800347}
348
Eric Laurent4dc68062014-07-28 17:26:49 -0700349status_t AudioPolicyService::startInput(audio_io_handle_t input,
350 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800351{
Eric Laurentdce54a12014-03-10 12:19:46 -0700352 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800353 return NO_INIT;
354 }
355 Mutex::Autolock _l(mLock);
356
Eric Laurent4dc68062014-07-28 17:26:49 -0700357 return mAudioPolicyManager->startInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800358}
359
Eric Laurent4dc68062014-07-28 17:26:49 -0700360status_t AudioPolicyService::stopInput(audio_io_handle_t input,
361 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800362{
Eric Laurentdce54a12014-03-10 12:19:46 -0700363 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800364 return NO_INIT;
365 }
366 Mutex::Autolock _l(mLock);
367
Eric Laurent4dc68062014-07-28 17:26:49 -0700368 return mAudioPolicyManager->stopInput(input, session);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800369}
370
Eric Laurent4dc68062014-07-28 17:26:49 -0700371void AudioPolicyService::releaseInput(audio_io_handle_t input,
372 audio_session_t session)
Eric Laurent2d388ec2014-03-07 13:25:54 -0800373{
Eric Laurentdce54a12014-03-10 12:19:46 -0700374 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800375 return;
376 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700377 sp<AudioPolicyEffects>audioPolicyEffects;
378 {
379 Mutex::Autolock _l(mLock);
380 mAudioPolicyManager->releaseInput(input, session);
381 audioPolicyEffects = mAudioPolicyEffects;
382 }
383 if (audioPolicyEffects != 0) {
384 // release audio processors from the input
385 status_t status = audioPolicyEffects->releaseInputEffects(input);
386 if(status != NO_ERROR) {
387 ALOGW("Failed to release effects on input %d", input);
388 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800389 }
Eric Laurent2d388ec2014-03-07 13:25:54 -0800390}
391
392status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
393 int indexMin,
394 int indexMax)
395{
Eric Laurentdce54a12014-03-10 12:19:46 -0700396 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800397 return NO_INIT;
398 }
399 if (!settingsAllowed()) {
400 return PERMISSION_DENIED;
401 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800402 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800403 return BAD_VALUE;
404 }
405 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700406 mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800407 return NO_ERROR;
408}
409
410status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
411 int index,
412 audio_devices_t device)
413{
Eric Laurentdce54a12014-03-10 12:19:46 -0700414 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800415 return NO_INIT;
416 }
417 if (!settingsAllowed()) {
418 return PERMISSION_DENIED;
419 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800420 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800421 return BAD_VALUE;
422 }
423 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700424 return mAudioPolicyManager->setStreamVolumeIndex(stream,
425 index,
426 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800427}
428
429status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
430 int *index,
431 audio_devices_t device)
432{
Eric Laurentdce54a12014-03-10 12:19:46 -0700433 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800434 return NO_INIT;
435 }
Eric Laurent223fd5c2014-11-11 13:43:36 -0800436 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800437 return BAD_VALUE;
438 }
439 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700440 return mAudioPolicyManager->getStreamVolumeIndex(stream,
441 index,
442 device);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800443}
444
445uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
446{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800447 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700448 return 0;
Eric Laurentdea15412014-10-28 15:46:45 -0700449 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700450 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800451 return 0;
452 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700453 return mAudioPolicyManager->getStrategyForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800454}
455
456//audio policy: use audio_device_t appropriately
457
458audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
459{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800460 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700461 return AUDIO_DEVICE_NONE;
Eric Laurentdea15412014-10-28 15:46:45 -0700462 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700463 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700464 return AUDIO_DEVICE_NONE;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800465 }
Haynes Mathew Georgedfb9f3b2015-10-26 18:22:13 -0700466 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700467 return mAudioPolicyManager->getDevicesForStream(stream);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800468}
469
470audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
471{
472 // FIXME change return type to status_t, and return NO_INIT here
Eric Laurentdce54a12014-03-10 12:19:46 -0700473 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800474 return 0;
475 }
476 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700477 return mAudioPolicyManager->getOutputForEffect(desc);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800478}
479
480status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
481 audio_io_handle_t io,
482 uint32_t strategy,
483 int session,
484 int id)
485{
Eric Laurentdce54a12014-03-10 12:19:46 -0700486 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800487 return NO_INIT;
488 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700489 return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800490}
491
492status_t AudioPolicyService::unregisterEffect(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 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700497 return mAudioPolicyManager->unregisterEffect(id);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800498}
499
500status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
501{
Eric Laurentdce54a12014-03-10 12:19:46 -0700502 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800503 return NO_INIT;
504 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700505 return mAudioPolicyManager->setEffectEnabled(id, enabled);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800506}
507
508bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
509{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800510 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700511 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700512 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700513 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700514 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800515 }
516 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700517 return mAudioPolicyManager->isStreamActive(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800518}
519
520bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
521{
Eric Laurent223fd5c2014-11-11 13:43:36 -0800522 if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700523 return false;
Eric Laurentdea15412014-10-28 15:46:45 -0700524 }
Eric Laurentdce54a12014-03-10 12:19:46 -0700525 if (mAudioPolicyManager == NULL) {
Eric Laurentb1322c72014-10-30 14:59:13 -0700526 return false;
Eric Laurent2d388ec2014-03-07 13:25:54 -0800527 }
528 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700529 return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800530}
531
532bool AudioPolicyService::isSourceActive(audio_source_t source) const
533{
Eric Laurentdce54a12014-03-10 12:19:46 -0700534 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800535 return false;
536 }
537 Mutex::Autolock _l(mLock);
Eric Laurentdce54a12014-03-10 12:19:46 -0700538 return mAudioPolicyManager->isSourceActive(source);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800539}
540
541status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
542 effect_descriptor_t *descriptors,
543 uint32_t *count)
544{
Eric Laurentdce54a12014-03-10 12:19:46 -0700545 if (mAudioPolicyManager == NULL) {
Eric Laurent2d388ec2014-03-07 13:25:54 -0800546 *count = 0;
547 return NO_INIT;
548 }
Eric Laurent8b1e80b2014-10-07 09:08:47 -0700549 sp<AudioPolicyEffects>audioPolicyEffects;
550 {
551 Mutex::Autolock _l(mLock);
552 audioPolicyEffects = mAudioPolicyEffects;
553 }
554 if (audioPolicyEffects == 0) {
555 *count = 0;
556 return NO_INIT;
557 }
558 return audioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800559}
560
561bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
562{
Eric Laurentdce54a12014-03-10 12:19:46 -0700563 if (mAudioPolicyManager == NULL) {
564 ALOGV("mAudioPolicyManager == NULL");
Eric Laurent2d388ec2014-03-07 13:25:54 -0800565 return false;
566 }
567
Eric Laurentdce54a12014-03-10 12:19:46 -0700568 return mAudioPolicyManager->isOffloadSupported(info);
Eric Laurent2d388ec2014-03-07 13:25:54 -0800569}
570
Eric Laurent6a94d692014-05-20 11:18:06 -0700571status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
572 audio_port_type_t type,
Eric Laurent203b1a12014-04-01 10:34:16 -0700573 unsigned int *num_ports,
Eric Laurent6a94d692014-05-20 11:18:06 -0700574 struct audio_port *ports,
575 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700576{
Eric Laurent6a94d692014-05-20 11:18:06 -0700577 Mutex::Autolock _l(mLock);
578 if (mAudioPolicyManager == NULL) {
579 return NO_INIT;
580 }
581
582 return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700583}
584
Eric Laurent6a94d692014-05-20 11:18:06 -0700585status_t AudioPolicyService::getAudioPort(struct audio_port *port)
Eric Laurent203b1a12014-04-01 10:34:16 -0700586{
Eric Laurent6a94d692014-05-20 11:18:06 -0700587 Mutex::Autolock _l(mLock);
588 if (mAudioPolicyManager == NULL) {
589 return NO_INIT;
590 }
591
592 return mAudioPolicyManager->getAudioPort(port);
Eric Laurent203b1a12014-04-01 10:34:16 -0700593}
594
Eric Laurent6a94d692014-05-20 11:18:06 -0700595status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
596 audio_patch_handle_t *handle)
Eric Laurent203b1a12014-04-01 10:34:16 -0700597{
Eric Laurent6a94d692014-05-20 11:18:06 -0700598 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700599 if(!modifyAudioRoutingAllowed()) {
600 return PERMISSION_DENIED;
601 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700602 if (mAudioPolicyManager == NULL) {
603 return NO_INIT;
604 }
605 return mAudioPolicyManager->createAudioPatch(patch, handle,
606 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700607}
608
Eric Laurent6a94d692014-05-20 11:18:06 -0700609status_t AudioPolicyService::releaseAudioPatch(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
619 return mAudioPolicyManager->releaseAudioPatch(handle,
620 IPCThreadState::self()->getCallingUid());
Eric Laurent203b1a12014-04-01 10:34:16 -0700621}
622
623status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
Eric Laurent6a94d692014-05-20 11:18:06 -0700624 struct audio_patch *patches,
625 unsigned int *generation)
Eric Laurent203b1a12014-04-01 10:34:16 -0700626{
Eric Laurent6a94d692014-05-20 11:18:06 -0700627 Mutex::Autolock _l(mLock);
628 if (mAudioPolicyManager == NULL) {
629 return NO_INIT;
630 }
631
632 return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
Eric Laurent203b1a12014-04-01 10:34:16 -0700633}
634
Eric Laurent6a94d692014-05-20 11:18:06 -0700635status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
Eric Laurent203b1a12014-04-01 10:34:16 -0700636{
Eric Laurent6a94d692014-05-20 11:18:06 -0700637 Mutex::Autolock _l(mLock);
Eric Laurent5284ed52014-05-29 14:37:38 -0700638 if(!modifyAudioRoutingAllowed()) {
639 return PERMISSION_DENIED;
640 }
Eric Laurent6a94d692014-05-20 11:18:06 -0700641 if (mAudioPolicyManager == NULL) {
642 return NO_INIT;
643 }
644
645 return mAudioPolicyManager->setAudioPortConfig(config);
Eric Laurent203b1a12014-04-01 10:34:16 -0700646}
Eric Laurent2d388ec2014-03-07 13:25:54 -0800647
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700648status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
649 audio_io_handle_t *ioHandle,
650 audio_devices_t *device)
651{
652 if (mAudioPolicyManager == NULL) {
653 return NO_INIT;
654 }
655
656 return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
657}
658
659status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
660{
661 if (mAudioPolicyManager == NULL) {
662 return NO_INIT;
663 }
664
665 return mAudioPolicyManager->releaseSoundTriggerSession(session);
666}
667
Eric Laurentbaac1832014-12-01 17:52:59 -0800668status_t AudioPolicyService::registerPolicyMixes(Vector<AudioMix> mixes, bool registration)
669{
670 Mutex::Autolock _l(mLock);
671 if(!modifyAudioRoutingAllowed()) {
672 return PERMISSION_DENIED;
673 }
674 if (mAudioPolicyManager == NULL) {
675 return NO_INIT;
676 }
677 if (registration) {
678 return mAudioPolicyManager->registerPolicyMixes(mixes);
679 } else {
680 return mAudioPolicyManager->unregisterPolicyMixes(mixes);
681 }
682}
683
Eric Laurent554a2772015-04-10 11:29:24 -0700684status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
685 const audio_attributes_t *attributes,
686 audio_io_handle_t *handle)
687{
688 Mutex::Autolock _l(mLock);
689 if (mAudioPolicyManager == NULL) {
690 return NO_INIT;
691 }
692
Eric Laurentd60560a2015-04-10 11:31:20 -0700693 return mAudioPolicyManager->startAudioSource(source, attributes, handle,
694 IPCThreadState::self()->getCallingUid());
Eric Laurent554a2772015-04-10 11:29:24 -0700695}
696
697status_t AudioPolicyService::stopAudioSource(audio_io_handle_t handle)
698{
699 Mutex::Autolock _l(mLock);
700 if (mAudioPolicyManager == NULL) {
701 return NO_INIT;
702 }
703
704 return mAudioPolicyManager->stopAudioSource(handle);
705}
706
Eric Laurent2d388ec2014-03-07 13:25:54 -0800707}; // namespace android