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