blob: 395340ee6bceaee77058a4a3cb81b47b0d3f4545 [file] [log] [blame]
Andy Hung6ac17eb2023-06-20 18:56:17 -07001/*
2 * Copyright (C) 2023 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#pragma once
18
19namespace android {
20
Andy Hung55a74fd2023-07-13 19:54:47 -070021class DeviceEffectManagerCallback;
Andy Hung6ac17eb2023-06-20 18:56:17 -070022class IAfDeviceEffectProxy;
23class IAfEffectBase;
24class IAfEffectChain;
25class IAfEffectHandle;
26class IAfEffectModule;
Andy Hung87c693c2023-07-06 20:56:16 -070027class IAfThreadBase;
Andy Hung6ac17eb2023-06-20 18:56:17 -070028
29// Interface implemented by the EffectModule parent or owner (e.g an EffectChain) to abstract
30// interactions between the EffectModule and the reset of the audio framework.
31class EffectCallbackInterface : public RefBase {
32public:
33 // Trivial methods usually implemented with help from ThreadBase
34 virtual audio_io_handle_t io() const = 0;
35 virtual bool isOutput() const = 0;
36 virtual bool isOffload() const = 0;
37 virtual bool isOffloadOrDirect() const = 0;
38 virtual bool isOffloadOrMmap() const = 0;
39 virtual bool isSpatializer() const = 0;
40 virtual uint32_t sampleRate() const = 0;
41 virtual audio_channel_mask_t inChannelMask(int id) const = 0;
42 virtual uint32_t inChannelCount(int id) const = 0;
43 virtual audio_channel_mask_t outChannelMask() const = 0;
44 virtual uint32_t outChannelCount() const = 0;
45 virtual audio_channel_mask_t hapticChannelMask() const = 0;
46 virtual size_t frameCount() const = 0;
47
48 // Non trivial methods usually implemented with help from ThreadBase:
49 // pay attention to mutex locking order
50 virtual uint32_t latency() const { return 0; }
51 virtual status_t addEffectToHal(const sp<EffectHalInterface>& effect) = 0;
52 virtual status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) = 0;
53 virtual void setVolumeForOutput(float left, float right) const = 0;
54 virtual bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) = 0;
55 virtual void checkSuspendOnEffectEnabled(
56 const sp<IAfEffectBase>& effect, bool enabled, bool threadLocked) = 0;
57 virtual void onEffectEnable(const sp<IAfEffectBase>& effect) = 0;
58 virtual void onEffectDisable(const sp<IAfEffectBase>& effect) = 0;
59
60 // Methods usually implemented with help from AudioFlinger: pay attention to mutex locking order
61 virtual status_t createEffectHal(const effect_uuid_t *pEffectUuid,
62 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) = 0;
63 virtual status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) = 0;
64 virtual bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect) = 0;
65
66 // Methods usually implemented with help from EffectChain: pay attention to mutex locking order
67 virtual product_strategy_t strategy() const = 0;
68 virtual int32_t activeTrackCnt() const = 0;
69 virtual void resetVolume() = 0;
70 virtual wp<IAfEffectChain> chain() const = 0;
71 virtual bool isAudioPolicyReady() const = 0;
72};
73
74class IAfEffectBase : public virtual RefBase {
75 friend class EffectChain;
76 friend class EffectHandle;
77
78public:
79 enum effect_state {
80 IDLE,
81 RESTART,
82 STARTING,
83 ACTIVE,
84 STOPPING,
85 STOPPED,
86 DESTROYED
87 };
88 virtual int id() const = 0;
89 virtual effect_state state() const = 0;
90 virtual audio_session_t sessionId() const = 0;
91 virtual const effect_descriptor_t& desc() const = 0;
92 virtual bool isOffloadable() const = 0;
93 virtual bool isImplementationSoftware() const = 0;
94 virtual bool isProcessImplemented() const = 0;
95 virtual bool isVolumeControl() const = 0;
96 virtual bool isVolumeMonitor() const = 0;
97 virtual bool isEnabled() const = 0;
98 virtual bool isPinned() const = 0;
99 virtual void unPin() = 0;
100 virtual status_t updatePolicyState() = 0;
101 virtual bool purgeHandles() = 0;
102 virtual void checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) = 0;
103
104 // mCallback is atomic so this can be lock-free.
105 virtual void setCallback(const sp<EffectCallbackInterface>& callback) = 0;
106 virtual sp<EffectCallbackInterface> getCallback() const = 0;
107
108 virtual status_t addHandle(IAfEffectHandle *handle) = 0;
109 virtual ssize_t removeHandle(IAfEffectHandle *handle) = 0;
110
111 virtual sp<IAfEffectModule> asEffectModule() = 0;
112 virtual sp<IAfDeviceEffectProxy> asDeviceEffectProxy() = 0;
113
François Gaffiea2e985b2023-06-09 14:37:56 +0200114 virtual status_t command(int32_t cmdCode,
115 const std::vector<uint8_t>& cmdData,
116 int32_t maxReplySize,
117 std::vector<uint8_t>* reply) = 0;
118
Andy Hung6ac17eb2023-06-20 18:56:17 -0700119 virtual void dump(int fd, const Vector<String16>& args) const = 0;
120
121private:
122 virtual status_t setEnabled(bool enabled, bool fromHandle) = 0;
123 virtual status_t setEnabled_l(bool enabled) = 0;
124 virtual void setSuspended(bool suspended) = 0;
125 virtual bool suspended() const = 0;
126
Andy Hung6ac17eb2023-06-20 18:56:17 -0700127 virtual ssize_t disconnectHandle(IAfEffectHandle *handle, bool unpinIfLast) = 0;
128 virtual ssize_t removeHandle_l(IAfEffectHandle *handle) = 0;
129 virtual IAfEffectHandle* controlHandle_l() = 0;
130
131 virtual void lock() = 0;
132 virtual void unlock() = 0;
133};
134
135class IAfEffectModule : public virtual IAfEffectBase {
136 friend class DeviceEffectProxy;
137 friend class EffectChain;
138
139public:
140 static sp<IAfEffectModule> create(
141 const sp<EffectCallbackInterface>& callabck,
142 effect_descriptor_t *desc,
143 int id,
144 audio_session_t sessionId,
145 bool pinned,
146 audio_port_handle_t deviceId);
147
148 virtual int16_t *inBuffer() const = 0;
149 virtual status_t setDevices(const AudioDeviceTypeAddrVector &devices) = 0;
150 virtual status_t setInputDevice(const AudioDeviceTypeAddr &device) = 0;
151 virtual status_t setVolume(uint32_t *left, uint32_t *right, bool controller) = 0;
152 virtual status_t setOffloaded(bool offloaded, audio_io_handle_t io) = 0;
153 virtual bool isOffloaded() const = 0;
154
155 virtual status_t setAudioSource(audio_source_t source) = 0;
156 virtual status_t setMode(audio_mode_t mode) = 0;
157
158 virtual status_t start() = 0;
159 virtual status_t getConfigs(audio_config_base_t* inputCfg,
160 audio_config_base_t* outputCfg,
161 bool* isOutput) const = 0;
162
163 static bool isHapticGenerator(const effect_uuid_t* type);
164 virtual bool isHapticGenerator() const = 0;
165 virtual status_t setHapticIntensity(int id, os::HapticScale intensity) = 0;
166 virtual status_t setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo) = 0;
167
168private:
169 virtual void process() = 0;
170 virtual bool updateState() = 0;
171 virtual void reset_l() = 0;
172 virtual status_t configure() = 0;
173 virtual status_t init() = 0;
174 virtual uint32_t status() const = 0;
175 virtual bool isProcessEnabled() const = 0;
176 virtual bool isOffloadedOrDirect() const = 0;
177 virtual bool isVolumeControlEnabled() const = 0;
178
179 virtual void setInBuffer(const sp<EffectBufferHalInterface>& buffer) = 0;
180 virtual void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) = 0;
181 virtual int16_t *outBuffer() const = 0;
182
183 // Updates the access mode if it is out of date. May issue a new effect configure.
184 virtual void updateAccessMode() = 0;
185
186 virtual status_t stop() = 0;
187 virtual void addEffectToHal_l() = 0;
188 virtual void release_l() = 0;
189};
190
191class IAfEffectChain : public RefBase {
192 // Most of these methods are accessed from AudioFlinger::Thread
193public:
194 static sp<IAfEffectChain> create(
Andy Hung87c693c2023-07-06 20:56:16 -0700195 const wp<IAfThreadBase>& wThread,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700196 audio_session_t sessionId);
197
198 // special key used for an entry in mSuspendedEffects keyed vector
199 // corresponding to a suspend all request.
200 static constexpr int kKeyForSuspendAll = 0;
201
202 // minimum duration during which we force calling effect process when last track on
203 // a session is stopped or removed to allow effect tail to be rendered
204 static constexpr int kProcessTailDurationMs = 1000;
205
206 virtual void process_l() = 0;
207
208 virtual void lock() = 0;
209 virtual void unlock() = 0;
210
211 virtual status_t createEffect_l(sp<IAfEffectModule>& effect,
212 effect_descriptor_t *desc,
213 int id,
214 audio_session_t sessionId,
215 bool pinned) = 0;
216
217 virtual status_t addEffect_l(const sp<IAfEffectModule>& handle) = 0;
218 virtual status_t addEffect_ll(const sp<IAfEffectModule>& handle) = 0;
219 virtual size_t removeEffect_l(const sp<IAfEffectModule>& handle, bool release = false) = 0;
220
221 virtual audio_session_t sessionId() const = 0;
222 virtual void setSessionId(audio_session_t sessionId) = 0;
223
224 virtual sp<IAfEffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor) const = 0;
225 virtual sp<IAfEffectModule> getEffectFromId_l(int id) const = 0;
226 virtual sp<IAfEffectModule> getEffectFromType_l(const effect_uuid_t *type) const = 0;
227 virtual std::vector<int> getEffectIds() const = 0;
228 virtual bool setVolume_l(uint32_t *left, uint32_t *right, bool force = false) = 0;
229 virtual void resetVolume_l() = 0;
230 virtual void setDevices_l(const AudioDeviceTypeAddrVector &devices) = 0;
231 virtual void setInputDevice_l(const AudioDeviceTypeAddr &device) = 0;
232 virtual void setMode_l(audio_mode_t mode) = 0;
233 virtual void setAudioSource_l(audio_source_t source) = 0;
234
235 virtual void setInBuffer(const sp<EffectBufferHalInterface>& buffer) = 0;
236 virtual float *inBuffer() const = 0;
237 virtual void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) = 0;
238 virtual float *outBuffer() const = 0;
239
240 virtual void incTrackCnt() = 0;
241 virtual void decTrackCnt() = 0;
242 virtual int32_t trackCnt() const = 0;
243
244 virtual void incActiveTrackCnt() = 0;
245 virtual void decActiveTrackCnt() = 0;
246 virtual int32_t activeTrackCnt() const = 0;
247
248 virtual product_strategy_t strategy() const = 0;
249 virtual void setStrategy(product_strategy_t strategy) = 0;
250
251 // suspend or restore effects of the specified type. The number of suspend requests is counted
252 // and restore occurs once all suspend requests are cancelled.
253 virtual void setEffectSuspended_l(
254 const effect_uuid_t *type, bool suspend) = 0;
255 // suspend all eligible effects
256 virtual void setEffectSuspendedAll_l(bool suspend) = 0;
257 // check if effects should be suspended or restored when a given effect is enable or disabled
258 virtual void checkSuspendOnEffectEnabled(const sp<IAfEffectModule>& effect, bool enabled) = 0;
259
260 virtual void clearInputBuffer() = 0;
261
262 // At least one non offloadable effect in the chain is enabled
263 virtual bool isNonOffloadableEnabled() const = 0;
264 virtual bool isNonOffloadableEnabled_l() const = 0;
265
266 virtual void syncHalEffectsState() = 0;
267
268 // flags is an ORed set of audio_output_flags_t which is updated on return.
269 virtual void checkOutputFlagCompatibility(audio_output_flags_t *flags) const = 0;
270
271 // flags is an ORed set of audio_input_flags_t which is updated on return.
272 virtual void checkInputFlagCompatibility(audio_input_flags_t *flags) const = 0;
273
274 // Is this EffectChain compatible with the RAW audio flag.
275 virtual bool isRawCompatible() const = 0;
276
277 // Is this EffectChain compatible with the FAST audio flag.
278 virtual bool isFastCompatible() const = 0;
279
280 // Is this EffectChain compatible with the bit-perfect audio flag.
281 virtual bool isBitPerfectCompatible() const = 0;
282
283 // isCompatibleWithThread_l() must be called with thread->mLock held
Andy Hung87c693c2023-07-06 20:56:16 -0700284 virtual bool isCompatibleWithThread_l(const sp<IAfThreadBase>& thread) const = 0;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700285
286 virtual bool containsHapticGeneratingEffect_l() = 0;
287
288 virtual void setHapticIntensity_l(int id, os::HapticScale intensity) = 0;
289
290 virtual sp<EffectCallbackInterface> effectCallback() const = 0;
291
Andy Hung87c693c2023-07-06 20:56:16 -0700292 virtual wp<IAfThreadBase> thread() const = 0;
293 virtual void setThread(const sp<IAfThreadBase>& thread) = 0;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700294
295 virtual bool isFirstEffect(int id) const = 0;
296
297 virtual size_t numberOfEffects() const = 0;
298 virtual sp<IAfEffectModule> getEffectModule(size_t index) const = 0;
299
300 virtual void dump(int fd, const Vector<String16>& args) const = 0;
301};
302
303class IAfEffectHandle : public virtual RefBase {
304 friend class EffectBase;
305 friend class EffectChain;
306 friend class EffectModule;
307
308public:
309 static sp<IAfEffectHandle> create(
310 const sp<IAfEffectBase>& effect,
Andy Hung59867e42023-06-27 17:05:02 -0700311 const sp<Client>& client,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700312 const sp<media::IEffectClient>& effectClient,
313 int32_t priority, bool notifyFramesProcessed);
314
315 virtual status_t initCheck() const = 0;
316 virtual bool enabled() const = 0;
317 virtual int id() const = 0;
318 virtual wp<IAfEffectBase> effect() const = 0;
319 virtual sp<android::media::IEffect> asIEffect() = 0;
Andy Hung59867e42023-06-27 17:05:02 -0700320 virtual const sp<Client>& client() const = 0;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700321
322private:
323 virtual void setControl(bool hasControl, bool signal, bool enabled) = 0;
324 virtual bool hasControl() const = 0;
325 virtual void setEnabled(bool enabled) = 0;
326 virtual bool disconnected() const = 0;
327 virtual int priority() const = 0;
328
329 virtual void commandExecuted(uint32_t cmdCode,
330 const std::vector<uint8_t>& cmdData,
331 const std::vector<uint8_t>& replyData) = 0;
332 virtual void framesProcessed(int32_t frames) const = 0;
333
334 virtual void dumpToBuffer(char* buffer, size_t size) const = 0;
335};
336
337class IAfDeviceEffectProxy : public virtual IAfEffectBase {
338public:
Andy Hung6ac17eb2023-06-20 18:56:17 -0700339 static sp<IAfDeviceEffectProxy> create(const AudioDeviceTypeAddr& device,
Andy Hung55a74fd2023-07-13 19:54:47 -0700340 const sp<DeviceEffectManagerCallback>& callback,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700341 effect_descriptor_t *desc, int id, bool notifyFramesProcessed);
342
343 virtual status_t init(
Andy Hung8e6b62a2023-07-13 18:11:33 -0700344 const std::map<audio_patch_handle_t,
345 IAfPatchPanel::Patch>& patches) = 0;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700346 virtual const AudioDeviceTypeAddr& device() const = 0;
347
348 virtual status_t onCreatePatch(
349 audio_patch_handle_t patchHandle,
Andy Hung8e6b62a2023-07-13 18:11:33 -0700350 const IAfPatchPanel::Patch& patch) = 0;
François Gaffie58e73af2023-02-15 11:47:24 +0100351 virtual status_t onUpdatePatch(audio_patch_handle_t oldPatchHandle,
352 audio_patch_handle_t newPatchHandle,
Andy Hung8e6b62a2023-07-13 18:11:33 -0700353 const IAfPatchPanel::Patch& patch) = 0;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700354 virtual void onReleasePatch(audio_patch_handle_t patchHandle) = 0;
355
356 virtual void dump2(int fd, int spaces) const = 0; // TODO(b/288339104) naming?
357
358private:
359 // used by DeviceEffectProxy
360 virtual bool isOutput() const = 0;
361 virtual uint32_t sampleRate() const = 0;
362 virtual audio_channel_mask_t channelMask() const = 0;
363 virtual uint32_t channelCount() const = 0;
364
365 virtual size_t removeEffect(const sp<IAfEffectModule>& effect) = 0;
366 virtual status_t addEffectToHal(const sp<EffectHalInterface>& effect) = 0;
367 virtual status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) = 0;
368};
369
Andy Hungee58e4a2023-07-07 13:47:37 -0700370} // namespace android