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 "Virtualizer_HAL"
18 
19 #include "VirtualizerEffect.h"
20 
21 #include <memory.h>
22 
23 #include <android/log.h>
24 #include <system/audio_effects/effect_virtualizer.h>
25 
26 #include "VersionUtils.h"
27 
28 namespace android {
29 namespace hardware {
30 namespace audio {
31 namespace effect {
32 namespace CPP_VERSION {
33 namespace implementation {
34 
VirtualizerEffect(effect_handle_t handle)35 VirtualizerEffect::VirtualizerEffect(effect_handle_t handle) : mEffect(new Effect(handle)) {}
36 
~VirtualizerEffect()37 VirtualizerEffect::~VirtualizerEffect() {}
38 
speakerAnglesFromHal(const int32_t * halAngles,uint32_t channelCount,hidl_vec<SpeakerAngle> & speakerAngles)39 void VirtualizerEffect::speakerAnglesFromHal(const int32_t* halAngles, uint32_t channelCount,
40                                              hidl_vec<SpeakerAngle>& speakerAngles) {
41     speakerAngles.resize(channelCount);
42     for (uint32_t i = 0; i < channelCount; ++i) {
43         speakerAngles[i].mask = AudioChannelBitfield(*halAngles++);
44         speakerAngles[i].azimuth = *halAngles++;
45         speakerAngles[i].elevation = *halAngles++;
46     }
47 }
48 
49 // Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow.
init()50 Return<Result> VirtualizerEffect::init() {
51     return mEffect->init();
52 }
53 
setConfig(const EffectConfig & config,const sp<IEffectBufferProviderCallback> & inputBufferProvider,const sp<IEffectBufferProviderCallback> & outputBufferProvider)54 Return<Result> VirtualizerEffect::setConfig(
55     const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
56     const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
57     return mEffect->setConfig(config, inputBufferProvider, outputBufferProvider);
58 }
59 
reset()60 Return<Result> VirtualizerEffect::reset() {
61     return mEffect->reset();
62 }
63 
enable()64 Return<Result> VirtualizerEffect::enable() {
65     return mEffect->enable();
66 }
67 
disable()68 Return<Result> VirtualizerEffect::disable() {
69     return mEffect->disable();
70 }
71 
setDevice(AudioDeviceBitfield device)72 Return<Result> VirtualizerEffect::setDevice(AudioDeviceBitfield device) {
73     return mEffect->setDevice(device);
74 }
75 
setAndGetVolume(const hidl_vec<uint32_t> & volumes,setAndGetVolume_cb _hidl_cb)76 Return<void> VirtualizerEffect::setAndGetVolume(const hidl_vec<uint32_t>& volumes,
77                                                 setAndGetVolume_cb _hidl_cb) {
78     return mEffect->setAndGetVolume(volumes, _hidl_cb);
79 }
80 
volumeChangeNotification(const hidl_vec<uint32_t> & volumes)81 Return<Result> VirtualizerEffect::volumeChangeNotification(const hidl_vec<uint32_t>& volumes) {
82     return mEffect->volumeChangeNotification(volumes);
83 }
84 
setAudioMode(AudioMode mode)85 Return<Result> VirtualizerEffect::setAudioMode(AudioMode mode) {
86     return mEffect->setAudioMode(mode);
87 }
88 
setConfigReverse(const EffectConfig & config,const sp<IEffectBufferProviderCallback> & inputBufferProvider,const sp<IEffectBufferProviderCallback> & outputBufferProvider)89 Return<Result> VirtualizerEffect::setConfigReverse(
90     const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
91     const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
92     return mEffect->setConfigReverse(config, inputBufferProvider, outputBufferProvider);
93 }
94 
setInputDevice(AudioDeviceBitfield device)95 Return<Result> VirtualizerEffect::setInputDevice(AudioDeviceBitfield device) {
96     return mEffect->setInputDevice(device);
97 }
98 
getConfig(getConfig_cb _hidl_cb)99 Return<void> VirtualizerEffect::getConfig(getConfig_cb _hidl_cb) {
100     return mEffect->getConfig(_hidl_cb);
101 }
102 
getConfigReverse(getConfigReverse_cb _hidl_cb)103 Return<void> VirtualizerEffect::getConfigReverse(getConfigReverse_cb _hidl_cb) {
104     return mEffect->getConfigReverse(_hidl_cb);
105 }
106 
getSupportedAuxChannelsConfigs(uint32_t maxConfigs,getSupportedAuxChannelsConfigs_cb _hidl_cb)107 Return<void> VirtualizerEffect::getSupportedAuxChannelsConfigs(
108     uint32_t maxConfigs, getSupportedAuxChannelsConfigs_cb _hidl_cb) {
109     return mEffect->getSupportedAuxChannelsConfigs(maxConfigs, _hidl_cb);
110 }
111 
getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb)112 Return<void> VirtualizerEffect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) {
113     return mEffect->getAuxChannelsConfig(_hidl_cb);
114 }
115 
setAuxChannelsConfig(const EffectAuxChannelsConfig & config)116 Return<Result> VirtualizerEffect::setAuxChannelsConfig(const EffectAuxChannelsConfig& config) {
117     return mEffect->setAuxChannelsConfig(config);
118 }
119 
setAudioSource(AudioSource source)120 Return<Result> VirtualizerEffect::setAudioSource(AudioSource source) {
121     return mEffect->setAudioSource(source);
122 }
123 
offload(const EffectOffloadParameter & param)124 Return<Result> VirtualizerEffect::offload(const EffectOffloadParameter& param) {
125     return mEffect->offload(param);
126 }
127 
getDescriptor(getDescriptor_cb _hidl_cb)128 Return<void> VirtualizerEffect::getDescriptor(getDescriptor_cb _hidl_cb) {
129     return mEffect->getDescriptor(_hidl_cb);
130 }
131 
prepareForProcessing(prepareForProcessing_cb _hidl_cb)132 Return<void> VirtualizerEffect::prepareForProcessing(prepareForProcessing_cb _hidl_cb) {
133     return mEffect->prepareForProcessing(_hidl_cb);
134 }
135 
setProcessBuffers(const AudioBuffer & inBuffer,const AudioBuffer & outBuffer)136 Return<Result> VirtualizerEffect::setProcessBuffers(const AudioBuffer& inBuffer,
137                                                     const AudioBuffer& outBuffer) {
138     return mEffect->setProcessBuffers(inBuffer, outBuffer);
139 }
140 
command(uint32_t commandId,const hidl_vec<uint8_t> & data,uint32_t resultMaxSize,command_cb _hidl_cb)141 Return<void> VirtualizerEffect::command(uint32_t commandId, const hidl_vec<uint8_t>& data,
142                                         uint32_t resultMaxSize, command_cb _hidl_cb) {
143     return mEffect->command(commandId, data, resultMaxSize, _hidl_cb);
144 }
145 
setParameter(const hidl_vec<uint8_t> & parameter,const hidl_vec<uint8_t> & value)146 Return<Result> VirtualizerEffect::setParameter(const hidl_vec<uint8_t>& parameter,
147                                                const hidl_vec<uint8_t>& value) {
148     return mEffect->setParameter(parameter, value);
149 }
150 
getParameter(const hidl_vec<uint8_t> & parameter,uint32_t valueMaxSize,getParameter_cb _hidl_cb)151 Return<void> VirtualizerEffect::getParameter(const hidl_vec<uint8_t>& parameter,
152                                              uint32_t valueMaxSize, getParameter_cb _hidl_cb) {
153     return mEffect->getParameter(parameter, valueMaxSize, _hidl_cb);
154 }
155 
getSupportedConfigsForFeature(uint32_t featureId,uint32_t maxConfigs,uint32_t configSize,getSupportedConfigsForFeature_cb _hidl_cb)156 Return<void> VirtualizerEffect::getSupportedConfigsForFeature(
157     uint32_t featureId, uint32_t maxConfigs, uint32_t configSize,
158     getSupportedConfigsForFeature_cb _hidl_cb) {
159     return mEffect->getSupportedConfigsForFeature(featureId, maxConfigs, configSize, _hidl_cb);
160 }
161 
getCurrentConfigForFeature(uint32_t featureId,uint32_t configSize,getCurrentConfigForFeature_cb _hidl_cb)162 Return<void> VirtualizerEffect::getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize,
163                                                            getCurrentConfigForFeature_cb _hidl_cb) {
164     return mEffect->getCurrentConfigForFeature(featureId, configSize, _hidl_cb);
165 }
166 
setCurrentConfigForFeature(uint32_t featureId,const hidl_vec<uint8_t> & configData)167 Return<Result> VirtualizerEffect::setCurrentConfigForFeature(uint32_t featureId,
168                                                              const hidl_vec<uint8_t>& configData) {
169     return mEffect->setCurrentConfigForFeature(featureId, configData);
170 }
171 
close()172 Return<Result> VirtualizerEffect::close() {
173     return mEffect->close();
174 }
175 
debug(const hidl_handle & fd,const hidl_vec<hidl_string> & options)176 Return<void> VirtualizerEffect::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) {
177     return mEffect->debug(fd, options);
178 }
179 
180 // Methods from ::android::hardware::audio::effect::CPP_VERSION::IVirtualizerEffect follow.
isStrengthSupported()181 Return<bool> VirtualizerEffect::isStrengthSupported() {
182     bool halSupported = false;
183     mEffect->getParam(VIRTUALIZER_PARAM_STRENGTH_SUPPORTED, halSupported);
184     return halSupported;
185 }
186 
setStrength(uint16_t strength)187 Return<Result> VirtualizerEffect::setStrength(uint16_t strength) {
188     return mEffect->setParam(VIRTUALIZER_PARAM_STRENGTH, strength);
189 }
190 
getStrength(getStrength_cb _hidl_cb)191 Return<void> VirtualizerEffect::getStrength(getStrength_cb _hidl_cb) {
192     return mEffect->getIntegerParam(VIRTUALIZER_PARAM_STRENGTH, _hidl_cb);
193 }
194 
getVirtualSpeakerAngles(AudioChannelBitfield mask,AudioDevice device,getVirtualSpeakerAngles_cb _hidl_cb)195 Return<void> VirtualizerEffect::getVirtualSpeakerAngles(AudioChannelBitfield mask,
196                                                         AudioDevice device,
197                                                         getVirtualSpeakerAngles_cb _hidl_cb) {
198     uint32_t channelCount =
199         audio_channel_count_from_out_mask(static_cast<audio_channel_mask_t>(mask));
200     size_t halSpeakerAnglesSize = sizeof(int32_t) * 3 * channelCount;
201     uint32_t halParam[3] = {VIRTUALIZER_PARAM_VIRTUAL_SPEAKER_ANGLES,
202                             static_cast<audio_channel_mask_t>(mask),
203                             static_cast<audio_devices_t>(device)};
204     hidl_vec<SpeakerAngle> speakerAngles;
205     Result retval = mEffect->getParameterImpl(
206         sizeof(halParam), halParam, halSpeakerAnglesSize,
207         [&](uint32_t valueSize, const void* valueData) {
208             if (valueSize > halSpeakerAnglesSize) {
209                 valueSize = halSpeakerAnglesSize;
210             } else if (valueSize < halSpeakerAnglesSize) {
211                 channelCount = valueSize / (sizeof(int32_t) * 3);
212             }
213             speakerAnglesFromHal(reinterpret_cast<const int32_t*>(valueData), channelCount,
214                                  speakerAngles);
215         });
216     _hidl_cb(retval, speakerAngles);
217     return Void();
218 }
219 
forceVirtualizationMode(AudioDevice device)220 Return<Result> VirtualizerEffect::forceVirtualizationMode(AudioDevice device) {
221     return mEffect->setParam(VIRTUALIZER_PARAM_FORCE_VIRTUALIZATION_MODE,
222                              static_cast<audio_devices_t>(device));
223 }
224 
getVirtualizationMode(getVirtualizationMode_cb _hidl_cb)225 Return<void> VirtualizerEffect::getVirtualizationMode(getVirtualizationMode_cb _hidl_cb) {
226     uint32_t halMode = 0;
227     Result retval = mEffect->getParam(VIRTUALIZER_PARAM_FORCE_VIRTUALIZATION_MODE, halMode);
228     _hidl_cb(retval, AudioDevice(halMode));
229     return Void();
230 }
231 
232 }  // namespace implementation
233 }  // namespace CPP_VERSION
234 }  // namespace effect
235 }  // namespace audio
236 }  // namespace hardware
237 }  // namespace android
238