AudioPolicyInterfaceImpl.cpp revision df3dc7e2fe6c639529b70e3f3a7d2bf0f4c6e871
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    return NO_ERROR;
84}
85
86status_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
87                                         audio_policy_forced_cfg_t config)
88{
89    if (mAudioPolicyManager == NULL) {
90        return NO_INIT;
91    }
92    if (!settingsAllowed()) {
93        return PERMISSION_DENIED;
94    }
95    if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
96        return BAD_VALUE;
97    }
98    if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
99        return BAD_VALUE;
100    }
101    ALOGV("setForceUse()");
102    Mutex::Autolock _l(mLock);
103    mAudioPolicyManager->setForceUse(usage, config);
104    return NO_ERROR;
105}
106
107audio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
108{
109    if (mAudioPolicyManager == NULL) {
110        return AUDIO_POLICY_FORCE_NONE;
111    }
112    if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
113        return AUDIO_POLICY_FORCE_NONE;
114    }
115    return mAudioPolicyManager->getForceUse(usage);
116}
117
118audio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
119                                    uint32_t samplingRate,
120                                    audio_format_t format,
121                                    audio_channel_mask_t channelMask,
122                                    audio_output_flags_t flags,
123                                    const audio_offload_info_t *offloadInfo)
124{
125    if (mAudioPolicyManager == NULL) {
126        return 0;
127    }
128    ALOGV("getOutput()");
129    Mutex::Autolock _l(mLock);
130    return mAudioPolicyManager->getOutput(stream, samplingRate,
131                                    format, channelMask, flags, offloadInfo);
132}
133
134audio_io_handle_t AudioPolicyService::getOutputForAttr(const audio_attributes_t *attr,
135                                    uint32_t samplingRate,
136                                    audio_format_t format,
137                                    audio_channel_mask_t channelMask,
138                                    audio_output_flags_t flags,
139                                    const audio_offload_info_t *offloadInfo)
140{
141    if (mAudioPolicyManager == NULL) {
142        return 0;
143    }
144    ALOGV("getOutput()");
145    Mutex::Autolock _l(mLock);
146    return mAudioPolicyManager->getOutputForAttr(attr, samplingRate,
147                                    format, channelMask, flags, offloadInfo);
148}
149
150status_t AudioPolicyService::startOutput(audio_io_handle_t output,
151                                         audio_stream_type_t stream,
152                                         int session)
153{
154    if (mAudioPolicyManager == NULL) {
155        return NO_INIT;
156    }
157    ALOGV("startOutput()");
158    Mutex::Autolock _l(mLock);
159
160    // create audio processors according to stream
161    status_t status = mAudioPolicyEffects->addOutputSessionEffects(output, stream, session);
162    if (status != NO_ERROR && status != ALREADY_EXISTS) {
163        ALOGW("Failed to add effects on session %d", session);
164    }
165
166    return mAudioPolicyManager->startOutput(output, stream, session);
167}
168
169status_t AudioPolicyService::stopOutput(audio_io_handle_t output,
170                                        audio_stream_type_t stream,
171                                        int session)
172{
173    if (mAudioPolicyManager == NULL) {
174        return NO_INIT;
175    }
176    ALOGV("stopOutput()");
177    mOutputCommandThread->stopOutputCommand(output, stream, session);
178    return NO_ERROR;
179}
180
181status_t  AudioPolicyService::doStopOutput(audio_io_handle_t output,
182                                      audio_stream_type_t stream,
183                                      int session)
184{
185    ALOGV("doStopOutput from tid %d", gettid());
186    Mutex::Autolock _l(mLock);
187
188    // release audio processors from the stream
189    status_t status = mAudioPolicyEffects->releaseOutputSessionEffects(output, stream, session);
190    if (status != NO_ERROR && status != ALREADY_EXISTS) {
191        ALOGW("Failed to release effects on session %d", session);
192    }
193
194    return mAudioPolicyManager->stopOutput(output, stream, session);
195}
196
197void AudioPolicyService::releaseOutput(audio_io_handle_t output)
198{
199    if (mAudioPolicyManager == NULL) {
200        return;
201    }
202    ALOGV("releaseOutput()");
203    mOutputCommandThread->releaseOutputCommand(output);
204}
205
206void AudioPolicyService::doReleaseOutput(audio_io_handle_t output)
207{
208    ALOGV("doReleaseOutput from tid %d", gettid());
209    Mutex::Autolock _l(mLock);
210    mAudioPolicyManager->releaseOutput(output);
211}
212
213audio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
214                                    uint32_t samplingRate,
215                                    audio_format_t format,
216                                    audio_channel_mask_t channelMask,
217                                    int audioSession,
218                                    audio_input_flags_t flags)
219{
220    if (mAudioPolicyManager == NULL) {
221        return 0;
222    }
223    // already checked by client, but double-check in case the client wrapper is bypassed
224    if (inputSource >= AUDIO_SOURCE_CNT && inputSource != AUDIO_SOURCE_HOTWORD) {
225        return 0;
226    }
227
228    if ((inputSource == AUDIO_SOURCE_HOTWORD) && !captureHotwordAllowed()) {
229        return 0;
230    }
231
232    Mutex::Autolock _l(mLock);
233    // the audio_in_acoustics_t parameter is ignored by get_input()
234    audio_io_handle_t input = mAudioPolicyManager->getInput(inputSource, samplingRate,
235                                                   format, channelMask,
236                                                   (audio_session_t)audioSession, flags);
237
238    if (input == 0) {
239        return input;
240    }
241
242    // create audio pre processors according to input source
243    status_t status = mAudioPolicyEffects->addInputEffects(input, inputSource, audioSession);
244    if (status != NO_ERROR && status != ALREADY_EXISTS) {
245        ALOGW("Failed to add effects on input %d", input);
246    }
247
248    return input;
249}
250
251status_t AudioPolicyService::startInput(audio_io_handle_t input,
252                                        audio_session_t session)
253{
254    if (mAudioPolicyManager == NULL) {
255        return NO_INIT;
256    }
257    Mutex::Autolock _l(mLock);
258
259    return mAudioPolicyManager->startInput(input, session);
260}
261
262status_t AudioPolicyService::stopInput(audio_io_handle_t input,
263                                       audio_session_t session)
264{
265    if (mAudioPolicyManager == NULL) {
266        return NO_INIT;
267    }
268    Mutex::Autolock _l(mLock);
269
270    return mAudioPolicyManager->stopInput(input, session);
271}
272
273void AudioPolicyService::releaseInput(audio_io_handle_t input,
274                                      audio_session_t session)
275{
276    if (mAudioPolicyManager == NULL) {
277        return;
278    }
279    Mutex::Autolock _l(mLock);
280    mAudioPolicyManager->releaseInput(input, session);
281
282    // release audio processors from the input
283    status_t status = mAudioPolicyEffects->releaseInputEffects(input);
284    if(status != NO_ERROR) {
285        ALOGW("Failed to release effects on input %d", input);
286    }
287}
288
289status_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
290                                            int indexMin,
291                                            int indexMax)
292{
293    if (mAudioPolicyManager == NULL) {
294        return NO_INIT;
295    }
296    if (!settingsAllowed()) {
297        return PERMISSION_DENIED;
298    }
299    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
300        return BAD_VALUE;
301    }
302    Mutex::Autolock _l(mLock);
303    mAudioPolicyManager->initStreamVolume(stream, indexMin, indexMax);
304    return NO_ERROR;
305}
306
307status_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
308                                                  int index,
309                                                  audio_devices_t device)
310{
311    if (mAudioPolicyManager == NULL) {
312        return NO_INIT;
313    }
314    if (!settingsAllowed()) {
315        return PERMISSION_DENIED;
316    }
317    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
318        return BAD_VALUE;
319    }
320    Mutex::Autolock _l(mLock);
321    return mAudioPolicyManager->setStreamVolumeIndex(stream,
322                                                    index,
323                                                    device);
324}
325
326status_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
327                                                  int *index,
328                                                  audio_devices_t device)
329{
330    if (mAudioPolicyManager == NULL) {
331        return NO_INIT;
332    }
333    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
334        return BAD_VALUE;
335    }
336    Mutex::Autolock _l(mLock);
337    return mAudioPolicyManager->getStreamVolumeIndex(stream,
338                                                    index,
339                                                    device);
340}
341
342uint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
343{
344    if (mAudioPolicyManager == NULL) {
345        return 0;
346    }
347    return mAudioPolicyManager->getStrategyForStream(stream);
348}
349
350//audio policy: use audio_device_t appropriately
351
352audio_devices_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
353{
354    if (mAudioPolicyManager == NULL) {
355        return (audio_devices_t)0;
356    }
357    return mAudioPolicyManager->getDevicesForStream(stream);
358}
359
360audio_io_handle_t AudioPolicyService::getOutputForEffect(const effect_descriptor_t *desc)
361{
362    // FIXME change return type to status_t, and return NO_INIT here
363    if (mAudioPolicyManager == NULL) {
364        return 0;
365    }
366    Mutex::Autolock _l(mLock);
367    return mAudioPolicyManager->getOutputForEffect(desc);
368}
369
370status_t AudioPolicyService::registerEffect(const effect_descriptor_t *desc,
371                                audio_io_handle_t io,
372                                uint32_t strategy,
373                                int session,
374                                int id)
375{
376    if (mAudioPolicyManager == NULL) {
377        return NO_INIT;
378    }
379    return mAudioPolicyManager->registerEffect(desc, io, strategy, session, id);
380}
381
382status_t AudioPolicyService::unregisterEffect(int id)
383{
384    if (mAudioPolicyManager == NULL) {
385        return NO_INIT;
386    }
387    return mAudioPolicyManager->unregisterEffect(id);
388}
389
390status_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
391{
392    if (mAudioPolicyManager == NULL) {
393        return NO_INIT;
394    }
395    return mAudioPolicyManager->setEffectEnabled(id, enabled);
396}
397
398bool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
399{
400    if (mAudioPolicyManager == NULL) {
401        return 0;
402    }
403    Mutex::Autolock _l(mLock);
404    return mAudioPolicyManager->isStreamActive(stream, inPastMs);
405}
406
407bool AudioPolicyService::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const
408{
409    if (mAudioPolicyManager == NULL) {
410        return 0;
411    }
412    Mutex::Autolock _l(mLock);
413    return mAudioPolicyManager->isStreamActiveRemotely(stream, inPastMs);
414}
415
416bool AudioPolicyService::isSourceActive(audio_source_t source) const
417{
418    if (mAudioPolicyManager == NULL) {
419        return false;
420    }
421    Mutex::Autolock _l(mLock);
422    return mAudioPolicyManager->isSourceActive(source);
423}
424
425status_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
426                                                       effect_descriptor_t *descriptors,
427                                                       uint32_t *count)
428{
429    if (mAudioPolicyManager == NULL) {
430        *count = 0;
431        return NO_INIT;
432    }
433    Mutex::Autolock _l(mLock);
434
435    return mAudioPolicyEffects->queryDefaultInputEffects(audioSession, descriptors, count);
436}
437
438bool AudioPolicyService::isOffloadSupported(const audio_offload_info_t& info)
439{
440    if (mAudioPolicyManager == NULL) {
441        ALOGV("mAudioPolicyManager == NULL");
442        return false;
443    }
444
445    return mAudioPolicyManager->isOffloadSupported(info);
446}
447
448status_t AudioPolicyService::listAudioPorts(audio_port_role_t role,
449                                            audio_port_type_t type,
450                                            unsigned int *num_ports,
451                                            struct audio_port *ports,
452                                            unsigned int *generation)
453{
454    Mutex::Autolock _l(mLock);
455    if(!modifyAudioRoutingAllowed()) {
456        return PERMISSION_DENIED;
457    }
458    if (mAudioPolicyManager == NULL) {
459        return NO_INIT;
460    }
461
462    return mAudioPolicyManager->listAudioPorts(role, type, num_ports, ports, generation);
463}
464
465status_t AudioPolicyService::getAudioPort(struct audio_port *port)
466{
467    Mutex::Autolock _l(mLock);
468    if(!modifyAudioRoutingAllowed()) {
469        return PERMISSION_DENIED;
470    }
471    if (mAudioPolicyManager == NULL) {
472        return NO_INIT;
473    }
474
475    return mAudioPolicyManager->getAudioPort(port);
476}
477
478status_t AudioPolicyService::createAudioPatch(const struct audio_patch *patch,
479        audio_patch_handle_t *handle)
480{
481    Mutex::Autolock _l(mLock);
482    if(!modifyAudioRoutingAllowed()) {
483        return PERMISSION_DENIED;
484    }
485    if (mAudioPolicyManager == NULL) {
486        return NO_INIT;
487    }
488    return mAudioPolicyManager->createAudioPatch(patch, handle,
489                                                  IPCThreadState::self()->getCallingUid());
490}
491
492status_t AudioPolicyService::releaseAudioPatch(audio_patch_handle_t handle)
493{
494    Mutex::Autolock _l(mLock);
495    if(!modifyAudioRoutingAllowed()) {
496        return PERMISSION_DENIED;
497    }
498    if (mAudioPolicyManager == NULL) {
499        return NO_INIT;
500    }
501
502    return mAudioPolicyManager->releaseAudioPatch(handle,
503                                                     IPCThreadState::self()->getCallingUid());
504}
505
506status_t AudioPolicyService::listAudioPatches(unsigned int *num_patches,
507        struct audio_patch *patches,
508        unsigned int *generation)
509{
510    Mutex::Autolock _l(mLock);
511    if(!modifyAudioRoutingAllowed()) {
512        return PERMISSION_DENIED;
513    }
514    if (mAudioPolicyManager == NULL) {
515        return NO_INIT;
516    }
517
518    return mAudioPolicyManager->listAudioPatches(num_patches, patches, generation);
519}
520
521status_t AudioPolicyService::setAudioPortConfig(const struct audio_port_config *config)
522{
523    Mutex::Autolock _l(mLock);
524    if(!modifyAudioRoutingAllowed()) {
525        return PERMISSION_DENIED;
526    }
527    if (mAudioPolicyManager == NULL) {
528        return NO_INIT;
529    }
530
531    return mAudioPolicyManager->setAudioPortConfig(config);
532}
533
534status_t AudioPolicyService::acquireSoundTriggerSession(audio_session_t *session,
535                                       audio_io_handle_t *ioHandle,
536                                       audio_devices_t *device)
537{
538    if (mAudioPolicyManager == NULL) {
539        return NO_INIT;
540    }
541
542    return mAudioPolicyManager->acquireSoundTriggerSession(session, ioHandle, device);
543}
544
545status_t AudioPolicyService::releaseSoundTriggerSession(audio_session_t session)
546{
547    if (mAudioPolicyManager == NULL) {
548        return NO_INIT;
549    }
550
551    return mAudioPolicyManager->releaseSoundTriggerSession(session);
552}
553
554}; // namespace android
555