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