1 /*
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 
17 #define LOG_TAG "AudioPolicyIntefaceImpl"
18 //#define LOG_NDEBUG 0
19 
20 #include <utils/Log.h>
21 #include "AudioPolicyService.h"
22 #include "ServiceUtilities.h"
23 
24 namespace android {
25 
26 
27 // ----------------------------------------------------------------------------
28 
setDeviceConnectionState(audio_devices_t device,audio_policy_dev_state_t state,const char * device_address,const char * device_name)29 status_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
30                                                   audio_policy_dev_state_t state,
31                                                   const char *device_address,
32                                                   const char *device_name)
33 {
34     if (mAudioPolicyManager == NULL) {
35         return NO_INIT;
36     }
37     if (!settingsAllowed()) {
38         return PERMISSION_DENIED;
39     }
40     if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
41             state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
42         return BAD_VALUE;
43     }
44 
45     ALOGV("setDeviceConnectionState()");
46     Mutex::Autolock _l(mLock);
47     return mAudioPolicyManager->setDeviceConnectionState(device, state,
48                                                          device_address, device_name);
49 }
50 
getDeviceConnectionState(audio_devices_t device,const char * device_address)51 audio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
52                                                               audio_devices_t device,
53                                                               const char *device_address)
54 {
55     if (mAudioPolicyManager == NULL) {
56         return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
57     }
58     return mAudioPolicyManager->getDeviceConnectionState(device,
59                                                       device_address);
60 }
61 
handleDeviceConfigChange(audio_devices_t device,const char * device_address,const char * device_name)62 status_t AudioPolicyService::handleDeviceConfigChange(audio_devices_t device,
63                                                   const char *device_address,
64                                                   const char *device_name)
65 {
66     if (mAudioPolicyManager == NULL) {
67         return NO_INIT;
68     }
69     if (!settingsAllowed()) {
70         return PERMISSION_DENIED;
71     }
72 
73     ALOGV("handleDeviceConfigChange()");
74     Mutex::Autolock _l(mLock);
75     return mAudioPolicyManager->handleDeviceConfigChange(device, device_address,
76                                                          device_name);
77 }
78 
setPhoneState(audio_mode_t state)79 status_t AudioPolicyService::setPhoneState(audio_mode_t state)
80 {
81     if (mAudioPolicyManager == NULL) {
82         return NO_INIT;
83     }
84     if (!settingsAllowed()) {
85         return PERMISSION_DENIED;
86     }
87     if (uint32_t(state) >= AUDIO_MODE_CNT) {
88         return BAD_VALUE;
89     }
90 
91     ALOGV("setPhoneState()");
92 
93     // acquire lock before calling setMode() so that setMode() + setPhoneState() are an atomic
94     // operation from policy manager standpoint (no other operation (e.g track start or stop)
95     // can be interleaved).
96     Mutex::Autolock _l(mLock);
97 
98     // TODO: check if it is more appropriate to do it in platform specific policy manager
99     AudioSystem::setMode(state);
100 
101     mAudioPolicyManager->setPhoneState(state);
102     mPhoneState = state;
103     return NO_ERROR;
104 }
105 
getPhoneState()106 audio_mode_t AudioPolicyService::getPhoneState()
107 {
108     Mutex::Autolock _l(mLock);
109     return mPhoneState;
110 }
111 
setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)112 status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
113                                          audio_policy_forced_cfg_t config)
114 {
115     if (mAudioPolicyManager == NULL) {
116         return NO_INIT;
117     }
118     if (!settingsAllowed()) {
119         return PERMISSION_DENIED;
120     }
121     if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
122         return BAD_VALUE;
123     }
124     if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
125         return BAD_VALUE;
126     }
127     ALOGV("setForceUse()");
128     Mutex::Autolock _l(mLock);
129     mAudioPolicyManager->setForceUse(usage, config);
130     return NO_ERROR;
131 }
132 
getForceUse(audio_policy_force_use_t usage)133 audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
134 {
135     if (mAudioPolicyManager == NULL) {
136         return AUDIO_POLICY_FORCE_NONE;
137     }
138     if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
139         return AUDIO_POLICY_FORCE_NONE;
140     }
141     return mAudioPolicyManager->getForceUse(usage);
142 }
143 
getOutput(audio_stream_type_t stream,uint32_t samplingRate,audio_format_t format,audio_channel_mask_t channelMask,audio_output_flags_t flags,const audio_offload_info_t * offloadInfo)144 audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
145                                     uint32_t samplingRate,
146                                     audio_format_t format,
147                                     audio_channel_mask_t channelMask,
148                                     audio_output_flags_t flags,
149                                     const audio_offload_info_t *offloadInfo)
150 {
151     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
152         return AUDIO_IO_HANDLE_NONE;
153     }
154     if (mAudioPolicyManager == NULL) {
155         return AUDIO_IO_HANDLE_NONE;
156     }
157     ALOGV("getOutput()");
158     Mutex::Autolock _l(mLock);
159     return mAudioPolicyManager->getOutput(stream, samplingRate,
160                                     format, channelMask, flags, offloadInfo);
161 }
162 
getOutputForAttr(const audio_attributes_t * attr,audio_io_handle_t * output,audio_session_t session,audio_stream_type_t * stream,uid_t uid,const audio_config_t * config,audio_output_flags_t flags,audio_port_handle_t selectedDeviceId,audio_port_handle_t * portId)163 status_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr,
164                                               audio_io_handle_t *output,
165                                               audio_session_t session,
166                                               audio_stream_type_t *stream,
167                                               uid_t uid,
168                                               const audio_config_t *config,
169                                               audio_output_flags_t flags,
170                                               audio_port_handle_t selectedDeviceId,
171                                               audio_port_handle_t *portId)
172 {
173     if (mAudioPolicyManager == NULL) {
174         return NO_INIT;
175     }
176     ALOGV("getOutput()");
177     Mutex::Autolock _l(mLock);
178 
179     const uid_t callingUid = IPCThreadState::self()->getCallingUid();
180     if (!isTrustedCallingUid(callingUid) || uid == (uid_t)-1) {
181         ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
182                 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
183         uid = callingUid;
184     }
185     return mAudioPolicyManager->getOutputForAttr(attr, output, session, stream, uid,
186                                                  config,
187                                                  flags, selectedDeviceId, portId);
188 }
189 
startOutput(audio_io_handle_t output,audio_stream_type_t stream,audio_session_t session)190 status_t AudioPolicyService::startOutput(audio_io_handle_t output,
191                                          audio_stream_type_t stream,
192                                          audio_session_t session)
193 {
194     if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
195         return BAD_VALUE;
196     }
197     if (mAudioPolicyManager == NULL) {
198         return NO_INIT;
199     }
200     ALOGV("startOutput()");
201     sp<AudioPolicyEffects>audioPolicyEffects;
202     {
203         Mutex::Autolock _l(mLock);
204         audioPolicyEffects = mAudioPolicyEffects;
205     }
206     if (audioPolicyEffects != 0) {
207         // create audio processors according to stream
208         status_t status = audioPolicyEffects->addOutputSessionEffects(output, stream, session);
209         if (status != NO_ERROR && status != ALREADY_EXISTS) {
210             ALOGW("Failed to add effects on session %d", session);
211         }
212     }
213     Mutex::Autolock _l(mLock);
214     return mAudioPolicyManager->startOutput(output, stream, session);
215 }
216 
stopOutput(audio_io_handle_t output,audio_stream_type_t stream,audio_session_t session)217 status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
218                                         audio_stream_type_t stream,
219                                         audio_session_t session)
220 {
221     if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
222         return BAD_VALUE;
223     }
224     if (mAudioPolicyManager == NULL) {
225         return NO_INIT;
226     }
227     ALOGV("stopOutput()");
228     mOutputCommandThread->stopOutputCommand(output, stream, session);
229     return NO_ERROR;
230 }
231 
doStopOutput(audio_io_handle_t output,audio_stream_type_t stream,audio_session_t session)232 status_t  AudioPolicyService::doStopOutput(audio_io_handle_t output,
233                                       audio_stream_type_t stream,
234                                       audio_session_t session)
235 {
236     ALOGV("doStopOutput from tid %d", gettid());
237     sp<AudioPolicyEffects>audioPolicyEffects;
238     {
239         Mutex::Autolock _l(mLock);
240         audioPolicyEffects = mAudioPolicyEffects;
241     }
242     if (audioPolicyEffects != 0) {
243         // release audio processors from the stream
244         status_t status = audioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
245         if (status != NO_ERROR && status != ALREADY_EXISTS) {
246             ALOGW("Failed to release effects on session %d", session);
247         }
248     }
249     Mutex::Autolock _l(mLock);
250     return mAudioPolicyManager->stopOutput(output, stream, session);
251 }
252 
releaseOutput(audio_io_handle_t output,audio_stream_type_t stream,audio_session_t session)253 void AudioPolicyService::releaseOutput(audio_io_handle_t output,
254                                        audio_stream_type_t stream,
255                                        audio_session_t session)
256 {
257     if (mAudioPolicyManager == NULL) {
258         return;
259     }
260     ALOGV("releaseOutput()");
261     mOutputCommandThread->releaseOutputCommand(output, stream, session);
262 }
263 
doReleaseOutput(audio_io_handle_t output,audio_stream_type_t stream,audio_session_t session)264 void AudioPolicyService::doReleaseOutput(audio_io_handle_t output,
265                                          audio_stream_type_t stream,
266                                          audio_session_t session)
267 {
268     ALOGV("doReleaseOutput from tid %d", gettid());
269     Mutex::Autolock _l(mLock);
270     mAudioPolicyManager->releaseOutput(output, stream, session);
271 }
272 
getInputForAttr(const audio_attributes_t * attr,audio_io_handle_t * input,audio_session_t session,pid_t pid,uid_t uid,const audio_config_base_t * config,audio_input_flags_t flags,audio_port_handle_t selectedDeviceId,audio_port_handle_t * portId)273 status_t AudioPolicyService::getInputForAttr(const audio_attributes_t *attr,
274                                              audio_io_handle_t *input,
275                                              audio_session_t session,
276                                              pid_t pid,
277                                              uid_t uid,
278                                              const audio_config_base_t *config,
279                                              audio_input_flags_t flags,
280                                              audio_port_handle_t selectedDeviceId,
281                                              audio_port_handle_t *portId)
282 {
283     if (mAudioPolicyManager == NULL) {
284         return NO_INIT;
285     }
286     // already checked by client, but double-check in case the client wrapper is bypassed
287     if (attr->source >= AUDIO_SOURCE_CNT && attr->source != AUDIO_SOURCE_HOTWORD &&
288         attr->source != AUDIO_SOURCE_FM_TUNER) {
289         return BAD_VALUE;
290     }
291 
292     if ((attr->source == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
293         return BAD_VALUE;
294     }
295     sp<AudioPolicyEffects>audioPolicyEffects;
296     status_t status;
297     AudioPolicyInterface::input_type_t inputType;
298 
299     bool updatePid = (pid == -1);
300     const uid_t callingUid = IPCThreadState::self()->getCallingUid();
301     if (!isTrustedCallingUid(callingUid)) {
302         ALOGW_IF(uid != (uid_t)-1 && uid != callingUid,
303                 "%s uid %d tried to pass itself off as %d", __FUNCTION__, callingUid, uid);
304         uid = callingUid;
305         updatePid = true;
306     }
307 
308     if (updatePid) {
309         const pid_t callingPid = IPCThreadState::self()->getCallingPid();
310         ALOGW_IF(pid != (pid_t)-1 && pid != callingPid,
311                  "%s uid %d pid %d tried to pass itself off as pid %d",
312                  __func__, callingUid, callingPid, pid);
313         pid = callingPid;
314     }
315 
316     {
317         Mutex::Autolock _l(mLock);
318         // the audio_in_acoustics_t parameter is ignored by get_input()
319         status = mAudioPolicyManager->getInputForAttr(attr, input, session, uid,
320                                                      config,
321                                                      flags, selectedDeviceId,
322                                                      &inputType, portId);
323         audioPolicyEffects = mAudioPolicyEffects;
324 
325         if (status == NO_ERROR) {
326             // enforce permission (if any) required for each type of input
327             switch (inputType) {
328             case AudioPolicyInterface::API_INPUT_LEGACY:
329                 break;
330             case AudioPolicyInterface::API_INPUT_TELEPHONY_RX:
331                 // FIXME: use the same permission as for remote submix for now.
332             case AudioPolicyInterface::API_INPUT_MIX_CAPTURE:
333                 if (!captureAudioOutputAllowed(pid, uid)) {
334                     ALOGE("getInputForAttr() permission denied: capture not allowed");
335                     status = PERMISSION_DENIED;
336                 }
337                 break;
338             case AudioPolicyInterface::API_INPUT_MIX_EXT_POLICY_REROUTE:
339                 if (!modifyAudioRoutingAllowed()) {
340                     ALOGE("getInputForAttr() permission denied: modify audio routing not allowed");
341                     status = PERMISSION_DENIED;
342                 }
343                 break;
344             case AudioPolicyInterface::API_INPUT_INVALID:
345             default:
346                 LOG_ALWAYS_FATAL("getInputForAttr() encountered an invalid input type %d",
347                         (int)inputType);
348             }
349         }
350 
351         if (status != NO_ERROR) {
352             if (status == PERMISSION_DENIED) {
353                 mAudioPolicyManager->releaseInput(*input, session);
354             }
355             return status;
356         }
357     }
358 
359     if (audioPolicyEffects != 0) {
360         // create audio pre processors according to input source
361         status_t status = audioPolicyEffects->addInputEffects(*input, attr->source, session);
362         if (status != NO_ERROR && status != ALREADY_EXISTS) {
363             ALOGW("Failed to add effects on input %d", *input);
364         }
365     }
366     return NO_ERROR;
367 }
368 
startInput(audio_io_handle_t input,audio_session_t session)369 status_t AudioPolicyService::startInput(audio_io_handle_t input,
370                                         audio_session_t session)
371 {
372     if (mAudioPolicyManager == NULL) {
373         return NO_INIT;
374     }
375     Mutex::Autolock _l(mLock);
376     AudioPolicyInterface::concurrency_type__mask_t concurrency;
377     status_t status = mAudioPolicyManager->startInput(input, session, &concurrency);
378 
379     if (status == NO_ERROR) {
380         LOG_ALWAYS_FATAL_IF(concurrency & ~AudioPolicyInterface::API_INPUT_CONCURRENCY_ALL,
381                             "startInput(): invalid concurrency type %d", (int)concurrency);
382 
383         // enforce permission (if any) required for each type of concurrency
384         if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CALL) {
385             //TODO: check incall capture permission
386         }
387         if (concurrency & AudioPolicyInterface::API_INPUT_CONCURRENCY_CAPTURE) {
388             //TODO: check concurrent capture permission
389         }
390     }
391 
392     return status;
393 }
394 
stopInput(audio_io_handle_t input,audio_session_t session)395 status_t AudioPolicyService::stopInput(audio_io_handle_t input,
396                                        audio_session_t session)
397 {
398     if (mAudioPolicyManager == NULL) {
399         return NO_INIT;
400     }
401     Mutex::Autolock _l(mLock);
402 
403     return mAudioPolicyManager->stopInput(input, session);
404 }
405 
releaseInput(audio_io_handle_t input,audio_session_t session)406 void AudioPolicyService::releaseInput(audio_io_handle_t input,
407                                       audio_session_t session)
408 {
409     if (mAudioPolicyManager == NULL) {
410         return;
411     }
412     sp<AudioPolicyEffects>audioPolicyEffects;
413     {
414         Mutex::Autolock _l(mLock);
415         audioPolicyEffects = mAudioPolicyEffects;
416     }
417     if (audioPolicyEffects != 0) {
418         // release audio processors from the input
419         status_t status = audioPolicyEffects->releaseInputEffects(input, session);
420         if(status != NO_ERROR) {
421             ALOGW("Failed to release effects on input %d", input);
422         }
423     }
424     {
425         Mutex::Autolock _l(mLock);
426         mAudioPolicyManager->releaseInput(input, session);
427     }
428 }
429 
initStreamVolume(audio_stream_type_t stream,int indexMin,int indexMax)430 status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
431                                             int indexMin,
432                                             int indexMax)
433 {
434     if (mAudioPolicyManager == NULL) {
435         return NO_INIT;
436     }
437     if (!settingsAllowed()) {
438         return PERMISSION_DENIED;
439     }
440     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
441         return BAD_VALUE;
442     }
443     Mutex::Autolock _l(mLock);
444     mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
445     return NO_ERROR;
446 }
447 
setStreamVolumeIndex(audio_stream_type_t stream,int index,audio_devices_t device)448 status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
449                                                   int index,
450                                                   audio_devices_t device)
451 {
452     if (mAudioPolicyManager == NULL) {
453         return NO_INIT;
454     }
455     if (!settingsAllowed()) {
456         return PERMISSION_DENIED;
457     }
458     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
459         return BAD_VALUE;
460     }
461     Mutex::Autolock _l(mLock);
462     return mAudioPolicyManager->setStreamVolumeIndex(stream,
463                                                     index,
464                                                     device);
465 }
466 
getStreamVolumeIndex(audio_stream_type_t stream,int * index,audio_devices_t device)467 status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
468                                                   int *index,
469                                                   audio_devices_t device)
470 {
471     if (mAudioPolicyManager == NULL) {
472         return NO_INIT;
473     }
474     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
475         return BAD_VALUE;
476     }
477     Mutex::Autolock _l(mLock);
478     return mAudioPolicyManager->getStreamVolumeIndex(stream,
479                                                     index,
480                                                     device);
481 }
482 
getStrategyForStream(audio_stream_type_t stream)483 uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
484 {
485     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
486         return 0;
487     }
488     if (mAudioPolicyManager == NULL) {
489         return 0;
490     }
491     return mAudioPolicyManager->getStrategyForStream(stream);
492 }
493 
494 //audio policy: use audio_device_t appropriately
495 
getDevicesForStream(audio_stream_type_t stream)496 audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
497 {
498     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
499         return AUDIO_DEVICE_NONE;
500     }
501     if (mAudioPolicyManager == NULL) {
502         return AUDIO_DEVICE_NONE;
503     }
504     Mutex::Autolock _l(mLock);
505     return mAudioPolicyManager->getDevicesForStream(stream);
506 }
507 
getOutputForEffect(const effect_descriptor_t * desc)508 audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
509 {
510     // FIXME change return type to status_t, and return NO_INIT here
511     if (mAudioPolicyManager == NULL) {
512         return 0;
513     }
514     Mutex::Autolock _l(mLock);
515     return mAudioPolicyManager->getOutputForEffect(desc);
516 }
517 
registerEffect(const effect_descriptor_t * desc,audio_io_handle_t io,uint32_t strategy,audio_session_t session,int id)518 status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
519                                 audio_io_handle_t io,
520                                 uint32_t strategy,
521                                 audio_session_t session,
522                                 int id)
523 {
524     if (mAudioPolicyManager == NULL) {
525         return NO_INIT;
526     }
527     Mutex::Autolock _l(mEffectsLock);
528     return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
529 }
530 
unregisterEffect(int id)531 status_t AudioPolicyService::unregisterEffect(int id)
532 {
533     if (mAudioPolicyManager == NULL) {
534         return NO_INIT;
535     }
536     Mutex::Autolock _l(mEffectsLock);
537     return mAudioPolicyManager->unregisterEffect(id);
538 }
539 
setEffectEnabled(int id,bool enabled)540 status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
541 {
542     if (mAudioPolicyManager == NULL) {
543         return NO_INIT;
544     }
545     Mutex::Autolock _l(mEffectsLock);
546     return mAudioPolicyManager->setEffectEnabled(id, enabled);
547 }
548 
isStreamActive(audio_stream_type_t stream,uint32_t inPastMs) const549 bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
550 {
551     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
552         return false;
553     }
554     if (mAudioPolicyManager == NULL) {
555         return false;
556     }
557     Mutex::Autolock _l(mLock);
558     return mAudioPolicyManager->isStreamActive(stream, inPastMs);
559 }
560 
isStreamActiveRemotely(audio_stream_type_t stream,uint32_t inPastMs) const561 bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
562 {
563     if (uint32_t(stream) >= AUDIO_STREAM_PUBLIC_CNT) {
564         return false;
565     }
566     if (mAudioPolicyManager == NULL) {
567         return false;
568     }
569     Mutex::Autolock _l(mLock);
570     return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
571 }
572 
isSourceActive(audio_source_t source) const573 bool AudioPolicyService::isSourceActive(audio_source_t source) const
574 {
575     if (mAudioPolicyManager == NULL) {
576         return false;
577     }
578     Mutex::Autolock _l(mLock);
579     return mAudioPolicyManager->isSourceActive(source);
580 }
581 
queryDefaultPreProcessing(audio_session_t audioSession,effect_descriptor_t * descriptors,uint32_t * count)582 status_t AudioPolicyService::queryDefaultPreProcessing(audio_session_t audioSession,
583                                                        effect_descriptor_t *descriptors,
584                                                        uint32_t *count)
585 {
586     if (mAudioPolicyManager == NULL) {
587         *count = 0;
588         return NO_INIT;
589     }
590     sp<AudioPolicyEffects>audioPolicyEffects;
591     {
592         Mutex::Autolock _l(mLock);
593         audioPolicyEffects = mAudioPolicyEffects;
594     }
595     if (audioPolicyEffects == 0) {
596         *count = 0;
597         return NO_INIT;
598     }
599     return audioPolicyEffects->queryDefaultInputEffects(
600             (audio_session_t)audioSession, descriptors, count);
601 }
602 
isOffloadSupported(const audio_offload_info_t & info)603 bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
604 {
605     if (mAudioPolicyManager == NULL) {
606         ALOGV("mAudioPolicyManager == NULL");
607         return false;
608     }
609     Mutex::Autolock _l(mLock);
610     Mutex::Autolock _le(mEffectsLock); // isOffloadSupported queries for
611                                       // non-offloadable effects
612     return mAudioPolicyManager->isOffloadSupported(info);
613 }
614 
listAudioPorts(audio_port_role_t role,audio_port_type_t type,unsigned int * num_ports,struct audio_port * ports,unsigned int * generation)615 status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
616                                             audio_port_type_t type,
617                                             unsigned int *num_ports,
618                                             struct audio_port *ports,
619                                             unsigned int *generation)
620 {
621     Mutex::Autolock _l(mLock);
622     if (mAudioPolicyManager == NULL) {
623         return NO_INIT;
624     }
625 
626     return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
627 }
628 
getAudioPort(struct audio_port * port)629 status_t AudioPolicyService::getAudioPort(struct audio_port *port)
630 {
631     Mutex::Autolock _l(mLock);
632     if (mAudioPolicyManager == NULL) {
633         return NO_INIT;
634     }
635 
636     return mAudioPolicyManager->getAudioPort(port);
637 }
638 
createAudioPatch(const struct audio_patch * patch,audio_patch_handle_t * handle)639 status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
640         audio_patch_handle_t *handle)
641 {
642     Mutex::Autolock _l(mLock);
643     if(!modifyAudioRoutingAllowed()) {
644         return PERMISSION_DENIED;
645     }
646     if (mAudioPolicyManager == NULL) {
647         return NO_INIT;
648     }
649     return mAudioPolicyManager->createAudioPatch(patch, handle,
650                                                   IPCThreadState::self()->getCallingUid());
651 }
652 
releaseAudioPatch(audio_patch_handle_t handle)653 status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
654 {
655     Mutex::Autolock _l(mLock);
656     if(!modifyAudioRoutingAllowed()) {
657         return PERMISSION_DENIED;
658     }
659     if (mAudioPolicyManager == NULL) {
660         return NO_INIT;
661     }
662 
663     return mAudioPolicyManager->releaseAudioPatch(handle,
664                                                      IPCThreadState::self()->getCallingUid());
665 }
666 
listAudioPatches(unsigned int * num_patches,struct audio_patch * patches,unsigned int * generation)667 status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
668         struct audio_patch *patches,
669         unsigned int *generation)
670 {
671     Mutex::Autolock _l(mLock);
672     if (mAudioPolicyManager == NULL) {
673         return NO_INIT;
674     }
675 
676     return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
677 }
678 
setAudioPortConfig(const struct audio_port_config * config)679 status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
680 {
681     Mutex::Autolock _l(mLock);
682     if(!modifyAudioRoutingAllowed()) {
683         return PERMISSION_DENIED;
684     }
685     if (mAudioPolicyManager == NULL) {
686         return NO_INIT;
687     }
688 
689     return mAudioPolicyManager->setAudioPortConfig(config);
690 }
691 
acquireSoundTriggerSession(audio_session_t * session,audio_io_handle_t * ioHandle,audio_devices_t * device)692 status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
693                                        audio_io_handle_t *ioHandle,
694                                        audio_devices_t *device)
695 {
696     if (mAudioPolicyManager == NULL) {
697         return NO_INIT;
698     }
699 
700     return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
701 }
702 
releaseSoundTriggerSession(audio_session_t session)703 status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
704 {
705     if (mAudioPolicyManager == NULL) {
706         return NO_INIT;
707     }
708 
709     return mAudioPolicyManager->releaseSoundTriggerSession(session);
710 }
711 
registerPolicyMixes(const Vector<AudioMix> & mixes,bool registration)712 status_t AudioPolicyService::registerPolicyMixes(const Vector<AudioMix>& mixes, bool registration)
713 {
714     Mutex::Autolock _l(mLock);
715     if(!modifyAudioRoutingAllowed()) {
716         return PERMISSION_DENIED;
717     }
718     if (mAudioPolicyManager == NULL) {
719         return NO_INIT;
720     }
721     if (registration) {
722         return mAudioPolicyManager->registerPolicyMixes(mixes);
723     } else {
724         return mAudioPolicyManager->unregisterPolicyMixes(mixes);
725     }
726 }
727 
startAudioSource(const struct audio_port_config * source,const audio_attributes_t * attributes,audio_patch_handle_t * handle)728 status_t AudioPolicyService::startAudioSource(const struct audio_port_config *source,
729                                   const audio_attributes_t *attributes,
730                                   audio_patch_handle_t *handle)
731 {
732     Mutex::Autolock _l(mLock);
733     if (mAudioPolicyManager == NULL) {
734         return NO_INIT;
735     }
736 
737     return mAudioPolicyManager->startAudioSource(source, attributes, handle,
738                                                  IPCThreadState::self()->getCallingUid());
739 }
740 
stopAudioSource(audio_patch_handle_t handle)741 status_t AudioPolicyService::stopAudioSource(audio_patch_handle_t handle)
742 {
743     Mutex::Autolock _l(mLock);
744     if (mAudioPolicyManager == NULL) {
745         return NO_INIT;
746     }
747 
748     return mAudioPolicyManager->stopAudioSource(handle);
749 }
750 
setMasterMono(bool mono)751 status_t AudioPolicyService::setMasterMono(bool mono)
752 {
753     if (mAudioPolicyManager == NULL) {
754         return NO_INIT;
755     }
756     if (!settingsAllowed()) {
757         return PERMISSION_DENIED;
758     }
759     Mutex::Autolock _l(mLock);
760     return mAudioPolicyManager->setMasterMono(mono);
761 }
762 
getMasterMono(bool * mono)763 status_t AudioPolicyService::getMasterMono(bool *mono)
764 {
765     if (mAudioPolicyManager == NULL) {
766         return NO_INIT;
767     }
768     Mutex::Autolock _l(mLock);
769     return mAudioPolicyManager->getMasterMono(mono);
770 }
771 
772 }; // namespace android
773