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 "Configuration.h"
     21 #undef __STRICT_ANSI__
     22 #define __STDINT_LIMITS
     23 #define __STDC_LIMIT_MACROS
     24 #include <stdint.h>
     25 
     26 #include <sys/time.h>
     27 #include <binder/IServiceManager.h>
     28 #include <utils/Log.h>
     29 #include <cutils/properties.h>
     30 #include <binder/IPCThreadState.h>
     31 #include <utils/String16.h>
     32 #include <utils/threads.h>
     33 #include "AudioPolicyService.h"
     34 #include "ServiceUtilities.h"
     35 #include <hardware_legacy/power.h>
     36 #include <media/AudioEffect.h>
     37 #include <media/EffectsFactoryApi.h>
     38 //#include <media/IAudioFlinger.h>
     39 
     40 #include <hardware/hardware.h>
     41 #include <system/audio.h>
     42 #include <system/audio_policy.h>
     43 #include <hardware/audio_policy.h>
     44 #include <audio_effects/audio_effects_conf.h>
     45 #include <media/AudioParameter.h>
     46 
     47 
     48 namespace android {
     49 
     50 /* implementation of the interface to the policy manager */
     51 extern "C" {
     52 
     53 audio_module_handle_t aps_load_hw_module(void *service __unused,
     54                                              const char *name)
     55 {
     56     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
     57     if (af == 0) {
     58         ALOGW("%s: could not get AudioFlinger", __func__);
     59         return 0;
     60     }
     61 
     62     return af->loadHwModule(name);
     63 }
     64 
     65 static audio_io_handle_t open_output(audio_module_handle_t module,
     66                                     audio_devices_t *pDevices,
     67                                     uint32_t *pSamplingRate,
     68                                     audio_format_t *pFormat,
     69                                     audio_channel_mask_t *pChannelMask,
     70                                     uint32_t *pLatencyMs,
     71                                     audio_output_flags_t flags,
     72                                     const audio_offload_info_t *offloadInfo)
     73 {
     74     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
     75     if (af == 0) {
     76         ALOGW("%s: could not get AudioFlinger", __func__);
     77         return AUDIO_IO_HANDLE_NONE;
     78     }
     79 
     80     if (pSamplingRate == NULL || pFormat == NULL || pChannelMask == NULL ||
     81             pDevices == NULL || pLatencyMs == NULL) {
     82         return AUDIO_IO_HANDLE_NONE;
     83     }
     84     audio_config_t config = AUDIO_CONFIG_INITIALIZER;
     85     config.sample_rate = *pSamplingRate;
     86     config.format = *pFormat;
     87     config.channel_mask = *pChannelMask;
     88     if (offloadInfo != NULL) {
     89         config.offload_info = *offloadInfo;
     90     }
     91     audio_io_handle_t output = AUDIO_IO_HANDLE_NONE;
     92     status_t status = af->openOutput(module, &output, &config, pDevices,
     93                                      String8(""), pLatencyMs, flags);
     94     if (status == NO_ERROR) {
     95         *pSamplingRate = config.sample_rate;
     96         *pFormat = config.format;
     97         *pChannelMask = config.channel_mask;
     98         if (offloadInfo != NULL) {
     99             *((audio_offload_info_t *)offloadInfo) = config.offload_info;
    100         }
    101     }
    102     return output;
    103 }
    104 
    105 // deprecated: replaced by aps_open_output_on_module()
    106 audio_io_handle_t aps_open_output(void *service __unused,
    107                                          audio_devices_t *pDevices,
    108                                          uint32_t *pSamplingRate,
    109                                          audio_format_t *pFormat,
    110                                          audio_channel_mask_t *pChannelMask,
    111                                          uint32_t *pLatencyMs,
    112                                          audio_output_flags_t flags)
    113 {
    114     return open_output(AUDIO_MODULE_HANDLE_NONE, pDevices, pSamplingRate, pFormat, pChannelMask,
    115                           pLatencyMs, flags, NULL);
    116 }
    117 
    118 audio_io_handle_t aps_open_output_on_module(void *service __unused,
    119                                                    audio_module_handle_t module,
    120                                                    audio_devices_t *pDevices,
    121                                                    uint32_t *pSamplingRate,
    122                                                    audio_format_t *pFormat,
    123                                                    audio_channel_mask_t *pChannelMask,
    124                                                    uint32_t *pLatencyMs,
    125                                                    audio_output_flags_t flags,
    126                                                    const audio_offload_info_t *offloadInfo)
    127 {
    128     return open_output(module, pDevices, pSamplingRate, pFormat, pChannelMask,
    129                           pLatencyMs, flags, offloadInfo);
    130 }
    131 
    132 audio_io_handle_t aps_open_dup_output(void *service __unused,
    133                                                  audio_io_handle_t output1,
    134                                                  audio_io_handle_t output2)
    135 {
    136     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
    137     if (af == 0) {
    138         ALOGW("%s: could not get AudioFlinger", __func__);
    139         return 0;
    140     }
    141     return af->openDuplicateOutput(output1, output2);
    142 }
    143 
    144 int aps_close_output(void *service __unused, audio_io_handle_t output)
    145 {
    146     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
    147     if (af == 0) {
    148         return PERMISSION_DENIED;
    149     }
    150 
    151     return af->closeOutput(output);
    152 }
    153 
    154 int aps_suspend_output(void *service __unused, audio_io_handle_t output)
    155 {
    156     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
    157     if (af == 0) {
    158         ALOGW("%s: could not get AudioFlinger", __func__);
    159         return PERMISSION_DENIED;
    160     }
    161 
    162     return af->suspendOutput(output);
    163 }
    164 
    165 int aps_restore_output(void *service __unused, audio_io_handle_t output)
    166 {
    167     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
    168     if (af == 0) {
    169         ALOGW("%s: could not get AudioFlinger", __func__);
    170         return PERMISSION_DENIED;
    171     }
    172 
    173     return af->restoreOutput(output);
    174 }
    175 
    176 static audio_io_handle_t open_input(audio_module_handle_t module,
    177                                     audio_devices_t *pDevices,
    178                                     uint32_t *pSamplingRate,
    179                                     audio_format_t *pFormat,
    180                                     audio_channel_mask_t *pChannelMask)
    181 {
    182     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
    183     if (af == 0) {
    184         ALOGW("%s: could not get AudioFlinger", __func__);
    185         return AUDIO_IO_HANDLE_NONE;
    186     }
    187 
    188     if (pSamplingRate == NULL || pFormat == NULL || pChannelMask == NULL || pDevices == NULL) {
    189         return AUDIO_IO_HANDLE_NONE;
    190     }
    191 
    192     if (((*pDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) == AUDIO_DEVICE_IN_REMOTE_SUBMIX)
    193             && !captureAudioOutputAllowed(IPCThreadState::self()->getCallingPid(),
    194                                           IPCThreadState::self()->getCallingUid())) {
    195         ALOGE("open_input() permission denied: capture not allowed");
    196         return AUDIO_IO_HANDLE_NONE;
    197     }
    198 
    199     audio_config_t config = AUDIO_CONFIG_INITIALIZER;;
    200     config.sample_rate = *pSamplingRate;
    201     config.format = *pFormat;
    202     config.channel_mask = *pChannelMask;
    203     audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
    204     status_t status = af->openInput(module, &input, &config, pDevices,
    205                                     String8(""), AUDIO_SOURCE_MIC, AUDIO_INPUT_FLAG_FAST /*FIXME*/);
    206     if (status == NO_ERROR) {
    207         *pSamplingRate = config.sample_rate;
    208         *pFormat = config.format;
    209         *pChannelMask = config.channel_mask;
    210     }
    211     return input;
    212 }
    213 
    214 
    215 // deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
    216 audio_io_handle_t aps_open_input(void *service __unused,
    217                                         audio_devices_t *pDevices,
    218                                         uint32_t *pSamplingRate,
    219                                         audio_format_t *pFormat,
    220                                         audio_channel_mask_t *pChannelMask,
    221                                         audio_in_acoustics_t acoustics __unused)
    222 {
    223     return  open_input(AUDIO_MODULE_HANDLE_NONE, pDevices, pSamplingRate, pFormat, pChannelMask);
    224 }
    225 
    226 audio_io_handle_t aps_open_input_on_module(void *service __unused,
    227                                                   audio_module_handle_t module,
    228                                                   audio_devices_t *pDevices,
    229                                                   uint32_t *pSamplingRate,
    230                                                   audio_format_t *pFormat,
    231                                                   audio_channel_mask_t *pChannelMask)
    232 {
    233     return  open_input(module, pDevices, pSamplingRate, pFormat, pChannelMask);
    234 }
    235 
    236 int aps_close_input(void *service __unused, audio_io_handle_t input)
    237 {
    238     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
    239     if (af == 0) {
    240         return PERMISSION_DENIED;
    241     }
    242 
    243     return af->closeInput(input);
    244 }
    245 
    246 int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream)
    247 {
    248     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
    249     if (af == 0) {
    250         return PERMISSION_DENIED;
    251     }
    252 
    253     return af->invalidateStream(stream);
    254 }
    255 
    256 int aps_move_effects(void *service __unused, audio_session_t session,
    257                                 audio_io_handle_t src_output,
    258                                 audio_io_handle_t dst_output)
    259 {
    260     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
    261     if (af == 0) {
    262         return PERMISSION_DENIED;
    263     }
    264 
    265     return af->moveEffects(session, src_output, dst_output);
    266 }
    267 
    268 char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
    269                                      const char *keys)
    270 {
    271     String8 result = AudioSystem::getParameters(io_handle, String8(keys));
    272     return strdup(result.string());
    273 }
    274 
    275 void aps_set_parameters(void *service, audio_io_handle_t io_handle,
    276                                    const char *kv_pairs, int delay_ms)
    277 {
    278     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
    279 
    280     audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
    281 }
    282 
    283 int aps_set_stream_volume(void *service, audio_stream_type_t stream,
    284                                      float volume, audio_io_handle_t output,
    285                                      int delay_ms)
    286 {
    287     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
    288 
    289     return audioPolicyService->setStreamVolume(stream, volume, output,
    290                                                delay_ms);
    291 }
    292 
    293 int aps_start_tone(void *service, audio_policy_tone_t tone,
    294                               audio_stream_type_t stream)
    295 {
    296     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
    297 
    298     return audioPolicyService->startTone(tone, stream);
    299 }
    300 
    301 int aps_stop_tone(void *service)
    302 {
    303     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
    304 
    305     return audioPolicyService->stopTone();
    306 }
    307 
    308 int aps_set_voice_volume(void *service, float volume, int delay_ms)
    309 {
    310     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
    311 
    312     return audioPolicyService->setVoiceVolume(volume, delay_ms);
    313 }
    314 
    315 }; // extern "C"
    316 
    317 }; // namespace android
    318