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_t)0, 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()) {
    194         ALOGE("open_input() permission denied: capture not allowed");
    195         return AUDIO_IO_HANDLE_NONE;
    196     }
    197 
    198     audio_config_t config = AUDIO_CONFIG_INITIALIZER;;
    199     config.sample_rate = *pSamplingRate;
    200     config.format = *pFormat;
    201     config.channel_mask = *pChannelMask;
    202     audio_io_handle_t input = AUDIO_IO_HANDLE_NONE;
    203     status_t status = af->openInput(module, &input, &config, pDevices,
    204                                     String8(""), AUDIO_SOURCE_MIC, AUDIO_INPUT_FLAG_FAST /*FIXME*/);
    205     if (status == NO_ERROR) {
    206         *pSamplingRate = config.sample_rate;
    207         *pFormat = config.format;
    208         *pChannelMask = config.channel_mask;
    209     }
    210     return input;
    211 }
    212 
    213 
    214 // deprecated: replaced by aps_open_input_on_module(), and acoustics parameter is ignored
    215 audio_io_handle_t aps_open_input(void *service __unused,
    216                                         audio_devices_t *pDevices,
    217                                         uint32_t *pSamplingRate,
    218                                         audio_format_t *pFormat,
    219                                         audio_channel_mask_t *pChannelMask,
    220                                         audio_in_acoustics_t acoustics __unused)
    221 {
    222     return  open_input((audio_module_handle_t)0, pDevices, pSamplingRate, pFormat, pChannelMask);
    223 }
    224 
    225 audio_io_handle_t aps_open_input_on_module(void *service __unused,
    226                                                   audio_module_handle_t module,
    227                                                   audio_devices_t *pDevices,
    228                                                   uint32_t *pSamplingRate,
    229                                                   audio_format_t *pFormat,
    230                                                   audio_channel_mask_t *pChannelMask)
    231 {
    232     return  open_input(module, pDevices, pSamplingRate, pFormat, pChannelMask);
    233 }
    234 
    235 int aps_close_input(void *service __unused, audio_io_handle_t input)
    236 {
    237     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
    238     if (af == 0) {
    239         return PERMISSION_DENIED;
    240     }
    241 
    242     return af->closeInput(input);
    243 }
    244 
    245 int aps_invalidate_stream(void *service __unused, audio_stream_type_t stream)
    246 {
    247     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
    248     if (af == 0) {
    249         return PERMISSION_DENIED;
    250     }
    251 
    252     return af->invalidateStream(stream);
    253 }
    254 
    255 int aps_move_effects(void *service __unused, int session,
    256                                 audio_io_handle_t src_output,
    257                                 audio_io_handle_t dst_output)
    258 {
    259     sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
    260     if (af == 0) {
    261         return PERMISSION_DENIED;
    262     }
    263 
    264     return af->moveEffects(session, src_output, dst_output);
    265 }
    266 
    267 char * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
    268                                      const char *keys)
    269 {
    270     String8 result = AudioSystem::getParameters(io_handle, String8(keys));
    271     return strdup(result.string());
    272 }
    273 
    274 void aps_set_parameters(void *service, audio_io_handle_t io_handle,
    275                                    const char *kv_pairs, int delay_ms)
    276 {
    277     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
    278 
    279     audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
    280 }
    281 
    282 int aps_set_stream_volume(void *service, audio_stream_type_t stream,
    283                                      float volume, audio_io_handle_t output,
    284                                      int delay_ms)
    285 {
    286     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
    287 
    288     return audioPolicyService->setStreamVolume(stream, volume, output,
    289                                                delay_ms);
    290 }
    291 
    292 int aps_start_tone(void *service, audio_policy_tone_t tone,
    293                               audio_stream_type_t stream)
    294 {
    295     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
    296 
    297     return audioPolicyService->startTone(tone, stream);
    298 }
    299 
    300 int aps_stop_tone(void *service)
    301 {
    302     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
    303 
    304     return audioPolicyService->stopTone();
    305 }
    306 
    307 int aps_set_voice_volume(void *service, float volume, int delay_ms)
    308 {
    309     AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
    310 
    311     return audioPolicyService->setVoiceVolume(volume, delay_ms);
    312 }
    313 
    314 }; // extern "C"
    315 
    316 }; // namespace android
    317