blob: d02fbb7471d964c4be544783341dc3842b55200d [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
Andy Hung0a51b5c2023-07-18 20:54:44 -070019#include "IAfPatchPanel.h" // full class Patch definition needed
20
21#include <android/media/AudioVibratorInfo.h>
22#include <android/media/BnEffect.h>
23#include <android/media/BnEffectClient.h>
Andy Hungf65f5a72023-08-29 12:19:17 -070024#include <audio_utils/mutex.h>
Andy Hung0a51b5c2023-07-18 20:54:44 -070025#include <media/AudioCommonTypes.h> // product_strategy_t
26#include <media/AudioDeviceTypeAddr.h>
27#include <media/audiohal/EffectHalInterface.h>
28#include <utils/RefBase.h>
29#include <vibrator/ExternalVibration.h>
30
Andy Hung6ac17eb2023-06-20 18:56:17 -070031namespace android {
32
Andy Hung0a51b5c2023-07-18 20:54:44 -070033class Client;
Andy Hung55a74fd2023-07-13 19:54:47 -070034class DeviceEffectManagerCallback;
Andy Hung0a51b5c2023-07-18 20:54:44 -070035
Andy Hung6ac17eb2023-06-20 18:56:17 -070036class IAfDeviceEffectProxy;
37class IAfEffectBase;
38class IAfEffectChain;
39class IAfEffectHandle;
40class IAfEffectModule;
Andy Hung87c693c2023-07-06 20:56:16 -070041class IAfThreadBase;
Andy Hung6ac17eb2023-06-20 18:56:17 -070042
43// Interface implemented by the EffectModule parent or owner (e.g an EffectChain) to abstract
44// interactions between the EffectModule and the reset of the audio framework.
45class EffectCallbackInterface : public RefBase {
46public:
47 // Trivial methods usually implemented with help from ThreadBase
48 virtual audio_io_handle_t io() const = 0;
49 virtual bool isOutput() const = 0;
50 virtual bool isOffload() const = 0;
51 virtual bool isOffloadOrDirect() const = 0;
52 virtual bool isOffloadOrMmap() const = 0;
53 virtual bool isSpatializer() const = 0;
54 virtual uint32_t sampleRate() const = 0;
55 virtual audio_channel_mask_t inChannelMask(int id) const = 0;
56 virtual uint32_t inChannelCount(int id) const = 0;
57 virtual audio_channel_mask_t outChannelMask() const = 0;
58 virtual uint32_t outChannelCount() const = 0;
59 virtual audio_channel_mask_t hapticChannelMask() const = 0;
60 virtual size_t frameCount() const = 0;
61
62 // Non trivial methods usually implemented with help from ThreadBase:
63 // pay attention to mutex locking order
64 virtual uint32_t latency() const { return 0; }
65 virtual status_t addEffectToHal(const sp<EffectHalInterface>& effect) = 0;
66 virtual status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) = 0;
67 virtual void setVolumeForOutput(float left, float right) const = 0;
68 virtual bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) = 0;
69 virtual void checkSuspendOnEffectEnabled(
70 const sp<IAfEffectBase>& effect, bool enabled, bool threadLocked) = 0;
71 virtual void onEffectEnable(const sp<IAfEffectBase>& effect) = 0;
72 virtual void onEffectDisable(const sp<IAfEffectBase>& effect) = 0;
73
74 // Methods usually implemented with help from AudioFlinger: pay attention to mutex locking order
75 virtual status_t createEffectHal(const effect_uuid_t *pEffectUuid,
76 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) = 0;
77 virtual status_t allocateHalBuffer(size_t size, sp<EffectBufferHalInterface>* buffer) = 0;
78 virtual bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect) = 0;
79
80 // Methods usually implemented with help from EffectChain: pay attention to mutex locking order
81 virtual product_strategy_t strategy() const = 0;
82 virtual int32_t activeTrackCnt() const = 0;
83 virtual void resetVolume() = 0;
84 virtual wp<IAfEffectChain> chain() const = 0;
85 virtual bool isAudioPolicyReady() const = 0;
86};
87
88class IAfEffectBase : public virtual RefBase {
89 friend class EffectChain;
90 friend class EffectHandle;
91
92public:
93 enum effect_state {
94 IDLE,
95 RESTART,
96 STARTING,
97 ACTIVE,
98 STOPPING,
99 STOPPED,
100 DESTROYED
101 };
102 virtual int id() const = 0;
103 virtual effect_state state() const = 0;
104 virtual audio_session_t sessionId() const = 0;
105 virtual const effect_descriptor_t& desc() const = 0;
106 virtual bool isOffloadable() const = 0;
107 virtual bool isImplementationSoftware() const = 0;
108 virtual bool isProcessImplemented() const = 0;
109 virtual bool isVolumeControl() const = 0;
110 virtual bool isVolumeMonitor() const = 0;
111 virtual bool isEnabled() const = 0;
112 virtual bool isPinned() const = 0;
113 virtual void unPin() = 0;
114 virtual status_t updatePolicyState() = 0;
115 virtual bool purgeHandles() = 0;
116 virtual void checkSuspendOnEffectEnabled(bool enabled, bool threadLocked) = 0;
117
118 // mCallback is atomic so this can be lock-free.
119 virtual void setCallback(const sp<EffectCallbackInterface>& callback) = 0;
120 virtual sp<EffectCallbackInterface> getCallback() const = 0;
121
122 virtual status_t addHandle(IAfEffectHandle *handle) = 0;
123 virtual ssize_t removeHandle(IAfEffectHandle *handle) = 0;
124
125 virtual sp<IAfEffectModule> asEffectModule() = 0;
126 virtual sp<IAfDeviceEffectProxy> asDeviceEffectProxy() = 0;
127
François Gaffiea2e985b2023-06-09 14:37:56 +0200128 virtual status_t command(int32_t cmdCode,
129 const std::vector<uint8_t>& cmdData,
130 int32_t maxReplySize,
131 std::vector<uint8_t>* reply) = 0;
132
Andy Hung6ac17eb2023-06-20 18:56:17 -0700133 virtual void dump(int fd, const Vector<String16>& args) const = 0;
134
135private:
136 virtual status_t setEnabled(bool enabled, bool fromHandle) = 0;
137 virtual status_t setEnabled_l(bool enabled) = 0;
138 virtual void setSuspended(bool suspended) = 0;
139 virtual bool suspended() const = 0;
140
Andy Hung6ac17eb2023-06-20 18:56:17 -0700141 virtual ssize_t disconnectHandle(IAfEffectHandle *handle, bool unpinIfLast) = 0;
142 virtual ssize_t removeHandle_l(IAfEffectHandle *handle) = 0;
143 virtual IAfEffectHandle* controlHandle_l() = 0;
144
Andy Hungf65f5a72023-08-29 12:19:17 -0700145 virtual audio_utils::mutex& mutex() const = 0;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700146};
147
148class IAfEffectModule : public virtual IAfEffectBase {
149 friend class DeviceEffectProxy;
150 friend class EffectChain;
151
152public:
153 static sp<IAfEffectModule> create(
154 const sp<EffectCallbackInterface>& callabck,
155 effect_descriptor_t *desc,
156 int id,
157 audio_session_t sessionId,
158 bool pinned,
159 audio_port_handle_t deviceId);
160
161 virtual int16_t *inBuffer() const = 0;
162 virtual status_t setDevices(const AudioDeviceTypeAddrVector &devices) = 0;
163 virtual status_t setInputDevice(const AudioDeviceTypeAddr &device) = 0;
164 virtual status_t setVolume(uint32_t *left, uint32_t *right, bool controller) = 0;
165 virtual status_t setOffloaded(bool offloaded, audio_io_handle_t io) = 0;
166 virtual bool isOffloaded() const = 0;
167
168 virtual status_t setAudioSource(audio_source_t source) = 0;
169 virtual status_t setMode(audio_mode_t mode) = 0;
170
171 virtual status_t start() = 0;
172 virtual status_t getConfigs(audio_config_base_t* inputCfg,
173 audio_config_base_t* outputCfg,
174 bool* isOutput) const = 0;
175
176 static bool isHapticGenerator(const effect_uuid_t* type);
177 virtual bool isHapticGenerator() const = 0;
Eric Laurent4eb45d02023-12-20 12:07:17 +0100178 static bool isSpatializer(const effect_uuid_t* type);
179 virtual bool isSpatializer() const = 0;
180
Andy Hung6ac17eb2023-06-20 18:56:17 -0700181 virtual status_t setHapticIntensity(int id, os::HapticScale intensity) = 0;
182 virtual status_t setVibratorInfo(const media::AudioVibratorInfo& vibratorInfo) = 0;
Eric Laurent4eb45d02023-12-20 12:07:17 +0100183 virtual status_t sendMetadata(const std::vector<playback_track_metadata_v7_t>& metadata) = 0;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700184
185private:
186 virtual void process() = 0;
187 virtual bool updateState() = 0;
188 virtual void reset_l() = 0;
189 virtual status_t configure() = 0;
190 virtual status_t init() = 0;
191 virtual uint32_t status() const = 0;
192 virtual bool isProcessEnabled() const = 0;
193 virtual bool isOffloadedOrDirect() const = 0;
194 virtual bool isVolumeControlEnabled() const = 0;
195
196 virtual void setInBuffer(const sp<EffectBufferHalInterface>& buffer) = 0;
197 virtual void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) = 0;
198 virtual int16_t *outBuffer() const = 0;
199
200 // Updates the access mode if it is out of date. May issue a new effect configure.
201 virtual void updateAccessMode() = 0;
202
203 virtual status_t stop() = 0;
204 virtual void addEffectToHal_l() = 0;
205 virtual void release_l() = 0;
206};
207
208class IAfEffectChain : public RefBase {
209 // Most of these methods are accessed from AudioFlinger::Thread
210public:
211 static sp<IAfEffectChain> create(
Andy Hung583043b2023-07-17 17:05:00 -0700212 const sp<IAfThreadBase>& thread,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700213 audio_session_t sessionId);
214
215 // special key used for an entry in mSuspendedEffects keyed vector
216 // corresponding to a suspend all request.
217 static constexpr int kKeyForSuspendAll = 0;
218
219 // minimum duration during which we force calling effect process when last track on
220 // a session is stopped or removed to allow effect tail to be rendered
221 static constexpr int kProcessTailDurationMs = 1000;
222
223 virtual void process_l() = 0;
224
Shunkai Yaof4847652024-01-12 00:25:20 +0000225 virtual audio_utils::mutex& mutex() const
226 RETURN_CAPABILITY(android::audio_utils::EffectChain_Mutex) = 0;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700227
228 virtual status_t createEffect_l(sp<IAfEffectModule>& effect,
229 effect_descriptor_t *desc,
230 int id,
231 audio_session_t sessionId,
232 bool pinned) = 0;
233
234 virtual status_t addEffect_l(const sp<IAfEffectModule>& handle) = 0;
235 virtual status_t addEffect_ll(const sp<IAfEffectModule>& handle) = 0;
236 virtual size_t removeEffect_l(const sp<IAfEffectModule>& handle, bool release = false) = 0;
237
238 virtual audio_session_t sessionId() const = 0;
239 virtual void setSessionId(audio_session_t sessionId) = 0;
240
241 virtual sp<IAfEffectModule> getEffectFromDesc_l(effect_descriptor_t *descriptor) const = 0;
242 virtual sp<IAfEffectModule> getEffectFromId_l(int id) const = 0;
243 virtual sp<IAfEffectModule> getEffectFromType_l(const effect_uuid_t *type) const = 0;
244 virtual std::vector<int> getEffectIds() const = 0;
Shunkai Yaof4847652024-01-12 00:25:20 +0000245 virtual bool setVolume(uint32_t* left, uint32_t* right,
246 bool force = false) EXCLUDES_EffectChain_Mutex = 0;
247 virtual void resetVolume_l() REQUIRES(audio_utils::EffectChain_Mutex) = 0;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700248 virtual void setDevices_l(const AudioDeviceTypeAddrVector &devices) = 0;
249 virtual void setInputDevice_l(const AudioDeviceTypeAddr &device) = 0;
250 virtual void setMode_l(audio_mode_t mode) = 0;
251 virtual void setAudioSource_l(audio_source_t source) = 0;
252
253 virtual void setInBuffer(const sp<EffectBufferHalInterface>& buffer) = 0;
254 virtual float *inBuffer() const = 0;
255 virtual void setOutBuffer(const sp<EffectBufferHalInterface>& buffer) = 0;
256 virtual float *outBuffer() const = 0;
257
258 virtual void incTrackCnt() = 0;
259 virtual void decTrackCnt() = 0;
260 virtual int32_t trackCnt() const = 0;
261
262 virtual void incActiveTrackCnt() = 0;
263 virtual void decActiveTrackCnt() = 0;
264 virtual int32_t activeTrackCnt() const = 0;
265
266 virtual product_strategy_t strategy() const = 0;
267 virtual void setStrategy(product_strategy_t strategy) = 0;
268
269 // suspend or restore effects of the specified type. The number of suspend requests is counted
270 // and restore occurs once all suspend requests are cancelled.
271 virtual void setEffectSuspended_l(
272 const effect_uuid_t *type, bool suspend) = 0;
273 // suspend all eligible effects
274 virtual void setEffectSuspendedAll_l(bool suspend) = 0;
275 // check if effects should be suspended or restored when a given effect is enable or disabled
276 virtual void checkSuspendOnEffectEnabled(const sp<IAfEffectModule>& effect, bool enabled) = 0;
277
278 virtual void clearInputBuffer() = 0;
279
280 // At least one non offloadable effect in the chain is enabled
281 virtual bool isNonOffloadableEnabled() const = 0;
282 virtual bool isNonOffloadableEnabled_l() const = 0;
283
284 virtual void syncHalEffectsState() = 0;
285
286 // flags is an ORed set of audio_output_flags_t which is updated on return.
287 virtual void checkOutputFlagCompatibility(audio_output_flags_t *flags) const = 0;
288
289 // flags is an ORed set of audio_input_flags_t which is updated on return.
290 virtual void checkInputFlagCompatibility(audio_input_flags_t *flags) const = 0;
291
292 // Is this EffectChain compatible with the RAW audio flag.
293 virtual bool isRawCompatible() const = 0;
294
295 // Is this EffectChain compatible with the FAST audio flag.
296 virtual bool isFastCompatible() const = 0;
297
298 // Is this EffectChain compatible with the bit-perfect audio flag.
299 virtual bool isBitPerfectCompatible() const = 0;
300
301 // isCompatibleWithThread_l() must be called with thread->mLock held
Andy Hung87c693c2023-07-06 20:56:16 -0700302 virtual bool isCompatibleWithThread_l(const sp<IAfThreadBase>& thread) const = 0;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700303
304 virtual bool containsHapticGeneratingEffect_l() = 0;
305
306 virtual void setHapticIntensity_l(int id, os::HapticScale intensity) = 0;
307
308 virtual sp<EffectCallbackInterface> effectCallback() const = 0;
309
Andy Hung87c693c2023-07-06 20:56:16 -0700310 virtual wp<IAfThreadBase> thread() const = 0;
311 virtual void setThread(const sp<IAfThreadBase>& thread) = 0;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700312
313 virtual bool isFirstEffect(int id) const = 0;
314
315 virtual size_t numberOfEffects() const = 0;
316 virtual sp<IAfEffectModule> getEffectModule(size_t index) const = 0;
317
Eric Laurent4eb45d02023-12-20 12:07:17 +0100318 // sendMetadata_l() must be called with thread->mLock held
319 virtual void sendMetadata_l(const std::vector<playback_track_metadata_v7_t>& allMetadata,
320 const std::optional<const std::vector<playback_track_metadata_v7_t>> spatializedMetadata);
321
Andy Hung6ac17eb2023-06-20 18:56:17 -0700322 virtual void dump(int fd, const Vector<String16>& args) const = 0;
323};
324
325class IAfEffectHandle : public virtual RefBase {
326 friend class EffectBase;
327 friend class EffectChain;
328 friend class EffectModule;
329
330public:
331 static sp<IAfEffectHandle> create(
332 const sp<IAfEffectBase>& effect,
Andy Hung59867e42023-06-27 17:05:02 -0700333 const sp<Client>& client,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700334 const sp<media::IEffectClient>& effectClient,
335 int32_t priority, bool notifyFramesProcessed);
336
337 virtual status_t initCheck() const = 0;
338 virtual bool enabled() const = 0;
339 virtual int id() const = 0;
340 virtual wp<IAfEffectBase> effect() const = 0;
341 virtual sp<android::media::IEffect> asIEffect() = 0;
Andy Hung59867e42023-06-27 17:05:02 -0700342 virtual const sp<Client>& client() const = 0;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700343
344private:
345 virtual void setControl(bool hasControl, bool signal, bool enabled) = 0;
346 virtual bool hasControl() const = 0;
347 virtual void setEnabled(bool enabled) = 0;
348 virtual bool disconnected() const = 0;
349 virtual int priority() const = 0;
350
351 virtual void commandExecuted(uint32_t cmdCode,
352 const std::vector<uint8_t>& cmdData,
353 const std::vector<uint8_t>& replyData) = 0;
354 virtual void framesProcessed(int32_t frames) const = 0;
355
356 virtual void dumpToBuffer(char* buffer, size_t size) const = 0;
357};
358
359class IAfDeviceEffectProxy : public virtual IAfEffectBase {
360public:
Andy Hung6ac17eb2023-06-20 18:56:17 -0700361 static sp<IAfDeviceEffectProxy> create(const AudioDeviceTypeAddr& device,
Andy Hung55a74fd2023-07-13 19:54:47 -0700362 const sp<DeviceEffectManagerCallback>& callback,
Andy Hung6ac17eb2023-06-20 18:56:17 -0700363 effect_descriptor_t *desc, int id, bool notifyFramesProcessed);
364
365 virtual status_t init(
Andy Hung8e6b62a2023-07-13 18:11:33 -0700366 const std::map<audio_patch_handle_t,
367 IAfPatchPanel::Patch>& patches) = 0;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700368 virtual const AudioDeviceTypeAddr& device() const = 0;
369
370 virtual status_t onCreatePatch(
371 audio_patch_handle_t patchHandle,
Andy Hung8e6b62a2023-07-13 18:11:33 -0700372 const IAfPatchPanel::Patch& patch) = 0;
François Gaffie58e73af2023-02-15 11:47:24 +0100373 virtual status_t onUpdatePatch(audio_patch_handle_t oldPatchHandle,
374 audio_patch_handle_t newPatchHandle,
Andy Hung8e6b62a2023-07-13 18:11:33 -0700375 const IAfPatchPanel::Patch& patch) = 0;
Andy Hung6ac17eb2023-06-20 18:56:17 -0700376 virtual void onReleasePatch(audio_patch_handle_t patchHandle) = 0;
377
Andy Hung99b1ba62023-07-14 11:00:08 -0700378 virtual void dump2(int fd, int spaces) const = 0; // TODO(b/291319101) naming?
Andy Hung6ac17eb2023-06-20 18:56:17 -0700379
380private:
381 // used by DeviceEffectProxy
382 virtual bool isOutput() const = 0;
383 virtual uint32_t sampleRate() const = 0;
384 virtual audio_channel_mask_t channelMask() const = 0;
385 virtual uint32_t channelCount() const = 0;
386
387 virtual size_t removeEffect(const sp<IAfEffectModule>& effect) = 0;
388 virtual status_t addEffectToHal(const sp<EffectHalInterface>& effect) = 0;
389 virtual status_t removeEffectFromHal(const sp<EffectHalInterface>& effect) = 0;
390};
391
Andy Hungee58e4a2023-07-07 13:47:37 -0700392} // namespace android