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