• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ANDROID_HARDWARE_AUDIO_EFFECT_EFFECT_H
18 #define ANDROID_HARDWARE_AUDIO_EFFECT_EFFECT_H
19 
20 #include PATH(android/hardware/audio/effect/FILE_VERSION/IEffect.h)
21 
22 #include "AudioBufferManager.h"
23 
24 #include <atomic>
25 #include <memory>
26 #include <vector>
27 
28 #include <fmq/EventFlag.h>
29 #include <fmq/MessageQueue.h>
30 #include <hidl/MQDescriptor.h>
31 #include <hidl/Status.h>
32 #include <utils/Thread.h>
33 
34 #include <hardware/audio_effect.h>
35 
36 #include "VersionUtils.h"
37 
38 namespace android {
39 namespace hardware {
40 namespace audio {
41 namespace effect {
42 namespace CPP_VERSION {
43 namespace implementation {
44 
45 using ::android::sp;
46 using ::android::hardware::hidl_string;
47 using ::android::hardware::hidl_vec;
48 using ::android::hardware::Return;
49 using ::android::hardware::Void;
50 using ::android::hardware::audio::common::CPP_VERSION::implementation::AudioDeviceBitfield;
51 using namespace ::android::hardware::audio::common::CPP_VERSION;
52 using namespace ::android::hardware::audio::effect::CPP_VERSION;
53 
54 struct Effect : public IEffect {
55     typedef MessageQueue<Result, kSynchronizedReadWrite> StatusMQ;
56     using GetParameterSuccessCallback =
57         std::function<void(uint32_t valueSize, const void* valueData)>;
58 
59     explicit Effect(effect_handle_t handle);
60 
61     // Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow.
62     Return<Result> init() override;
63     Return<Result> setConfig(
64         const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
65         const sp<IEffectBufferProviderCallback>& outputBufferProvider) override;
66     Return<Result> reset() override;
67     Return<Result> enable() override;
68     Return<Result> disable() override;
69     Return<Result> setDevice(AudioDeviceBitfield device) override;
70     Return<void> setAndGetVolume(const hidl_vec<uint32_t>& volumes,
71                                  setAndGetVolume_cb _hidl_cb) override;
72     Return<Result> volumeChangeNotification(const hidl_vec<uint32_t>& volumes) override;
73     Return<Result> setAudioMode(AudioMode mode) override;
74     Return<Result> setConfigReverse(
75         const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider,
76         const sp<IEffectBufferProviderCallback>& outputBufferProvider) override;
77     Return<Result> setInputDevice(AudioDeviceBitfield device) override;
78     Return<void> getConfig(getConfig_cb _hidl_cb) override;
79     Return<void> getConfigReverse(getConfigReverse_cb _hidl_cb) override;
80     Return<void> getSupportedAuxChannelsConfigs(
81         uint32_t maxConfigs, getSupportedAuxChannelsConfigs_cb _hidl_cb) override;
82     Return<void> getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) override;
83     Return<Result> setAuxChannelsConfig(const EffectAuxChannelsConfig& config) override;
84     Return<Result> setAudioSource(AudioSource source) override;
85     Return<Result> offload(const EffectOffloadParameter& param) override;
86     Return<void> getDescriptor(getDescriptor_cb _hidl_cb) override;
87     Return<void> prepareForProcessing(prepareForProcessing_cb _hidl_cb) override;
88     Return<Result> setProcessBuffers(const AudioBuffer& inBuffer,
89                                      const AudioBuffer& outBuffer) override;
90     Return<void> command(uint32_t commandId, const hidl_vec<uint8_t>& data, uint32_t resultMaxSize,
91                          command_cb _hidl_cb) override;
92     Return<Result> setParameter(const hidl_vec<uint8_t>& parameter,
93                                 const hidl_vec<uint8_t>& value) override;
94     Return<void> getParameter(const hidl_vec<uint8_t>& parameter, uint32_t valueMaxSize,
95                               getParameter_cb _hidl_cb) override;
96     Return<void> getSupportedConfigsForFeature(uint32_t featureId, uint32_t maxConfigs,
97                                                uint32_t configSize,
98                                                getSupportedConfigsForFeature_cb _hidl_cb) override;
99     Return<void> getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize,
100                                             getCurrentConfigForFeature_cb _hidl_cb) override;
101     Return<Result> setCurrentConfigForFeature(uint32_t featureId,
102                                               const hidl_vec<uint8_t>& configData) override;
103     Return<Result> close() override;
104     Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override;
105 
106     // Utility methods for extending interfaces.
107     template <typename T>
getIntegerParamEffect108     Return<void> getIntegerParam(uint32_t paramId,
109                                  std::function<void(Result retval, T paramValue)> cb) {
110         T value;
111         Result retval = getParameterImpl(sizeof(uint32_t), &paramId, sizeof(T),
112                                          [&](uint32_t valueSize, const void* valueData) {
113                                              if (valueSize > sizeof(T)) valueSize = sizeof(T);
114                                              memcpy(&value, valueData, valueSize);
115                                          });
116         cb(retval, value);
117         return Void();
118     }
119 
120     template <typename T>
getParamEffect121     Result getParam(uint32_t paramId, T& paramValue) {
122         return getParameterImpl(sizeof(uint32_t), &paramId, sizeof(T),
123                                 [&](uint32_t valueSize, const void* valueData) {
124                                     if (valueSize > sizeof(T)) valueSize = sizeof(T);
125                                     memcpy(&paramValue, valueData, valueSize);
126                                 });
127     }
128 
129     template <typename T>
getParamEffect130     Result getParam(uint32_t paramId, uint32_t paramArg, T& paramValue) {
131         uint32_t params[2] = {paramId, paramArg};
132         return getParameterImpl(sizeof(params), params, sizeof(T),
133                                 [&](uint32_t valueSize, const void* valueData) {
134                                     if (valueSize > sizeof(T)) valueSize = sizeof(T);
135                                     memcpy(&paramValue, valueData, valueSize);
136                                 });
137     }
138 
139     template <typename T>
setParamEffect140     Result setParam(uint32_t paramId, const T& paramValue) {
141         return setParameterImpl(sizeof(uint32_t), &paramId, sizeof(T), &paramValue);
142     }
143 
144     template <typename T>
setParamEffect145     Result setParam(uint32_t paramId, uint32_t paramArg, const T& paramValue) {
146         uint32_t params[2] = {paramId, paramArg};
147         return setParameterImpl(sizeof(params), params, sizeof(T), &paramValue);
148     }
149 
getParameterImplEffect150     Result getParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize,
151                             GetParameterSuccessCallback onSuccess) {
152         return getParameterImpl(paramSize, paramData, valueSize, valueSize, onSuccess);
153     }
154     Result getParameterImpl(uint32_t paramSize, const void* paramData, uint32_t requestValueSize,
155                             uint32_t replyValueSize, GetParameterSuccessCallback onSuccess);
156     Result setParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize,
157                             const void* valueData);
158 
159    private:
160     friend struct VirtualizerEffect;  // for getParameterImpl
161     friend struct VisualizerEffect;   // to allow executing commands
162 
163     using CommandSuccessCallback = std::function<void()>;
164     using GetConfigCallback = std::function<void(Result retval, const EffectConfig& config)>;
165     using GetCurrentConfigSuccessCallback = std::function<void(void* configData)>;
166     using GetSupportedConfigsSuccessCallback =
167         std::function<void(uint32_t supportedConfigs, void* configsData)>;
168 
169     static const char* sContextResultOfCommand;
170     static const char* sContextCallToCommand;
171     static const char* sContextCallFunction;
172 
173     bool mIsClosed;
174     effect_handle_t mHandle;
175     sp<AudioBufferWrapper> mInBuffer;
176     sp<AudioBufferWrapper> mOutBuffer;
177     std::atomic<audio_buffer_t*> mHalInBufferPtr;
178     std::atomic<audio_buffer_t*> mHalOutBufferPtr;
179     std::unique_ptr<StatusMQ> mStatusMQ;
180     EventFlag* mEfGroup;
181     std::atomic<bool> mStopProcessThread;
182     sp<Thread> mProcessThread;
183 
184     virtual ~Effect();
185 
186     template <typename T>
187     static size_t alignedSizeIn(size_t s);
188     template <typename T>
189     std::unique_ptr<uint8_t[]> hidlVecToHal(const hidl_vec<T>& vec, uint32_t* halDataSize);
190     static void effectAuxChannelsConfigFromHal(const channel_config_t& halConfig,
191                                                EffectAuxChannelsConfig* config);
192     static void effectAuxChannelsConfigToHal(const EffectAuxChannelsConfig& config,
193                                              channel_config_t* halConfig);
194     static void effectBufferConfigFromHal(const buffer_config_t& halConfig,
195                                           EffectBufferConfig* config);
196     static void effectBufferConfigToHal(const EffectBufferConfig& config,
197                                         buffer_config_t* halConfig);
198     static void effectConfigFromHal(const effect_config_t& halConfig, EffectConfig* config);
199     static void effectConfigToHal(const EffectConfig& config, effect_config_t* halConfig);
200     static void effectOffloadParamToHal(const EffectOffloadParameter& offload,
201                                         effect_offload_param_t* halOffload);
202     static std::vector<uint8_t> parameterToHal(uint32_t paramSize, const void* paramData,
203                                                uint32_t valueSize, const void** valueData);
204 
205     Result analyzeCommandStatus(const char* commandName, const char* context, status_t status);
206     Result analyzeStatus(const char* funcName, const char* subFuncName,
207                          const char* contextDescription, status_t status);
208     void getConfigImpl(int commandCode, const char* commandName, GetConfigCallback cb);
209     Result getCurrentConfigImpl(uint32_t featureId, uint32_t configSize,
210                                 GetCurrentConfigSuccessCallback onSuccess);
211     Result getSupportedConfigsImpl(uint32_t featureId, uint32_t maxConfigs, uint32_t configSize,
212                                    GetSupportedConfigsSuccessCallback onSuccess);
213     Result sendCommand(int commandCode, const char* commandName);
214     Result sendCommand(int commandCode, const char* commandName, uint32_t size, void* data);
215     Result sendCommandReturningData(int commandCode, const char* commandName, uint32_t* replySize,
216                                     void* replyData);
217     Result sendCommandReturningData(int commandCode, const char* commandName, uint32_t size,
218                                     void* data, uint32_t* replySize, void* replyData);
219     Result sendCommandReturningStatus(int commandCode, const char* commandName);
220     Result sendCommandReturningStatus(int commandCode, const char* commandName, uint32_t size,
221                                       void* data);
222     Result sendCommandReturningStatusAndData(int commandCode, const char* commandName,
223                                              uint32_t size, void* data, uint32_t* replySize,
224                                              void* replyData, uint32_t minReplySize,
225                                              CommandSuccessCallback onSuccess);
226     Result setConfigImpl(int commandCode, const char* commandName, const EffectConfig& config,
227                          const sp<IEffectBufferProviderCallback>& inputBufferProvider,
228                          const sp<IEffectBufferProviderCallback>& outputBufferProvider);
229 };
230 
231 }  // namespace implementation
232 }  // namespace CPP_VERSION
233 }  // namespace effect
234 }  // namespace audio
235 }  // namespace hardware
236 }  // namespace android
237 
238 #endif  // ANDROID_HARDWARE_AUDIO_EFFECT_EFFECT_H
239