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