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