diff options
author | Mikhail Naganov <mnaganov@google.com> | 2021-01-15 19:05:04 +0000 |
---|---|---|
committer | Mikhail Naganov <mnaganov@google.com> | 2021-02-02 18:17:26 +0000 |
commit | 5ec48c2d4d7a8ccdea489ad4b34a7cf75480ffcc (patch) | |
tree | e1458ca90fba3abd0f427da2f65fc0d52ecc5d35 /audio/effect/all-versions/default/util/EffectUtils.cpp | |
parent | 4f5cb710b92fd58338630e2f54f03cac0468b3f8 (diff) |
audio: Create libraries for data types in core and effect
Add 'CoreUtils' library similar to 'HidlUtils' for the types
specific to the core HAL. Add 'EffectUtils' library similar to
'HidlUtils' for the types specific to the effects HAL. Move into
them and de-duplicate code previously scattered across the
default HAL implementation and libaudiohal. Add unit tests.
Removed 'AUDIO_{INPUT|OUTPUT}_FLAG_NONE' from the list of
values in the XSD file to avoid additional complexity due to
equivalence of this value to an empty list of flags.
Bug: 142480271
Test: m android.hardware.audio@X.0-impl
Test: m android.hardware.audio.effect@X.0-impl
Test: atest android.hardware.audio@7.0-util_tests
Test: atest android.hardware.audio.common@7.0-util_tests
Test: atest android.hardware.audio.effect@7.0-util_tests
Change-Id: I71a95cbe07fcc162dc6d74ff9665747a17ce5a80
Diffstat (limited to 'audio/effect/all-versions/default/util/EffectUtils.cpp')
-rw-r--r-- | audio/effect/all-versions/default/util/EffectUtils.cpp | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/audio/effect/all-versions/default/util/EffectUtils.cpp b/audio/effect/all-versions/default/util/EffectUtils.cpp new file mode 100644 index 0000000000..1c0419aba3 --- /dev/null +++ b/audio/effect/all-versions/default/util/EffectUtils.cpp @@ -0,0 +1,183 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <memory.h> + +#include <HidlUtils.h> +#include <UuidUtils.h> +#include <common/all-versions/VersionUtils.h> + +#include "util/EffectUtils.h" + +using ::android::hardware::audio::common::CPP_VERSION::implementation::HidlUtils; +using ::android::hardware::audio::common::CPP_VERSION::implementation::UuidUtils; +using ::android::hardware::audio::common::utils::EnumBitfield; + +namespace android { +namespace hardware { +namespace audio { +namespace effect { +namespace CPP_VERSION { +namespace implementation { + +using namespace ::android::hardware::audio::common::CPP_VERSION; + +#define CONVERT_CHECKED(expr, result) \ + if (status_t status = (expr); status != NO_ERROR) { \ + result = status; \ + } + +#if MAJOR_VERSION <= 6 + +status_t EffectUtils::effectBufferConfigFromHal(const buffer_config_t& halConfig, bool /*isInput*/, + EffectBufferConfig* config) { + config->buffer.id = 0; + config->buffer.frameCount = 0; + config->samplingRateHz = halConfig.samplingRate; + config->channels = EnumBitfield<AudioChannelMask>(halConfig.channels); + config->format = AudioFormat(halConfig.format); + config->accessMode = EffectBufferAccess(halConfig.accessMode); + config->mask = EnumBitfield<EffectConfigParameters>(halConfig.mask); + return NO_ERROR; +} + +status_t EffectUtils::effectBufferConfigToHal(const EffectBufferConfig& config, + buffer_config_t* halConfig) { + // Note: setting the buffers directly is considered obsolete. They need to be set + // using 'setProcessBuffers'. + halConfig->buffer.frameCount = 0; + halConfig->buffer.raw = nullptr; + halConfig->samplingRate = config.samplingRateHz; + halConfig->channels = static_cast<uint32_t>(config.channels); + // Note: The framework code does not use BP. + halConfig->bufferProvider.cookie = nullptr; + halConfig->bufferProvider.getBuffer = nullptr; + halConfig->bufferProvider.releaseBuffer = nullptr; + halConfig->format = static_cast<uint8_t>(config.format); + halConfig->accessMode = static_cast<uint8_t>(config.accessMode); + halConfig->mask = static_cast<uint8_t>(config.mask); + return NO_ERROR; +} + +#else + +status_t EffectUtils::effectBufferConfigFromHal(const buffer_config_t& halConfig, bool isInput, + EffectBufferConfig* config) { + status_t result = NO_ERROR; + config->buffer.unspecified(); + audio_config_base_t halConfigBase = {halConfig.samplingRate, + static_cast<audio_channel_mask_t>(halConfig.channels), + static_cast<audio_format_t>(halConfig.format)}; + CONVERT_CHECKED(HidlUtils::audioConfigBaseOptionalFromHal( + halConfigBase, isInput, halConfig.mask & EFFECT_CONFIG_FORMAT, + halConfig.mask & EFFECT_CONFIG_SMP_RATE, + halConfig.mask & EFFECT_CONFIG_CHANNELS, &config->base), + result); + if (halConfig.mask & EFFECT_CONFIG_ACC_MODE) { + config->accessMode.value(EffectBufferAccess(halConfig.accessMode)); + } + return result; +} + +status_t EffectUtils::effectBufferConfigToHal(const EffectBufferConfig& config, + buffer_config_t* halConfig) { + status_t result = NO_ERROR; + // Note: setting the buffers directly is considered obsolete. They need to be set + // using 'setProcessBuffers'. + halConfig->buffer.frameCount = 0; + halConfig->buffer.raw = nullptr; + audio_config_base_t halConfigBase = AUDIO_CONFIG_BASE_INITIALIZER; + bool formatSpecified = false, sRateSpecified = false, channelMaskSpecified = false; + CONVERT_CHECKED( + HidlUtils::audioConfigBaseOptionalToHal(config.base, &halConfigBase, &formatSpecified, + &sRateSpecified, &channelMaskSpecified), + result); + halConfig->mask = 0; + if (sRateSpecified) { + halConfig->mask |= EFFECT_CONFIG_SMP_RATE; + halConfig->samplingRate = halConfigBase.sample_rate; + } + if (channelMaskSpecified) { + halConfig->mask |= EFFECT_CONFIG_CHANNELS; + halConfig->channels = halConfigBase.channel_mask; + } + if (formatSpecified) { + halConfig->mask |= EFFECT_CONFIG_FORMAT; + halConfig->format = halConfigBase.format; + } + // Note: The framework code does not use BP. + halConfig->bufferProvider.cookie = nullptr; + halConfig->bufferProvider.getBuffer = nullptr; + halConfig->bufferProvider.releaseBuffer = nullptr; + if (config.accessMode.getDiscriminator() == + EffectBufferConfig::OptionalAccessMode::hidl_discriminator::value) { + halConfig->mask |= EFFECT_CONFIG_ACC_MODE; + halConfig->accessMode = static_cast<uint8_t>(config.accessMode.value()); + } + return result; +} + +#endif // MAJOR_VERSION >= 6 + +status_t EffectUtils::effectConfigFromHal(const effect_config_t& halConfig, bool isInput, + EffectConfig* config) { + status_t result = NO_ERROR; + CONVERT_CHECKED(effectBufferConfigFromHal(halConfig.inputCfg, isInput, &config->inputCfg), + result); + CONVERT_CHECKED(effectBufferConfigFromHal(halConfig.outputCfg, isInput, &config->outputCfg), + result); + return result; +} + +status_t EffectUtils::effectConfigToHal(const EffectConfig& config, effect_config_t* halConfig) { + status_t result = NO_ERROR; + CONVERT_CHECKED(effectBufferConfigToHal(config.inputCfg, &halConfig->inputCfg), result); + CONVERT_CHECKED(effectBufferConfigToHal(config.outputCfg, &halConfig->outputCfg), result); + return result; +} + +status_t EffectUtils::effectDescriptorFromHal(const effect_descriptor_t& halDescriptor, + EffectDescriptor* descriptor) { + UuidUtils::uuidFromHal(halDescriptor.type, &descriptor->type); + UuidUtils::uuidFromHal(halDescriptor.uuid, &descriptor->uuid); + descriptor->flags = EnumBitfield<EffectFlags>(halDescriptor.flags); + descriptor->cpuLoad = halDescriptor.cpuLoad; + descriptor->memoryUsage = halDescriptor.memoryUsage; + memcpy(descriptor->name.data(), halDescriptor.name, descriptor->name.size()); + memcpy(descriptor->implementor.data(), halDescriptor.implementor, + descriptor->implementor.size()); + return NO_ERROR; +} + +status_t EffectUtils::effectDescriptorToHal(const EffectDescriptor& descriptor, + effect_descriptor_t* halDescriptor) { + UuidUtils::uuidToHal(descriptor.type, &halDescriptor->type); + UuidUtils::uuidToHal(descriptor.uuid, &halDescriptor->uuid); + halDescriptor->flags = static_cast<uint32_t>(descriptor.flags); + halDescriptor->cpuLoad = descriptor.cpuLoad; + halDescriptor->memoryUsage = descriptor.memoryUsage; + memcpy(halDescriptor->name, descriptor.name.data(), descriptor.name.size()); + memcpy(halDescriptor->implementor, descriptor.implementor.data(), + descriptor.implementor.size()); + return NO_ERROR; +} + +} // namespace implementation +} // namespace CPP_VERSION +} // namespace effect +} // namespace audio +} // namespace hardware +} // namespace android |