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