1 /*
2  * Copyright (C) 2018 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 "Equalizer_HAL"
18 
19 #include "EqualizerEffect.h"
20 
21 #include <memory.h>
22 
23 #include <android/log.h>
24 
25 #include "VersionUtils.h"
26 
27 namespace android {
28 namespace hardware {
29 namespace audio {
30 namespace effect {
31 namespace CPP_VERSION {
32 namespace implementation {
33 
EqualizerEffect(effect_handle_t handle)34 EqualizerEffect::EqualizerEffect(effect_handle_t handle) : mEffect(new Effect(handle)) {}
35 
~EqualizerEffect()36 EqualizerEffect::~EqualizerEffect() {}
37 
propertiesFromHal(const t_equalizer_settings & halProperties,IEqualizerEffect::AllProperties * properties)38 void EqualizerEffect::propertiesFromHal(const t_equalizer_settings& halProperties,
39                                         IEqualizerEffect::AllProperties* properties) {
40     properties->curPreset = halProperties.curPreset;
41     // t_equalizer_settings incorrectly defines bandLevels as uint16_t,
42     // whereas the actual type of values used by effects is int16_t.
43     const int16_t* signedBandLevels =
44         reinterpret_cast<const int16_t*>(&halProperties.bandLevels[0]);
45     properties->bandLevels.setToExternal(const_cast<int16_t*>(signedBandLevels),
46                                          halProperties.numBands);
47 }
48 
propertiesToHal(const IEqualizerEffect::AllProperties & properties,t_equalizer_settings ** halProperties)49 std::vector<uint8_t> EqualizerEffect::propertiesToHal(
50     const IEqualizerEffect::AllProperties& properties, t_equalizer_settings** halProperties) {
51     size_t bandsSize = properties.bandLevels.size() * sizeof(uint16_t);
52     std::vector<uint8_t> halBuffer(sizeof(t_equalizer_settings) + bandsSize, 0);
53     *halProperties = reinterpret_cast<t_equalizer_settings*>(&halBuffer[0]);
54     (*halProperties)->curPreset = properties.curPreset;
55     (*halProperties)->numBands = properties.bandLevels.size();
56     memcpy((*halProperties)->bandLevels, &properties.bandLevels[0], bandsSize);
57     return halBuffer;
58 }
59 
60 // Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow.
init()61 Return<Result> EqualizerEffect::init() {
62     return mEffect->init();
63 }
64 
setConfig(const EffectConfig & config,const sp<IEffectBufferProviderCallback> & inputBufferProvider,const sp<IEffectBufferProviderCallback> & outputBufferProvider)65 Return<Result> EqualizerEffect::setConfig(
66     const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
67     const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
68     return mEffect->setConfig(config, inputBufferProvider, outputBufferProvider);
69 }
70 
reset()71 Return<Result> EqualizerEffect::reset() {
72     return mEffect->reset();
73 }
74 
enable()75 Return<Result> EqualizerEffect::enable() {
76     return mEffect->enable();
77 }
78 
disable()79 Return<Result> EqualizerEffect::disable() {
80     return mEffect->disable();
81 }
82 
setDevice(AudioDeviceBitfield device)83 Return<Result> EqualizerEffect::setDevice(AudioDeviceBitfield device) {
84     return mEffect->setDevice(device);
85 }
86 
setAndGetVolume(const hidl_vec<uint32_t> & volumes,setAndGetVolume_cb _hidl_cb)87 Return<void> EqualizerEffect::setAndGetVolume(const hidl_vec<uint32_t>& volumes,
88                                               setAndGetVolume_cb _hidl_cb) {
89     return mEffect->setAndGetVolume(volumes, _hidl_cb);
90 }
91 
volumeChangeNotification(const hidl_vec<uint32_t> & volumes)92 Return<Result> EqualizerEffect::volumeChangeNotification(const hidl_vec<uint32_t>& volumes) {
93     return mEffect->volumeChangeNotification(volumes);
94 }
95 
setAudioMode(AudioMode mode)96 Return<Result> EqualizerEffect::setAudioMode(AudioMode mode) {
97     return mEffect->setAudioMode(mode);
98 }
99 
setConfigReverse(const EffectConfig & config,const sp<IEffectBufferProviderCallback> & inputBufferProvider,const sp<IEffectBufferProviderCallback> & outputBufferProvider)100 Return<Result> EqualizerEffect::setConfigReverse(
101     const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
102     const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
103     return mEffect->setConfigReverse(config, inputBufferProvider, outputBufferProvider);
104 }
105 
setInputDevice(AudioDeviceBitfield device)106 Return<Result> EqualizerEffect::setInputDevice(AudioDeviceBitfield device) {
107     return mEffect->setInputDevice(device);
108 }
109 
getConfig(getConfig_cb _hidl_cb)110 Return<void> EqualizerEffect::getConfig(getConfig_cb _hidl_cb) {
111     return mEffect->getConfig(_hidl_cb);
112 }
113 
getConfigReverse(getConfigReverse_cb _hidl_cb)114 Return<void> EqualizerEffect::getConfigReverse(getConfigReverse_cb _hidl_cb) {
115     return mEffect->getConfigReverse(_hidl_cb);
116 }
117 
getSupportedAuxChannelsConfigs(uint32_t maxConfigs,getSupportedAuxChannelsConfigs_cb _hidl_cb)118 Return<void> EqualizerEffect::getSupportedAuxChannelsConfigs(
119     uint32_t maxConfigs, getSupportedAuxChannelsConfigs_cb _hidl_cb) {
120     return mEffect->getSupportedAuxChannelsConfigs(maxConfigs, _hidl_cb);
121 }
122 
getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb)123 Return<void> EqualizerEffect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) {
124     return mEffect->getAuxChannelsConfig(_hidl_cb);
125 }
126 
setAuxChannelsConfig(const EffectAuxChannelsConfig & config)127 Return<Result> EqualizerEffect::setAuxChannelsConfig(const EffectAuxChannelsConfig& config) {
128     return mEffect->setAuxChannelsConfig(config);
129 }
130 
setAudioSource(AudioSource source)131 Return<Result> EqualizerEffect::setAudioSource(AudioSource source) {
132     return mEffect->setAudioSource(source);
133 }
134 
offload(const EffectOffloadParameter & param)135 Return<Result> EqualizerEffect::offload(const EffectOffloadParameter& param) {
136     return mEffect->offload(param);
137 }
138 
getDescriptor(getDescriptor_cb _hidl_cb)139 Return<void> EqualizerEffect::getDescriptor(getDescriptor_cb _hidl_cb) {
140     return mEffect->getDescriptor(_hidl_cb);
141 }
142 
prepareForProcessing(prepareForProcessing_cb _hidl_cb)143 Return<void> EqualizerEffect::prepareForProcessing(prepareForProcessing_cb _hidl_cb) {
144     return mEffect->prepareForProcessing(_hidl_cb);
145 }
146 
setProcessBuffers(const AudioBuffer & inBuffer,const AudioBuffer & outBuffer)147 Return<Result> EqualizerEffect::setProcessBuffers(const AudioBuffer& inBuffer,
148                                                   const AudioBuffer& outBuffer) {
149     return mEffect->setProcessBuffers(inBuffer, outBuffer);
150 }
151 
command(uint32_t commandId,const hidl_vec<uint8_t> & data,uint32_t resultMaxSize,command_cb _hidl_cb)152 Return<void> EqualizerEffect::command(uint32_t commandId, const hidl_vec<uint8_t>& data,
153                                       uint32_t resultMaxSize, command_cb _hidl_cb) {
154     return mEffect->command(commandId, data, resultMaxSize, _hidl_cb);
155 }
156 
setParameter(const hidl_vec<uint8_t> & parameter,const hidl_vec<uint8_t> & value)157 Return<Result> EqualizerEffect::setParameter(const hidl_vec<uint8_t>& parameter,
158                                              const hidl_vec<uint8_t>& value) {
159     return mEffect->setParameter(parameter, value);
160 }
161 
getParameter(const hidl_vec<uint8_t> & parameter,uint32_t valueMaxSize,getParameter_cb _hidl_cb)162 Return<void> EqualizerEffect::getParameter(const hidl_vec<uint8_t>& parameter,
163                                            uint32_t valueMaxSize, getParameter_cb _hidl_cb) {
164     return mEffect->getParameter(parameter, valueMaxSize, _hidl_cb);
165 }
166 
getSupportedConfigsForFeature(uint32_t featureId,uint32_t maxConfigs,uint32_t configSize,getSupportedConfigsForFeature_cb _hidl_cb)167 Return<void> EqualizerEffect::getSupportedConfigsForFeature(
168     uint32_t featureId, uint32_t maxConfigs, uint32_t configSize,
169     getSupportedConfigsForFeature_cb _hidl_cb) {
170     return mEffect->getSupportedConfigsForFeature(featureId, maxConfigs, configSize, _hidl_cb);
171 }
172 
getCurrentConfigForFeature(uint32_t featureId,uint32_t configSize,getCurrentConfigForFeature_cb _hidl_cb)173 Return<void> EqualizerEffect::getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize,
174                                                          getCurrentConfigForFeature_cb _hidl_cb) {
175     return mEffect->getCurrentConfigForFeature(featureId, configSize, _hidl_cb);
176 }
177 
setCurrentConfigForFeature(uint32_t featureId,const hidl_vec<uint8_t> & configData)178 Return<Result> EqualizerEffect::setCurrentConfigForFeature(uint32_t featureId,
179                                                            const hidl_vec<uint8_t>& configData) {
180     return mEffect->setCurrentConfigForFeature(featureId, configData);
181 }
182 
close()183 Return<Result> EqualizerEffect::close() {
184     return mEffect->close();
185 }
186 
debug(const hidl_handle & fd,const hidl_vec<hidl_string> & options)187 Return<void> EqualizerEffect::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) {
188     return mEffect->debug(fd, options);
189 }
190 
191 // Methods from ::android::hardware::audio::effect::CPP_VERSION::IEqualizerEffect follow.
getNumBands(getNumBands_cb _hidl_cb)192 Return<void> EqualizerEffect::getNumBands(getNumBands_cb _hidl_cb) {
193     return mEffect->getIntegerParam(EQ_PARAM_NUM_BANDS, _hidl_cb);
194 }
195 
getLevelRange(getLevelRange_cb _hidl_cb)196 Return<void> EqualizerEffect::getLevelRange(getLevelRange_cb _hidl_cb) {
197     int16_t halLevels[2] = {0, 0};
198     Result retval = mEffect->getParam(EQ_PARAM_LEVEL_RANGE, halLevels);
199     _hidl_cb(retval, halLevels[0], halLevels[1]);
200     return Void();
201 }
202 
setBandLevel(uint16_t band,int16_t level)203 Return<Result> EqualizerEffect::setBandLevel(uint16_t band, int16_t level) {
204     return mEffect->setParam(EQ_PARAM_BAND_LEVEL, band, level);
205 }
206 
getBandLevel(uint16_t band,getBandLevel_cb _hidl_cb)207 Return<void> EqualizerEffect::getBandLevel(uint16_t band, getBandLevel_cb _hidl_cb) {
208     int16_t halLevel = 0;
209     Result retval = mEffect->getParam(EQ_PARAM_BAND_LEVEL, band, halLevel);
210     _hidl_cb(retval, halLevel);
211     return Void();
212 }
213 
getBandCenterFrequency(uint16_t band,getBandCenterFrequency_cb _hidl_cb)214 Return<void> EqualizerEffect::getBandCenterFrequency(uint16_t band,
215                                                      getBandCenterFrequency_cb _hidl_cb) {
216     uint32_t halFreq = 0;
217     Result retval = mEffect->getParam(EQ_PARAM_CENTER_FREQ, band, halFreq);
218     _hidl_cb(retval, halFreq);
219     return Void();
220 }
221 
getBandFrequencyRange(uint16_t band,getBandFrequencyRange_cb _hidl_cb)222 Return<void> EqualizerEffect::getBandFrequencyRange(uint16_t band,
223                                                     getBandFrequencyRange_cb _hidl_cb) {
224     uint32_t halFreqs[2] = {0, 0};
225     Result retval = mEffect->getParam(EQ_PARAM_BAND_FREQ_RANGE, band, halFreqs);
226     _hidl_cb(retval, halFreqs[0], halFreqs[1]);
227     return Void();
228 }
229 
getBandForFrequency(uint32_t freq,getBandForFrequency_cb _hidl_cb)230 Return<void> EqualizerEffect::getBandForFrequency(uint32_t freq, getBandForFrequency_cb _hidl_cb) {
231     uint16_t halBand = 0;
232     Result retval = mEffect->getParam(EQ_PARAM_GET_BAND, freq, halBand);
233     _hidl_cb(retval, halBand);
234     return Void();
235 }
236 
getPresetNames(getPresetNames_cb _hidl_cb)237 Return<void> EqualizerEffect::getPresetNames(getPresetNames_cb _hidl_cb) {
238     uint16_t halPresetCount = 0;
239     Result retval = mEffect->getParam(EQ_PARAM_GET_NUM_OF_PRESETS, halPresetCount);
240     hidl_vec<hidl_string> presetNames;
241     if (retval == Result::OK) {
242         presetNames.resize(halPresetCount);
243         for (uint16_t i = 0; i < halPresetCount; ++i) {
244             char halPresetName[EFFECT_STRING_LEN_MAX];
245             retval = mEffect->getParam(EQ_PARAM_GET_PRESET_NAME, i, halPresetName);
246             if (retval == Result::OK) {
247                 presetNames[i] = halPresetName;
248             } else {
249                 presetNames.resize(i);
250             }
251         }
252     }
253     _hidl_cb(retval, presetNames);
254     return Void();
255 }
256 
setCurrentPreset(uint16_t preset)257 Return<Result> EqualizerEffect::setCurrentPreset(uint16_t preset) {
258     return mEffect->setParam(EQ_PARAM_CUR_PRESET, preset);
259 }
260 
getCurrentPreset(getCurrentPreset_cb _hidl_cb)261 Return<void> EqualizerEffect::getCurrentPreset(getCurrentPreset_cb _hidl_cb) {
262     return mEffect->getIntegerParam(EQ_PARAM_CUR_PRESET, _hidl_cb);
263 }
264 
setAllProperties(const IEqualizerEffect::AllProperties & properties)265 Return<Result> EqualizerEffect::setAllProperties(
266     const IEqualizerEffect::AllProperties& properties) {
267     t_equalizer_settings* halPropertiesPtr = nullptr;
268     std::vector<uint8_t> halBuffer = propertiesToHal(properties, &halPropertiesPtr);
269     uint32_t paramId = EQ_PARAM_PROPERTIES;
270     return mEffect->setParameterImpl(sizeof(paramId), &paramId, halBuffer.size(), halPropertiesPtr);
271 }
272 
getAllProperties(getAllProperties_cb _hidl_cb)273 Return<void> EqualizerEffect::getAllProperties(getAllProperties_cb _hidl_cb) {
274     uint16_t numBands = 0;
275     Result retval = mEffect->getParam(EQ_PARAM_NUM_BANDS, numBands);
276     AllProperties properties;
277     if (retval != Result::OK) {
278         _hidl_cb(retval, properties);
279         return Void();
280     }
281     size_t valueSize = sizeof(t_equalizer_settings) + sizeof(int16_t) * numBands;
282     uint32_t paramId = EQ_PARAM_PROPERTIES;
283     retval = mEffect->getParameterImpl(
284         sizeof(paramId), &paramId, valueSize, [&](uint32_t, const void* valueData) {
285             const t_equalizer_settings* halProperties =
286                 reinterpret_cast<const t_equalizer_settings*>(valueData);
287             propertiesFromHal(*halProperties, &properties);
288         });
289     _hidl_cb(retval, properties);
290     return Void();
291 }
292 
293 }  // namespace implementation
294 }  // namespace CPP_VERSION
295 }  // namespace effect
296 }  // namespace audio
297 }  // namespace hardware
298 }  // namespace android
299