Home | History | Annotate | Download | only in audio
      1 /*
      2  * Copyright (C) 2011 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 "legacy_audio_policy_hal"
     18 //#define LOG_NDEBUG 0
     19 
     20 #include <stdint.h>
     21 
     22 #include <hardware/hardware.h>
     23 #include <system/audio.h>
     24 #include <system/audio_policy.h>
     25 #include <hardware/audio_policy.h>
     26 
     27 #include <hardware_legacy/AudioPolicyInterface.h>
     28 #include <hardware_legacy/AudioSystemLegacy.h>
     29 
     30 #include "AudioPolicyCompatClient.h"
     31 
     32 namespace android_audio_legacy {
     33 
     34 extern "C" {
     35 
     36 struct legacy_ap_module {
     37     struct audio_policy_module module;
     38 };
     39 
     40 struct legacy_ap_device {
     41     struct audio_policy_device device;
     42 };
     43 
     44 struct legacy_audio_policy {
     45     struct audio_policy policy;
     46 
     47     void *service;
     48     struct audio_policy_service_ops *aps_ops;
     49     AudioPolicyCompatClient *service_client;
     50     AudioPolicyInterface *apm;
     51 };
     52 
     53 static inline struct legacy_audio_policy * to_lap(struct audio_policy *pol)
     54 {
     55     return reinterpret_cast<struct legacy_audio_policy *>(pol);
     56 }
     57 
     58 static inline const struct legacy_audio_policy * to_clap(const struct audio_policy *pol)
     59 {
     60     return reinterpret_cast<const struct legacy_audio_policy *>(pol);
     61 }
     62 
     63 
     64 static int ap_set_device_connection_state(struct audio_policy *pol,
     65                                           audio_devices_t device,
     66                                           audio_policy_dev_state_t state,
     67                                           const char *device_address)
     68 {
     69     struct legacy_audio_policy *lap = to_lap(pol);
     70     return lap->apm->setDeviceConnectionState(
     71                     (AudioSystem::audio_devices)device,
     72                     (AudioSystem::device_connection_state)state,
     73                     device_address);
     74 }
     75 
     76 static audio_policy_dev_state_t ap_get_device_connection_state(
     77                                             const struct audio_policy *pol,
     78                                             audio_devices_t device,
     79                                             const char *device_address)
     80 {
     81     const struct legacy_audio_policy *lap = to_clap(pol);
     82     return (audio_policy_dev_state_t)lap->apm->getDeviceConnectionState(
     83                     (AudioSystem::audio_devices)device,
     84                     device_address);
     85 }
     86 
     87 static void ap_set_phone_state(struct audio_policy *pol, audio_mode_t state)
     88 {
     89     struct legacy_audio_policy *lap = to_lap(pol);
     90     // as this is the legacy API, don't change it to use audio_mode_t instead of int
     91     lap->apm->setPhoneState((int) state);
     92 }
     93 
     94     /* indicate a change in ringer mode */
     95 static void ap_set_ringer_mode(struct audio_policy *pol, uint32_t mode,
     96                                uint32_t mask)
     97 {
     98     // deprecated, never called
     99 }
    100 
    101     /* force using a specific device category for the specified usage */
    102 static void ap_set_force_use(struct audio_policy *pol,
    103                           audio_policy_force_use_t usage,
    104                           audio_policy_forced_cfg_t config)
    105 {
    106     struct legacy_audio_policy *lap = to_lap(pol);
    107     lap->apm->setForceUse((AudioSystem::force_use)usage,
    108                           (AudioSystem::forced_config)config);
    109 }
    110 
    111     /* retreive current device category forced for a given usage */
    112 static audio_policy_forced_cfg_t ap_get_force_use(
    113                                                const struct audio_policy *pol,
    114                                                audio_policy_force_use_t usage)
    115 {
    116     const struct legacy_audio_policy *lap = to_clap(pol);
    117     return (audio_policy_forced_cfg_t)lap->apm->getForceUse(
    118                           (AudioSystem::force_use)usage);
    119 }
    120 
    121 /* if can_mute is true, then audio streams that are marked ENFORCED_AUDIBLE
    122  * can still be muted. */
    123 static void ap_set_can_mute_enforced_audible(struct audio_policy *pol,
    124                                              bool can_mute)
    125 {
    126     struct legacy_audio_policy *lap = to_lap(pol);
    127     lap->apm->setSystemProperty("ro.camera.sound.forced", can_mute ? "0" : "1");
    128 }
    129 
    130 static int ap_init_check(const struct audio_policy *pol)
    131 {
    132     const struct legacy_audio_policy *lap = to_clap(pol);
    133     return lap->apm->initCheck();
    134 }
    135 
    136 static audio_io_handle_t ap_get_output(struct audio_policy *pol,
    137                                        audio_stream_type_t stream,
    138                                        uint32_t sampling_rate,
    139                                        audio_format_t format,
    140                                        audio_channel_mask_t channelMask,
    141                                        audio_output_flags_t flags,
    142                                        const audio_offload_info_t *offloadInfo)
    143 {
    144     struct legacy_audio_policy *lap = to_lap(pol);
    145 
    146     ALOGV("%s: tid %d", __func__, gettid());
    147     return lap->apm->getOutput((AudioSystem::stream_type)stream,
    148                                sampling_rate, format, channelMask,
    149                                (AudioSystem::output_flags)flags,
    150                                offloadInfo);
    151 }
    152 
    153 static int ap_start_output(struct audio_policy *pol, audio_io_handle_t output,
    154                            audio_stream_type_t stream, int session)
    155 {
    156     struct legacy_audio_policy *lap = to_lap(pol);
    157     return lap->apm->startOutput(output, (AudioSystem::stream_type)stream,
    158                                  session);
    159 }
    160 
    161 static int ap_stop_output(struct audio_policy *pol, audio_io_handle_t output,
    162                           audio_stream_type_t stream, int session)
    163 {
    164     struct legacy_audio_policy *lap = to_lap(pol);
    165     return lap->apm->stopOutput(output, (AudioSystem::stream_type)stream,
    166                                 session);
    167 }
    168 
    169 static void ap_release_output(struct audio_policy *pol,
    170                               audio_io_handle_t output)
    171 {
    172     struct legacy_audio_policy *lap = to_lap(pol);
    173     lap->apm->releaseOutput(output);
    174 }
    175 
    176 static audio_io_handle_t ap_get_input(struct audio_policy *pol, audio_source_t inputSource,
    177                                       uint32_t sampling_rate,
    178                                       audio_format_t format,
    179                                       audio_channel_mask_t channelMask,
    180                                       audio_in_acoustics_t acoustics)
    181 {
    182     struct legacy_audio_policy *lap = to_lap(pol);
    183     return lap->apm->getInput((int) inputSource, sampling_rate, format, channelMask,
    184                               (AudioSystem::audio_in_acoustics)acoustics);
    185 }
    186 
    187 static int ap_start_input(struct audio_policy *pol, audio_io_handle_t input)
    188 {
    189     struct legacy_audio_policy *lap = to_lap(pol);
    190     return lap->apm->startInput(input);
    191 }
    192 
    193 static int ap_stop_input(struct audio_policy *pol, audio_io_handle_t input)
    194 {
    195     struct legacy_audio_policy *lap = to_lap(pol);
    196     return lap->apm->stopInput(input);
    197 }
    198 
    199 static void ap_release_input(struct audio_policy *pol, audio_io_handle_t input)
    200 {
    201     struct legacy_audio_policy *lap = to_lap(pol);
    202     lap->apm->releaseInput(input);
    203 }
    204 
    205 static void ap_init_stream_volume(struct audio_policy *pol,
    206                                   audio_stream_type_t stream, int index_min,
    207                                   int index_max)
    208 {
    209     struct legacy_audio_policy *lap = to_lap(pol);
    210     lap->apm->initStreamVolume((AudioSystem::stream_type)stream, index_min,
    211                                index_max);
    212 }
    213 
    214 static int ap_set_stream_volume_index(struct audio_policy *pol,
    215                                       audio_stream_type_t stream,
    216                                       int index)
    217 {
    218     struct legacy_audio_policy *lap = to_lap(pol);
    219     return lap->apm->setStreamVolumeIndex((AudioSystem::stream_type)stream,
    220                                           index,
    221                                           AUDIO_DEVICE_OUT_DEFAULT);
    222 }
    223 
    224 static int ap_get_stream_volume_index(const struct audio_policy *pol,
    225                                       audio_stream_type_t stream,
    226                                       int *index)
    227 {
    228     const struct legacy_audio_policy *lap = to_clap(pol);
    229     return lap->apm->getStreamVolumeIndex((AudioSystem::stream_type)stream,
    230                                           index,
    231                                           AUDIO_DEVICE_OUT_DEFAULT);
    232 }
    233 
    234 static int ap_set_stream_volume_index_for_device(struct audio_policy *pol,
    235                                       audio_stream_type_t stream,
    236                                       int index,
    237                                       audio_devices_t device)
    238 {
    239     struct legacy_audio_policy *lap = to_lap(pol);
    240     return lap->apm->setStreamVolumeIndex((AudioSystem::stream_type)stream,
    241                                           index,
    242                                           device);
    243 }
    244 
    245 static int ap_get_stream_volume_index_for_device(const struct audio_policy *pol,
    246                                       audio_stream_type_t stream,
    247                                       int *index,
    248                                       audio_devices_t device)
    249 {
    250     const struct legacy_audio_policy *lap = to_clap(pol);
    251     return lap->apm->getStreamVolumeIndex((AudioSystem::stream_type)stream,
    252                                           index,
    253                                           device);
    254 }
    255 
    256 static uint32_t ap_get_strategy_for_stream(const struct audio_policy *pol,
    257                                            audio_stream_type_t stream)
    258 {
    259     const struct legacy_audio_policy *lap = to_clap(pol);
    260     return lap->apm->getStrategyForStream((AudioSystem::stream_type)stream);
    261 }
    262 
    263 static audio_devices_t ap_get_devices_for_stream(const struct audio_policy *pol,
    264                                        audio_stream_type_t stream)
    265 {
    266     const struct legacy_audio_policy *lap = to_clap(pol);
    267     return lap->apm->getDevicesForStream((AudioSystem::stream_type)stream);
    268 }
    269 
    270 static audio_io_handle_t ap_get_output_for_effect(struct audio_policy *pol,
    271                                             const struct effect_descriptor_s *desc)
    272 {
    273     struct legacy_audio_policy *lap = to_lap(pol);
    274     return lap->apm->getOutputForEffect(desc);
    275 }
    276 
    277 static int ap_register_effect(struct audio_policy *pol,
    278                               const struct effect_descriptor_s *desc,
    279                               audio_io_handle_t io,
    280                               uint32_t strategy,
    281                               int session,
    282                               int id)
    283 {
    284     struct legacy_audio_policy *lap = to_lap(pol);
    285     return lap->apm->registerEffect(desc, io, strategy, session, id);
    286 }
    287 
    288 static int ap_unregister_effect(struct audio_policy *pol, int id)
    289 {
    290     struct legacy_audio_policy *lap = to_lap(pol);
    291     return lap->apm->unregisterEffect(id);
    292 }
    293 
    294 static int ap_set_effect_enabled(struct audio_policy *pol, int id, bool enabled)
    295 {
    296     struct legacy_audio_policy *lap = to_lap(pol);
    297     return lap->apm->setEffectEnabled(id, enabled);
    298 }
    299 
    300 static bool ap_is_stream_active(const struct audio_policy *pol, audio_stream_type_t stream,
    301                                 uint32_t in_past_ms)
    302 {
    303     const struct legacy_audio_policy *lap = to_clap(pol);
    304     return lap->apm->isStreamActive((int) stream, in_past_ms);
    305 }
    306 
    307 static bool ap_is_stream_active_remotely(const struct audio_policy *pol, audio_stream_type_t stream,
    308                                 uint32_t in_past_ms)
    309 {
    310     const struct legacy_audio_policy *lap = to_clap(pol);
    311     return lap->apm->isStreamActiveRemotely((int) stream, in_past_ms);
    312 }
    313 
    314 static bool ap_is_source_active(const struct audio_policy *pol, audio_source_t source)
    315 {
    316     const struct legacy_audio_policy *lap = to_clap(pol);
    317     return lap->apm->isSourceActive(source);
    318 }
    319 
    320 static int ap_dump(const struct audio_policy *pol, int fd)
    321 {
    322     const struct legacy_audio_policy *lap = to_clap(pol);
    323     return lap->apm->dump(fd);
    324 }
    325 
    326 static bool ap_is_offload_supported(const struct audio_policy *pol,
    327                                     const audio_offload_info_t *info)
    328 {
    329     const struct legacy_audio_policy *lap = to_clap(pol);
    330     return lap->apm->isOffloadSupported(*info);
    331 }
    332 
    333 static int create_legacy_ap(const struct audio_policy_device *device,
    334                             struct audio_policy_service_ops *aps_ops,
    335                             void *service,
    336                             struct audio_policy **ap)
    337 {
    338     struct legacy_audio_policy *lap;
    339     int ret;
    340 
    341     if (!service || !aps_ops)
    342         return -EINVAL;
    343 
    344     lap = (struct legacy_audio_policy *)calloc(1, sizeof(*lap));
    345     if (!lap)
    346         return -ENOMEM;
    347 
    348     lap->policy.set_device_connection_state = ap_set_device_connection_state;
    349     lap->policy.get_device_connection_state = ap_get_device_connection_state;
    350     lap->policy.set_phone_state = ap_set_phone_state;
    351     lap->policy.set_ringer_mode = ap_set_ringer_mode;
    352     lap->policy.set_force_use = ap_set_force_use;
    353     lap->policy.get_force_use = ap_get_force_use;
    354     lap->policy.set_can_mute_enforced_audible =
    355         ap_set_can_mute_enforced_audible;
    356     lap->policy.init_check = ap_init_check;
    357     lap->policy.get_output = ap_get_output;
    358     lap->policy.start_output = ap_start_output;
    359     lap->policy.stop_output = ap_stop_output;
    360     lap->policy.release_output = ap_release_output;
    361     lap->policy.get_input = ap_get_input;
    362     lap->policy.start_input = ap_start_input;
    363     lap->policy.stop_input = ap_stop_input;
    364     lap->policy.release_input = ap_release_input;
    365     lap->policy.init_stream_volume = ap_init_stream_volume;
    366     lap->policy.set_stream_volume_index = ap_set_stream_volume_index;
    367     lap->policy.get_stream_volume_index = ap_get_stream_volume_index;
    368     lap->policy.set_stream_volume_index_for_device = ap_set_stream_volume_index_for_device;
    369     lap->policy.get_stream_volume_index_for_device = ap_get_stream_volume_index_for_device;
    370     lap->policy.get_strategy_for_stream = ap_get_strategy_for_stream;
    371     lap->policy.get_devices_for_stream = ap_get_devices_for_stream;
    372     lap->policy.get_output_for_effect = ap_get_output_for_effect;
    373     lap->policy.register_effect = ap_register_effect;
    374     lap->policy.unregister_effect = ap_unregister_effect;
    375     lap->policy.set_effect_enabled = ap_set_effect_enabled;
    376     lap->policy.is_stream_active = ap_is_stream_active;
    377     lap->policy.is_stream_active_remotely = ap_is_stream_active_remotely;
    378     lap->policy.is_source_active = ap_is_source_active;
    379     lap->policy.dump = ap_dump;
    380     lap->policy.is_offload_supported = ap_is_offload_supported;
    381 
    382     lap->service = service;
    383     lap->aps_ops = aps_ops;
    384     lap->service_client =
    385         new AudioPolicyCompatClient(aps_ops, service);
    386     if (!lap->service_client) {
    387         ret = -ENOMEM;
    388         goto err_new_compat_client;
    389     }
    390 
    391     lap->apm = createAudioPolicyManager(lap->service_client);
    392     if (!lap->apm) {
    393         ret = -ENOMEM;
    394         goto err_create_apm;
    395     }
    396 
    397     *ap = &lap->policy;
    398     return 0;
    399 
    400 err_create_apm:
    401     delete lap->service_client;
    402 err_new_compat_client:
    403     free(lap);
    404     *ap = NULL;
    405     return ret;
    406 }
    407 
    408 static int destroy_legacy_ap(const struct audio_policy_device *ap_dev,
    409                              struct audio_policy *ap)
    410 {
    411     struct legacy_audio_policy *lap = to_lap(ap);
    412 
    413     if (!lap)
    414         return 0;
    415 
    416     if (lap->apm)
    417         destroyAudioPolicyManager(lap->apm);
    418     if (lap->service_client)
    419         delete lap->service_client;
    420     free(lap);
    421     return 0;
    422 }
    423 
    424 static int legacy_ap_dev_close(hw_device_t* device)
    425 {
    426     if (device)
    427         free(device);
    428     return 0;
    429 }
    430 
    431 static int legacy_ap_dev_open(const hw_module_t* module, const char* name,
    432                                     hw_device_t** device)
    433 {
    434     struct legacy_ap_device *dev;
    435 
    436     if (strcmp(name, AUDIO_POLICY_INTERFACE) != 0)
    437         return -EINVAL;
    438 
    439     dev = (struct legacy_ap_device *)calloc(1, sizeof(*dev));
    440     if (!dev)
    441         return -ENOMEM;
    442 
    443     dev->device.common.tag = HARDWARE_DEVICE_TAG;
    444     dev->device.common.version = 0;
    445     dev->device.common.module = const_cast<hw_module_t*>(module);
    446     dev->device.common.close = legacy_ap_dev_close;
    447     dev->device.create_audio_policy = create_legacy_ap;
    448     dev->device.destroy_audio_policy = destroy_legacy_ap;
    449 
    450     *device = &dev->device.common;
    451 
    452     return 0;
    453 }
    454 
    455 static struct hw_module_methods_t legacy_ap_module_methods = {
    456         .open = legacy_ap_dev_open
    457 };
    458 
    459 struct legacy_ap_module HAL_MODULE_INFO_SYM = {
    460     .module = {
    461         .common = {
    462             .tag = HARDWARE_MODULE_TAG,
    463             .version_major = 1,
    464             .version_minor = 0,
    465             .id = AUDIO_POLICY_HARDWARE_MODULE_ID,
    466             .name = "LEGACY Audio Policy HAL",
    467             .author = "The Android Open Source Project",
    468             .methods = &legacy_ap_module_methods,
    469             .dso = NULL,
    470             .reserved = {0},
    471         },
    472     },
    473 };
    474 
    475 }; // extern "C"
    476 
    477 }; // namespace android_audio_legacy
    478