• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include <memory.h>
18 
19 #define LOG_TAG "EffectUtils"
20 #include <log/log.h>
21 
22 #include <HidlUtils.h>
23 #include <UuidUtils.h>
24 #include <common/all-versions/VersionUtils.h>
25 
26 #include "util/EffectUtils.h"
27 
28 using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::HidlUtils;
29 using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::UuidUtils;
30 using ::android::hardware::audio::common::utils::EnumBitfield;
31 
32 namespace android {
33 namespace hardware {
34 namespace audio {
35 namespace effect {
36 namespace CPP_VERSION {
37 namespace implementation {
38 
39 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION;
40 
41 #define CONVERT_CHECKED(expr, result)                   \
42     if (status_t status = (expr); status != NO_ERROR) { \
43         result = status;                                \
44     }
45 
46 #if MAJOR_VERSION <= 6
47 
effectBufferConfigFromHal(const buffer_config_t & halConfig,bool,EffectBufferConfig * config)48 status_t EffectUtils::effectBufferConfigFromHal(const buffer_config_t& halConfig, bool /*isInput*/,
49                                                 EffectBufferConfig* config) {
50     config->buffer.id = 0;
51     config->buffer.frameCount = 0;
52     config->samplingRateHz = halConfig.samplingRate;
53     config->channels = EnumBitfield<AudioChannelMask>(halConfig.channels);
54     config->format = AudioFormat(halConfig.format);
55     config->accessMode = EffectBufferAccess(halConfig.accessMode);
56     config->mask = EnumBitfield<EffectConfigParameters>(halConfig.mask);
57     return NO_ERROR;
58 }
59 
effectBufferConfigToHal(const EffectBufferConfig & config,buffer_config_t * halConfig)60 status_t EffectUtils::effectBufferConfigToHal(const EffectBufferConfig& config,
61                                               buffer_config_t* halConfig) {
62     // Note: setting the buffers directly is considered obsolete. They need to be set
63     // using 'setProcessBuffers'.
64     halConfig->buffer.frameCount = 0;
65     halConfig->buffer.raw = nullptr;
66     halConfig->samplingRate = config.samplingRateHz;
67     halConfig->channels = static_cast<uint32_t>(config.channels);
68     // Note: The framework code does not use BP.
69     halConfig->bufferProvider.cookie = nullptr;
70     halConfig->bufferProvider.getBuffer = nullptr;
71     halConfig->bufferProvider.releaseBuffer = nullptr;
72     halConfig->format = static_cast<uint8_t>(config.format);
73     halConfig->accessMode = static_cast<uint8_t>(config.accessMode);
74     halConfig->mask = static_cast<uint8_t>(config.mask);
75     return NO_ERROR;
76 }
77 
78 #else
79 
effectBufferConfigFromHal(const buffer_config_t & halConfig,bool isInput,EffectBufferConfig * config)80 status_t EffectUtils::effectBufferConfigFromHal(const buffer_config_t& halConfig, bool isInput,
81                                                 EffectBufferConfig* config) {
82     status_t result = NO_ERROR;
83     config->buffer.unspecified();
84     audio_config_base_t halConfigBase = {halConfig.samplingRate,
85                                          static_cast<audio_channel_mask_t>(halConfig.channels),
86                                          static_cast<audio_format_t>(halConfig.format)};
87     CONVERT_CHECKED(HidlUtils::audioConfigBaseOptionalFromHal(
88                             halConfigBase, isInput, halConfig.mask & EFFECT_CONFIG_FORMAT,
89                             halConfig.mask & EFFECT_CONFIG_SMP_RATE,
90                             halConfig.mask & EFFECT_CONFIG_CHANNELS, &config->base),
91                     result);
92     if (halConfig.mask & EFFECT_CONFIG_ACC_MODE) {
93         config->accessMode.value(EffectBufferAccess(halConfig.accessMode));
94     }
95     return result;
96 }
97 
effectBufferConfigToHal(const EffectBufferConfig & config,buffer_config_t * halConfig)98 status_t EffectUtils::effectBufferConfigToHal(const EffectBufferConfig& config,
99                                               buffer_config_t* halConfig) {
100     status_t result = NO_ERROR;
101     // Note: setting the buffers directly is considered obsolete. They need to be set
102     // using 'setProcessBuffers'.
103     halConfig->buffer.frameCount = 0;
104     halConfig->buffer.raw = nullptr;
105     audio_config_base_t halConfigBase = AUDIO_CONFIG_BASE_INITIALIZER;
106     bool formatSpecified = false, sRateSpecified = false, channelMaskSpecified = false;
107     CONVERT_CHECKED(
108             HidlUtils::audioConfigBaseOptionalToHal(config.base, &halConfigBase, &formatSpecified,
109                                                     &sRateSpecified, &channelMaskSpecified),
110             result);
111     halConfig->mask = 0;
112     if (sRateSpecified) {
113         halConfig->mask |= EFFECT_CONFIG_SMP_RATE;
114         halConfig->samplingRate = halConfigBase.sample_rate;
115     }
116     if (channelMaskSpecified) {
117         halConfig->mask |= EFFECT_CONFIG_CHANNELS;
118         halConfig->channels = halConfigBase.channel_mask;
119     }
120     if (formatSpecified) {
121         halConfig->mask |= EFFECT_CONFIG_FORMAT;
122         halConfig->format = halConfigBase.format;
123     }
124     // Note: The framework code does not use BP.
125     halConfig->bufferProvider.cookie = nullptr;
126     halConfig->bufferProvider.getBuffer = nullptr;
127     halConfig->bufferProvider.releaseBuffer = nullptr;
128     if (config.accessMode.getDiscriminator() ==
129         EffectBufferConfig::OptionalAccessMode::hidl_discriminator::value) {
130         halConfig->mask |= EFFECT_CONFIG_ACC_MODE;
131         halConfig->accessMode = static_cast<uint8_t>(config.accessMode.value());
132     }
133     return result;
134 }
135 
136 #endif  // MAJOR_VERSION >= 6
137 
effectConfigFromHal(const effect_config_t & halConfig,bool isInput,EffectConfig * config)138 status_t EffectUtils::effectConfigFromHal(const effect_config_t& halConfig, bool isInput,
139                                           EffectConfig* config) {
140     status_t result = NO_ERROR;
141     CONVERT_CHECKED(effectBufferConfigFromHal(halConfig.inputCfg, isInput, &config->inputCfg),
142                     result);
143     CONVERT_CHECKED(effectBufferConfigFromHal(halConfig.outputCfg, isInput, &config->outputCfg),
144                     result);
145     return result;
146 }
147 
effectConfigToHal(const EffectConfig & config,effect_config_t * halConfig)148 status_t EffectUtils::effectConfigToHal(const EffectConfig& config, effect_config_t* halConfig) {
149     status_t result = NO_ERROR;
150     CONVERT_CHECKED(effectBufferConfigToHal(config.inputCfg, &halConfig->inputCfg), result);
151     CONVERT_CHECKED(effectBufferConfigToHal(config.outputCfg, &halConfig->outputCfg), result);
152     return result;
153 }
154 
155 template <std::size_t N>
charBufferFromHal(const char (& halBuf)[N])156 inline hidl_string charBufferFromHal(const char (&halBuf)[N]) {
157     // Even if the original field contains a non-terminated string, hidl_string
158     // adds a NUL terminator.
159     return hidl_string(halBuf, strnlen(halBuf, N));
160 }
161 
162 template <std::size_t N>
charBufferToHal(const hidl_string & str,char (& halBuf)[N],const char * fieldName)163 inline status_t charBufferToHal(const hidl_string& str, char (&halBuf)[N], const char* fieldName) {
164     static_assert(N > 0);
165     const size_t halBufChars = N - 1;  // Reserve one character for terminating NUL.
166     status_t result = NO_ERROR;
167     size_t strSize = str.size();
168     if (strSize > halBufChars) {
169         ALOGE("%s is too long: %zu (%zu max)", fieldName, strSize, halBufChars);
170         strSize = halBufChars;
171         result = BAD_VALUE;
172     }
173     strncpy(halBuf, str.c_str(), strSize);
174     halBuf[strSize] = '\0';
175     return result;
176 }
177 
effectDescriptorFromHal(const effect_descriptor_t & halDescriptor,EffectDescriptor * descriptor)178 status_t EffectUtils::effectDescriptorFromHal(const effect_descriptor_t& halDescriptor,
179                                               EffectDescriptor* descriptor) {
180     UuidUtils::uuidFromHal(halDescriptor.type, &descriptor->type);
181     UuidUtils::uuidFromHal(halDescriptor.uuid, &descriptor->uuid);
182     descriptor->flags = EnumBitfield<EffectFlags>(halDescriptor.flags);
183     descriptor->cpuLoad = halDescriptor.cpuLoad;
184     descriptor->memoryUsage = halDescriptor.memoryUsage;
185 #if MAJOR_VERSION <= 6
186     memcpy(descriptor->name.data(), halDescriptor.name, descriptor->name.size());
187     memcpy(descriptor->implementor.data(), halDescriptor.implementor,
188            descriptor->implementor.size());
189 #else
190     descriptor->name = charBufferFromHal(halDescriptor.name);
191     descriptor->implementor = charBufferFromHal(halDescriptor.implementor);
192 #endif
193     return NO_ERROR;
194 }
195 
effectDescriptorToHal(const EffectDescriptor & descriptor,effect_descriptor_t * halDescriptor)196 status_t EffectUtils::effectDescriptorToHal(const EffectDescriptor& descriptor,
197                                             effect_descriptor_t* halDescriptor) {
198     status_t result = NO_ERROR;
199     UuidUtils::uuidToHal(descriptor.type, &halDescriptor->type);
200     UuidUtils::uuidToHal(descriptor.uuid, &halDescriptor->uuid);
201     halDescriptor->flags = static_cast<uint32_t>(descriptor.flags);
202     halDescriptor->cpuLoad = descriptor.cpuLoad;
203     halDescriptor->memoryUsage = descriptor.memoryUsage;
204 #if MAJOR_VERSION <= 6
205     memcpy(halDescriptor->name, descriptor.name.data(), descriptor.name.size());
206     memcpy(halDescriptor->implementor, descriptor.implementor.data(),
207            descriptor.implementor.size());
208 #else
209     // According to 'dumpEffectDescriptor', 'name' and 'implementor' must be NUL-terminated.
210     CONVERT_CHECKED(charBufferToHal(descriptor.name, halDescriptor->name, "effect name"), result);
211     CONVERT_CHECKED(charBufferToHal(descriptor.implementor, halDescriptor->implementor,
212                                     "effect implementor"),
213                     result);
214 #endif
215     return result;
216 }
217 
218 }  // namespace implementation
219 }  // namespace CPP_VERSION
220 }  // namespace effect
221 }  // namespace audio
222 }  // namespace hardware
223 }  // namespace android
224