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 #include <stdlib.h>
23 
24 #include <HidlUtils.h>
25 #include <android/log.h>
26 #include <system/audio_effects/effect_virtualizer.h>
27 
28 #include "VersionUtils.h"
29 
30 namespace android {
31 namespace hardware {
32 namespace audio {
33 namespace effect {
34 namespace CPP_VERSION {
35 namespace implementation {
36 
37 using ::android::hardware::audio::common::CPP_VERSION::implementation::HidlUtils;
38 
VirtualizerEffect(effect_handle_t handle)39 VirtualizerEffect::VirtualizerEffect(effect_handle_t handle)
40     : mEffect(new Effect(false /*isInput*/, handle)) {}
41 
42 // Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow.
init()43 Return<Result> VirtualizerEffect::init() {
44     return mEffect->init();
45 }
46 
setConfig(const EffectConfig & config,const sp<IEffectBufferProviderCallback> & inputBufferProvider,const sp<IEffectBufferProviderCallback> & outputBufferProvider)47 Return<Result> VirtualizerEffect::setConfig(
48     const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
49     const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
50     return mEffect->setConfig(config, inputBufferProvider, outputBufferProvider);
51 }
52 
reset()53 Return<Result> VirtualizerEffect::reset() {
54     return mEffect->reset();
55 }
56 
enable()57 Return<Result> VirtualizerEffect::enable() {
58     return mEffect->enable();
59 }
60 
disable()61 Return<Result> VirtualizerEffect::disable() {
62     return mEffect->disable();
63 }
64 
65 #if MAJOR_VERSION <= 6
setAudioSource(AudioSource source)66 Return<Result> VirtualizerEffect::setAudioSource(AudioSource source) {
67     return mEffect->setAudioSource(source);
68 }
69 
setDevice(AudioDeviceBitfield device)70 Return<Result> VirtualizerEffect::setDevice(AudioDeviceBitfield device) {
71     return mEffect->setDevice(device);
72 }
73 
setInputDevice(AudioDeviceBitfield device)74 Return<Result> VirtualizerEffect::setInputDevice(AudioDeviceBitfield device) {
75     return mEffect->setInputDevice(device);
76 }
77 #else
setAudioSource(const AudioSource & source)78 Return<Result> VirtualizerEffect::setAudioSource(const AudioSource& source) {
79     return mEffect->setAudioSource(source);
80 }
81 
setDevice(const DeviceAddress & device)82 Return<Result> VirtualizerEffect::setDevice(const DeviceAddress& device) {
83     return mEffect->setDevice(device);
84 }
85 
setInputDevice(const DeviceAddress & device)86 Return<Result> VirtualizerEffect::setInputDevice(const DeviceAddress& device) {
87     return mEffect->setInputDevice(device);
88 }
89 #endif
90 
setAndGetVolume(const hidl_vec<uint32_t> & volumes,setAndGetVolume_cb _hidl_cb)91 Return<void> VirtualizerEffect::setAndGetVolume(const hidl_vec<uint32_t>& volumes,
92                                                 setAndGetVolume_cb _hidl_cb) {
93     return mEffect->setAndGetVolume(volumes, _hidl_cb);
94 }
95 
volumeChangeNotification(const hidl_vec<uint32_t> & volumes)96 Return<Result> VirtualizerEffect::volumeChangeNotification(const hidl_vec<uint32_t>& volumes) {
97     return mEffect->volumeChangeNotification(volumes);
98 }
99 
setAudioMode(AudioMode mode)100 Return<Result> VirtualizerEffect::setAudioMode(AudioMode mode) {
101     return mEffect->setAudioMode(mode);
102 }
103 
setConfigReverse(const EffectConfig & config,const sp<IEffectBufferProviderCallback> & inputBufferProvider,const sp<IEffectBufferProviderCallback> & outputBufferProvider)104 Return<Result> VirtualizerEffect::setConfigReverse(
105     const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
106     const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
107     return mEffect->setConfigReverse(config, inputBufferProvider, outputBufferProvider);
108 }
109 
getConfig(getConfig_cb _hidl_cb)110 Return<void> VirtualizerEffect::getConfig(getConfig_cb _hidl_cb) {
111     return mEffect->getConfig(_hidl_cb);
112 }
113 
getConfigReverse(getConfigReverse_cb _hidl_cb)114 Return<void> VirtualizerEffect::getConfigReverse(getConfigReverse_cb _hidl_cb) {
115     return mEffect->getConfigReverse(_hidl_cb);
116 }
117 
getSupportedAuxChannelsConfigs(uint32_t maxConfigs,getSupportedAuxChannelsConfigs_cb _hidl_cb)118 Return<void> VirtualizerEffect::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> VirtualizerEffect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) {
124     return mEffect->getAuxChannelsConfig(_hidl_cb);
125 }
126 
setAuxChannelsConfig(const EffectAuxChannelsConfig & config)127 Return<Result> VirtualizerEffect::setAuxChannelsConfig(const EffectAuxChannelsConfig& config) {
128     return mEffect->setAuxChannelsConfig(config);
129 }
130 
offload(const EffectOffloadParameter & param)131 Return<Result> VirtualizerEffect::offload(const EffectOffloadParameter& param) {
132     return mEffect->offload(param);
133 }
134 
getDescriptor(getDescriptor_cb _hidl_cb)135 Return<void> VirtualizerEffect::getDescriptor(getDescriptor_cb _hidl_cb) {
136     return mEffect->getDescriptor(_hidl_cb);
137 }
138 
prepareForProcessing(prepareForProcessing_cb _hidl_cb)139 Return<void> VirtualizerEffect::prepareForProcessing(prepareForProcessing_cb _hidl_cb) {
140     return mEffect->prepareForProcessing(_hidl_cb);
141 }
142 
setProcessBuffers(const AudioBuffer & inBuffer,const AudioBuffer & outBuffer)143 Return<Result> VirtualizerEffect::setProcessBuffers(const AudioBuffer& inBuffer,
144                                                     const AudioBuffer& outBuffer) {
145     return mEffect->setProcessBuffers(inBuffer, outBuffer);
146 }
147 
command(uint32_t commandId,const hidl_vec<uint8_t> & data,uint32_t resultMaxSize,command_cb _hidl_cb)148 Return<void> VirtualizerEffect::command(uint32_t commandId, const hidl_vec<uint8_t>& data,
149                                         uint32_t resultMaxSize, 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(const hidl_vec<uint8_t>& parameter,
154                                                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(const hidl_vec<uint8_t>& parameter,
159                                              uint32_t valueMaxSize, getParameter_cb _hidl_cb) {
160     return mEffect->getParameter(parameter, valueMaxSize, _hidl_cb);
161 }
162 
getSupportedConfigsForFeature(uint32_t featureId,uint32_t maxConfigs,uint32_t configSize,getSupportedConfigsForFeature_cb _hidl_cb)163 Return<void> VirtualizerEffect::getSupportedConfigsForFeature(
164     uint32_t featureId, uint32_t maxConfigs, uint32_t configSize,
165     getSupportedConfigsForFeature_cb _hidl_cb) {
166     return mEffect->getSupportedConfigsForFeature(featureId, maxConfigs, configSize, _hidl_cb);
167 }
168 
getCurrentConfigForFeature(uint32_t featureId,uint32_t configSize,getCurrentConfigForFeature_cb _hidl_cb)169 Return<void> VirtualizerEffect::getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize,
170                                                            getCurrentConfigForFeature_cb _hidl_cb) {
171     return mEffect->getCurrentConfigForFeature(featureId, configSize, _hidl_cb);
172 }
173 
setCurrentConfigForFeature(uint32_t featureId,const hidl_vec<uint8_t> & configData)174 Return<Result> VirtualizerEffect::setCurrentConfigForFeature(uint32_t featureId,
175                                                              const hidl_vec<uint8_t>& configData) {
176     return mEffect->setCurrentConfigForFeature(featureId, configData);
177 }
178 
close()179 Return<Result> VirtualizerEffect::close() {
180     return mEffect->close();
181 }
182 
debug(const hidl_handle & fd,const hidl_vec<hidl_string> & options)183 Return<void> VirtualizerEffect::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) {
184     return mEffect->debug(fd, options);
185 }
186 
187 // Methods from ::android::hardware::audio::effect::CPP_VERSION::IVirtualizerEffect follow.
isStrengthSupported()188 Return<bool> VirtualizerEffect::isStrengthSupported() {
189     bool halSupported = false;
190     mEffect->getParam(VIRTUALIZER_PARAM_STRENGTH_SUPPORTED, halSupported);
191     return halSupported;
192 }
193 
setStrength(uint16_t strength)194 Return<Result> VirtualizerEffect::setStrength(uint16_t strength) {
195     return mEffect->setParam(VIRTUALIZER_PARAM_STRENGTH, strength);
196 }
197 
getStrength(getStrength_cb _hidl_cb)198 Return<void> VirtualizerEffect::getStrength(getStrength_cb _hidl_cb) {
199     return mEffect->getIntegerParam(VIRTUALIZER_PARAM_STRENGTH, _hidl_cb);
200 }
201 
getVirtualSpeakerAngles(AudioChannelBitfield mask,AudioDevice device,getVirtualSpeakerAngles_cb _hidl_cb)202 Return<void> VirtualizerEffect::getVirtualSpeakerAngles(
203 #if MAJOR_VERSION <= 6
204         AudioChannelBitfield mask, AudioDevice device, getVirtualSpeakerAngles_cb _hidl_cb) {
205     audio_channel_mask_t halChannelMask = static_cast<audio_channel_mask_t>(mask);
206     audio_devices_t halDeviceType = static_cast<audio_devices_t>(device);
207 #else
208         const AudioChannelMask& mask, const DeviceAddress& device,
209         getVirtualSpeakerAngles_cb _hidl_cb) {
210     audio_channel_mask_t halChannelMask;
211     if (status_t status = HidlUtils::audioChannelMaskToHal(mask, &halChannelMask);
212         status != NO_ERROR) {
213         _hidl_cb(mEffect->analyzeStatus(__func__, "audioChannelMaskToHal",
214                                         Effect::sContextConversion, status),
215                  SpeakerAngles{});
216         return Void();
217     }
218     audio_devices_t halDeviceType;
219     char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
220     if (status_t status = HidlUtils::deviceAddressToHal(device, &halDeviceType, halDeviceAddress);
221         status != NO_ERROR) {
222         _hidl_cb(mEffect->analyzeStatus(__func__, "deviceAddressToHal", Effect::sContextConversion,
223                                         status),
224                  SpeakerAngles{});
225         return Void();
226     }
227 #endif
228     uint32_t channelCount = audio_channel_count_from_out_mask(halChannelMask);
229     size_t halSpeakerAnglesSize = sizeof(int32_t) * 3 * channelCount;
230     uint32_t halParam[3] = {VIRTUALIZER_PARAM_VIRTUAL_SPEAKER_ANGLES, halChannelMask,
231                             halDeviceType};
232     SpeakerAngles speakerAngles;
233     status_t status = NO_ERROR;
234     Result retval = mEffect->getParameterImpl(
235             sizeof(halParam), halParam, halSpeakerAnglesSize,
236             [&](uint32_t valueSize, const void* valueData) {
237                 if (valueSize < halSpeakerAnglesSize) {
238                     channelCount = valueSize / (sizeof(int32_t) * 3);
239                 }
240                 status = speakerAnglesFromHal(reinterpret_cast<const int32_t*>(valueData),
241                                               channelCount, speakerAngles);
242             });
243     if (retval == Result::OK) {
244         retval = mEffect->analyzeStatus(__func__, "speakerAnglesFromHal", "", status);
245     }
246     _hidl_cb(retval, speakerAngles);
247     return Void();
248 }
249 
250 Return<void> VirtualizerEffect::getVirtualizationMode(getVirtualizationMode_cb _hidl_cb) {
251     uint32_t halMode = 0;
252     Result retval = mEffect->getParam(VIRTUALIZER_PARAM_FORCE_VIRTUALIZATION_MODE, halMode);
253 #if MAJOR_VERSION <= 6
254     _hidl_cb(retval, AudioDevice(halMode));
255 #else
256     DeviceAddress device;
257     (void)HidlUtils::deviceAddressFromHal(static_cast<audio_devices_t>(halMode), nullptr, &device);
258     _hidl_cb(retval, device);
259 #endif
260     return Void();
261 }
262 
263 Return<Result> VirtualizerEffect::forceVirtualizationMode(
264 #if MAJOR_VERSION <= 6
265         AudioDevice device) {
266     audio_devices_t halDeviceType = static_cast<audio_devices_t>(device);
267 #else
268         const DeviceAddress& device) {
269     audio_devices_t halDeviceType;
270     char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN];
271     (void)HidlUtils::deviceAddressToHal(device, &halDeviceType, halDeviceAddress);
272 #endif
273     return mEffect->setParam(VIRTUALIZER_PARAM_FORCE_VIRTUALIZATION_MODE, halDeviceType);
274 }
275 
276 #if MAJOR_VERSION <= 6
277 // static
278 status_t VirtualizerEffect::speakerAnglesFromHal(const int32_t* halAngles, uint32_t channelCount,
279                                                  hidl_vec<SpeakerAngle>& speakerAngles) {
280     speakerAngles.resize(channelCount);
281     for (uint32_t i = 0; i < channelCount; ++i) {
282         speakerAngles[i].mask = AudioChannelBitfield(*halAngles++);
283         speakerAngles[i].azimuth = *halAngles++;
284         speakerAngles[i].elevation = *halAngles++;
285     }
286     return NO_ERROR;
287 }
288 #else
289 static int compare_channels(const void* lhs, const void* rhs) {
290     return *(int32_t*)lhs - *(int32_t*)rhs;
291 }
292 
293 // static
294 status_t VirtualizerEffect::speakerAnglesFromHal(const int32_t* halAngles, uint32_t channelCount,
295                                                  SpeakerAngles& speakerAngles) {
296     speakerAngles.azimuth.resize(channelCount);
297     speakerAngles.elevation.resize(channelCount);
298     int32_t halAnglesSorted[channelCount * 3];
299     memcpy(halAnglesSorted, halAngles, sizeof(halAnglesSorted));
300     // Ensure that channels are ordered from LSb to MSb.
301     qsort(halAnglesSorted, channelCount, sizeof(int32_t) * 3, compare_channels);
302     audio_channel_mask_t halMask = AUDIO_CHANNEL_NONE;
303     int32_t* halAnglesPtr = halAnglesSorted;
304     for (uint32_t i = 0; i < channelCount; ++i) {
305         halMask = static_cast<audio_channel_mask_t>(halMask | *halAnglesPtr++);
306         speakerAngles.azimuth[i] = *halAnglesPtr++;
307         speakerAngles.elevation[i] = *halAnglesPtr++;
308     }
309     return HidlUtils::audioChannelMaskFromHal(halMask, false /*isInput*/, &speakerAngles.mask);
310 }
311 #endif
312 
313 }  // namespace implementation
314 }  // namespace CPP_VERSION
315 }  // namespace effect
316 }  // namespace audio
317 }  // namespace hardware
318 }  // namespace android
319