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