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 "Visualizer_HAL"
18 
19 #include "VisualizerEffect.h"
20 
21 #include <android/log.h>
22 #include <system/audio_effects/effect_visualizer.h>
23 
24 #include "VersionUtils.h"
25 
26 namespace android {
27 namespace hardware {
28 namespace audio {
29 namespace effect {
30 namespace CPP_VERSION {
31 namespace implementation {
32 
VisualizerEffect(effect_handle_t handle)33 VisualizerEffect::VisualizerEffect(effect_handle_t handle)
34     : mEffect(new Effect(handle)), mCaptureSize(0), mMeasurementMode(MeasurementMode::NONE) {}
35 
~VisualizerEffect()36 VisualizerEffect::~VisualizerEffect() {}
37 
38 // Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow.
init()39 Return<Result> VisualizerEffect::init() {
40     return mEffect->init();
41 }
42 
setConfig(const EffectConfig & config,const sp<IEffectBufferProviderCallback> & inputBufferProvider,const sp<IEffectBufferProviderCallback> & outputBufferProvider)43 Return<Result> VisualizerEffect::setConfig(
44     const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
45     const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
46     return mEffect->setConfig(config, inputBufferProvider, outputBufferProvider);
47 }
48 
reset()49 Return<Result> VisualizerEffect::reset() {
50     return mEffect->reset();
51 }
52 
enable()53 Return<Result> VisualizerEffect::enable() {
54     return mEffect->enable();
55 }
56 
disable()57 Return<Result> VisualizerEffect::disable() {
58     return mEffect->disable();
59 }
60 
setDevice(AudioDeviceBitfield device)61 Return<Result> VisualizerEffect::setDevice(AudioDeviceBitfield device) {
62     return mEffect->setDevice(device);
63 }
64 
setAndGetVolume(const hidl_vec<uint32_t> & volumes,setAndGetVolume_cb _hidl_cb)65 Return<void> VisualizerEffect::setAndGetVolume(const hidl_vec<uint32_t>& volumes,
66                                                setAndGetVolume_cb _hidl_cb) {
67     return mEffect->setAndGetVolume(volumes, _hidl_cb);
68 }
69 
volumeChangeNotification(const hidl_vec<uint32_t> & volumes)70 Return<Result> VisualizerEffect::volumeChangeNotification(const hidl_vec<uint32_t>& volumes) {
71     return mEffect->volumeChangeNotification(volumes);
72 }
73 
setAudioMode(AudioMode mode)74 Return<Result> VisualizerEffect::setAudioMode(AudioMode mode) {
75     return mEffect->setAudioMode(mode);
76 }
77 
setConfigReverse(const EffectConfig & config,const sp<IEffectBufferProviderCallback> & inputBufferProvider,const sp<IEffectBufferProviderCallback> & outputBufferProvider)78 Return<Result> VisualizerEffect::setConfigReverse(
79     const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
80     const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
81     return mEffect->setConfigReverse(config, inputBufferProvider, outputBufferProvider);
82 }
83 
setInputDevice(AudioDeviceBitfield device)84 Return<Result> VisualizerEffect::setInputDevice(AudioDeviceBitfield device) {
85     return mEffect->setInputDevice(device);
86 }
87 
getConfig(getConfig_cb _hidl_cb)88 Return<void> VisualizerEffect::getConfig(getConfig_cb _hidl_cb) {
89     return mEffect->getConfig(_hidl_cb);
90 }
91 
getConfigReverse(getConfigReverse_cb _hidl_cb)92 Return<void> VisualizerEffect::getConfigReverse(getConfigReverse_cb _hidl_cb) {
93     return mEffect->getConfigReverse(_hidl_cb);
94 }
95 
getSupportedAuxChannelsConfigs(uint32_t maxConfigs,getSupportedAuxChannelsConfigs_cb _hidl_cb)96 Return<void> VisualizerEffect::getSupportedAuxChannelsConfigs(
97     uint32_t maxConfigs, getSupportedAuxChannelsConfigs_cb _hidl_cb) {
98     return mEffect->getSupportedAuxChannelsConfigs(maxConfigs, _hidl_cb);
99 }
100 
getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb)101 Return<void> VisualizerEffect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) {
102     return mEffect->getAuxChannelsConfig(_hidl_cb);
103 }
104 
setAuxChannelsConfig(const EffectAuxChannelsConfig & config)105 Return<Result> VisualizerEffect::setAuxChannelsConfig(const EffectAuxChannelsConfig& config) {
106     return mEffect->setAuxChannelsConfig(config);
107 }
108 
setAudioSource(AudioSource source)109 Return<Result> VisualizerEffect::setAudioSource(AudioSource source) {
110     return mEffect->setAudioSource(source);
111 }
112 
offload(const EffectOffloadParameter & param)113 Return<Result> VisualizerEffect::offload(const EffectOffloadParameter& param) {
114     return mEffect->offload(param);
115 }
116 
getDescriptor(getDescriptor_cb _hidl_cb)117 Return<void> VisualizerEffect::getDescriptor(getDescriptor_cb _hidl_cb) {
118     return mEffect->getDescriptor(_hidl_cb);
119 }
120 
prepareForProcessing(prepareForProcessing_cb _hidl_cb)121 Return<void> VisualizerEffect::prepareForProcessing(prepareForProcessing_cb _hidl_cb) {
122     return mEffect->prepareForProcessing(_hidl_cb);
123 }
124 
setProcessBuffers(const AudioBuffer & inBuffer,const AudioBuffer & outBuffer)125 Return<Result> VisualizerEffect::setProcessBuffers(const AudioBuffer& inBuffer,
126                                                    const AudioBuffer& outBuffer) {
127     return mEffect->setProcessBuffers(inBuffer, outBuffer);
128 }
129 
command(uint32_t commandId,const hidl_vec<uint8_t> & data,uint32_t resultMaxSize,command_cb _hidl_cb)130 Return<void> VisualizerEffect::command(uint32_t commandId, const hidl_vec<uint8_t>& data,
131                                        uint32_t resultMaxSize, command_cb _hidl_cb) {
132     return mEffect->command(commandId, data, resultMaxSize, _hidl_cb);
133 }
134 
setParameter(const hidl_vec<uint8_t> & parameter,const hidl_vec<uint8_t> & value)135 Return<Result> VisualizerEffect::setParameter(const hidl_vec<uint8_t>& parameter,
136                                               const hidl_vec<uint8_t>& value) {
137     return mEffect->setParameter(parameter, value);
138 }
139 
getParameter(const hidl_vec<uint8_t> & parameter,uint32_t valueMaxSize,getParameter_cb _hidl_cb)140 Return<void> VisualizerEffect::getParameter(const hidl_vec<uint8_t>& parameter,
141                                             uint32_t valueMaxSize, getParameter_cb _hidl_cb) {
142     return mEffect->getParameter(parameter, valueMaxSize, _hidl_cb);
143 }
144 
getSupportedConfigsForFeature(uint32_t featureId,uint32_t maxConfigs,uint32_t configSize,getSupportedConfigsForFeature_cb _hidl_cb)145 Return<void> VisualizerEffect::getSupportedConfigsForFeature(
146     uint32_t featureId, uint32_t maxConfigs, uint32_t configSize,
147     getSupportedConfigsForFeature_cb _hidl_cb) {
148     return mEffect->getSupportedConfigsForFeature(featureId, maxConfigs, configSize, _hidl_cb);
149 }
150 
getCurrentConfigForFeature(uint32_t featureId,uint32_t configSize,getCurrentConfigForFeature_cb _hidl_cb)151 Return<void> VisualizerEffect::getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize,
152                                                           getCurrentConfigForFeature_cb _hidl_cb) {
153     return mEffect->getCurrentConfigForFeature(featureId, configSize, _hidl_cb);
154 }
155 
setCurrentConfigForFeature(uint32_t featureId,const hidl_vec<uint8_t> & configData)156 Return<Result> VisualizerEffect::setCurrentConfigForFeature(uint32_t featureId,
157                                                             const hidl_vec<uint8_t>& configData) {
158     return mEffect->setCurrentConfigForFeature(featureId, configData);
159 }
160 
close()161 Return<Result> VisualizerEffect::close() {
162     return mEffect->close();
163 }
164 
debug(const hidl_handle & fd,const hidl_vec<hidl_string> & options)165 Return<void> VisualizerEffect::debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) {
166     return mEffect->debug(fd, options);
167 }
168 
169 // Methods from ::android::hardware::audio::effect::CPP_VERSION::IVisualizerEffect follow.
setCaptureSize(uint16_t captureSize)170 Return<Result> VisualizerEffect::setCaptureSize(uint16_t captureSize) {
171     Result retval = mEffect->setParam(VISUALIZER_PARAM_CAPTURE_SIZE, captureSize);
172     if (retval == Result::OK) {
173         mCaptureSize = captureSize;
174     }
175     return retval;
176 }
177 
getCaptureSize(getCaptureSize_cb _hidl_cb)178 Return<void> VisualizerEffect::getCaptureSize(getCaptureSize_cb _hidl_cb) {
179     return mEffect->getIntegerParam(VISUALIZER_PARAM_CAPTURE_SIZE, _hidl_cb);
180 }
181 
setScalingMode(IVisualizerEffect::ScalingMode scalingMode)182 Return<Result> VisualizerEffect::setScalingMode(IVisualizerEffect::ScalingMode scalingMode) {
183     return mEffect->setParam(VISUALIZER_PARAM_SCALING_MODE, static_cast<int32_t>(scalingMode));
184 }
185 
getScalingMode(getScalingMode_cb _hidl_cb)186 Return<void> VisualizerEffect::getScalingMode(getScalingMode_cb _hidl_cb) {
187     int32_t halMode;
188     Result retval = mEffect->getParam(VISUALIZER_PARAM_SCALING_MODE, halMode);
189     _hidl_cb(retval, ScalingMode(halMode));
190     return Void();
191 }
192 
setLatency(uint32_t latencyMs)193 Return<Result> VisualizerEffect::setLatency(uint32_t latencyMs) {
194     return mEffect->setParam(VISUALIZER_PARAM_LATENCY, latencyMs);
195 }
196 
getLatency(getLatency_cb _hidl_cb)197 Return<void> VisualizerEffect::getLatency(getLatency_cb _hidl_cb) {
198     return mEffect->getIntegerParam(VISUALIZER_PARAM_LATENCY, _hidl_cb);
199 }
200 
setMeasurementMode(IVisualizerEffect::MeasurementMode measurementMode)201 Return<Result> VisualizerEffect::setMeasurementMode(
202     IVisualizerEffect::MeasurementMode measurementMode) {
203     Result retval =
204         mEffect->setParam(VISUALIZER_PARAM_MEASUREMENT_MODE, static_cast<int32_t>(measurementMode));
205     if (retval == Result::OK) {
206         mMeasurementMode = measurementMode;
207     }
208     return retval;
209 }
210 
getMeasurementMode(getMeasurementMode_cb _hidl_cb)211 Return<void> VisualizerEffect::getMeasurementMode(getMeasurementMode_cb _hidl_cb) {
212     int32_t halMode;
213     Result retval = mEffect->getParam(VISUALIZER_PARAM_MEASUREMENT_MODE, halMode);
214     _hidl_cb(retval, MeasurementMode(halMode));
215     return Void();
216 }
217 
capture(capture_cb _hidl_cb)218 Return<void> VisualizerEffect::capture(capture_cb _hidl_cb) {
219     if (mCaptureSize == 0) {
220         _hidl_cb(Result::NOT_INITIALIZED, hidl_vec<uint8_t>());
221         return Void();
222     }
223     uint32_t halCaptureSize = mCaptureSize;
224     uint8_t halCapture[mCaptureSize];
225     Result retval = mEffect->sendCommandReturningData(VISUALIZER_CMD_CAPTURE, "VISUALIZER_CAPTURE",
226                                                       &halCaptureSize, halCapture);
227     hidl_vec<uint8_t> capture;
228     if (retval == Result::OK) {
229         capture.setToExternal(&halCapture[0], halCaptureSize);
230     }
231     _hidl_cb(retval, capture);
232     return Void();
233 }
234 
measure(measure_cb _hidl_cb)235 Return<void> VisualizerEffect::measure(measure_cb _hidl_cb) {
236     if (mMeasurementMode == MeasurementMode::NONE) {
237         _hidl_cb(Result::NOT_INITIALIZED, Measurement());
238         return Void();
239     }
240     int32_t halMeasurement[MEASUREMENT_COUNT];
241     uint32_t halMeasurementSize = sizeof(halMeasurement);
242     Result retval = mEffect->sendCommandReturningData(VISUALIZER_CMD_MEASURE, "VISUALIZER_MEASURE",
243                                                       &halMeasurementSize, halMeasurement);
244     Measurement measurement = {.mode = MeasurementMode::PEAK_RMS};
245     measurement.value.peakAndRms.peakMb = 0;
246     measurement.value.peakAndRms.rmsMb = 0;
247     if (retval == Result::OK) {
248         measurement.value.peakAndRms.peakMb = halMeasurement[MEASUREMENT_IDX_PEAK];
249         measurement.value.peakAndRms.rmsMb = halMeasurement[MEASUREMENT_IDX_RMS];
250     }
251     _hidl_cb(retval, measurement);
252     return Void();
253 }
254 
255 }  // namespace implementation
256 }  // namespace CPP_VERSION
257 }  // namespace effect
258 }  // namespace audio
259 }  // namespace hardware
260 }  // namespace android
261