blob: 6a5c88937da758de27e169672f800c6fe81e201c [file] [log] [blame]
Eric Laurentb82e6b72019-11-22 17:25:04 -08001/*
2**
3** Copyright 2019, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
Andy Hungbf766942023-07-13 19:54:47 -070018#pragma once
19
Andy Hung21ff9672023-07-18 20:54:44 -070020#include "IAfEffect.h"
21#include "PatchCommandThread.h"
22
Andy Hungbf766942023-07-13 19:54:47 -070023namespace android {
24
Andy Hunge9a1c7a2023-07-17 13:45:55 -070025class IAfDeviceEffectManagerCallback : public virtual RefBase {
26public:
27 virtual bool isAudioPolicyReady() const = 0;
28 virtual audio_unique_id_t nextUniqueId(audio_unique_id_use_t use) = 0;
29 virtual const sp<PatchCommandThread>& getPatchCommandThread() = 0;
30 virtual status_t addEffectToHal(
Andy Hung51c7a1e2023-08-30 18:19:00 -070031 const struct audio_port_config* device, const sp<EffectHalInterface>& effect)
32 EXCLUDES_AudioFlinger_HardwareMutex = 0;
Andy Hunge9a1c7a2023-07-17 13:45:55 -070033 virtual status_t removeEffectFromHal(
Andy Hung51c7a1e2023-08-30 18:19:00 -070034 const struct audio_port_config* device, const sp<EffectHalInterface>& effect)
35 EXCLUDES_AudioFlinger_HardwareMutex= 0;
Andy Hunge9a1c7a2023-07-17 13:45:55 -070036};
37
Andy Hungbf766942023-07-13 19:54:47 -070038class DeviceEffectManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -080039
40// DeviceEffectManager is concealed within AudioFlinger, their lifetimes are the same.
Vlad Popa5161f8a2022-10-10 16:17:20 +020041class DeviceEffectManager : public PatchCommandThread::PatchCommandListener {
Eric Laurentb82e6b72019-11-22 17:25:04 -080042public:
Andy Hunge9a1c7a2023-07-17 13:45:55 -070043 explicit DeviceEffectManager(
44 const sp<IAfDeviceEffectManagerCallback>& afDeviceEffectManagerCallback);
Eric Laurentb82e6b72019-11-22 17:25:04 -080045
Andy Hungbf766942023-07-13 19:54:47 -070046 void onFirstRef() override;
Eric Laurentb82e6b72019-11-22 17:25:04 -080047
Andy Hungbd72c542023-06-20 18:56:17 -070048 sp<IAfEffectHandle> createEffect_l(effect_descriptor_t *descriptor,
Eric Laurentb82e6b72019-11-22 17:25:04 -080049 const AudioDeviceTypeAddr& device,
Andy Hungd65869f2023-06-27 17:05:02 -070050 const sp<Client>& client,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070051 const sp<media::IEffectClient>& effectClient,
Andy Hung07434ef2023-07-13 18:11:33 -070052 const std::map<audio_patch_handle_t, IAfPatchPanel::Patch>& patches,
Eric Laurentb82e6b72019-11-22 17:25:04 -080053 int *enabled,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070054 status_t *status,
Eric Laurentde8caf42021-08-11 17:19:25 +020055 bool probe,
Andy Hunga2e69002023-09-06 17:37:34 -070056 bool notifyFramesProcessed) REQUIRES(audio_utils::AudioFlinger_Mutex);
Eric Laurentb82e6b72019-11-22 17:25:04 -080057
Andy Hungbd72c542023-06-20 18:56:17 -070058 size_t removeEffect(const sp<IAfDeviceEffectProxy>& effect);
Andy Hunga2e69002023-09-06 17:37:34 -070059 static status_t createEffectHal(const effect_uuid_t *pEffectUuid,
Eric Laurentb82e6b72019-11-22 17:25:04 -080060 int32_t sessionId, int32_t deviceId,
61 sp<EffectHalInterface> *effect);
Mikhail Naganovd2c7f852023-06-14 18:00:13 -070062 status_t addEffectToHal(const struct audio_port_config *device,
Andy Hungbf766942023-07-13 19:54:47 -070063 const sp<EffectHalInterface>& effect);
Mikhail Naganovd2c7f852023-06-14 18:00:13 -070064 status_t removeEffectFromHal(const struct audio_port_config *device,
Andy Hungbf766942023-07-13 19:54:47 -070065 const sp<EffectHalInterface>& effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -080066
Andy Hunge9a1c7a2023-07-17 13:45:55 -070067 const auto& afDeviceEffectManagerCallback() const { return mAfDeviceEffectManagerCallback; }
Eric Laurentb82e6b72019-11-22 17:25:04 -080068
69 void dump(int fd);
70
Vlad Popa5161f8a2022-10-10 16:17:20 +020071 // PatchCommandThread::PatchCommandListener implementation
72
73 void onCreateAudioPatch(audio_patch_handle_t handle,
Andy Hunga2e69002023-09-06 17:37:34 -070074 const IAfPatchPanel::Patch& patch) final
75 EXCLUDES_DeviceEffectManager_Mutex;
76 void onReleaseAudioPatch(audio_patch_handle_t handle) final
77 EXCLUDES_DeviceEffectManager_Mutex;
Vlad Popa5161f8a2022-10-10 16:17:20 +020078
Eric Laurentb82e6b72019-11-22 17:25:04 -080079private:
Andy Hunga2e69002023-09-06 17:37:34 -070080 static status_t checkEffectCompatibility(const effect_descriptor_t *desc);
Eric Laurentb82e6b72019-11-22 17:25:04 -080081
Andy Hunga2e69002023-09-06 17:37:34 -070082 audio_utils::mutex& mutex() const RETURN_CAPABILITY(audio_utils::DeviceEffectManager_Mutex) {
83 return mMutex;
84 }
Andy Hung60a6c3d2023-08-29 12:19:17 -070085 mutable audio_utils::mutex mMutex;
Andy Hunge9a1c7a2023-07-17 13:45:55 -070086 const sp<IAfDeviceEffectManagerCallback> mAfDeviceEffectManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -080087 const sp<DeviceEffectManagerCallback> mMyCallback;
Andy Hunga2e69002023-09-06 17:37:34 -070088 std::map<AudioDeviceTypeAddr, sp<IAfDeviceEffectProxy>> mDeviceEffects GUARDED_BY(mutex());
Eric Laurentb82e6b72019-11-22 17:25:04 -080089};
90
Vlad Popa5161f8a2022-10-10 16:17:20 +020091class DeviceEffectManagerCallback : public EffectCallbackInterface {
Eric Laurentb82e6b72019-11-22 17:25:04 -080092public:
Andy Hung920f6572022-10-06 12:09:49 -070093 explicit DeviceEffectManagerCallback(DeviceEffectManager& manager)
Vlad Popa5161f8a2022-10-10 16:17:20 +020094 : mManager(manager) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -080095
96 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
Andy Hunga2e69002023-09-06 17:37:34 -070097 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) final {
Eric Laurentb82e6b72019-11-22 17:25:04 -080098 return mManager.createEffectHal(pEffectUuid, sessionId, deviceId, effect);
99 }
100 status_t allocateHalBuffer(size_t size __unused,
Andy Hunga2e69002023-09-06 17:37:34 -0700101 sp<EffectBufferHalInterface>* buffer __unused) final { return NO_ERROR; }
102 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect __unused) final {
Andy Hungbd72c542023-06-20 18:56:17 -0700103 return false;
104 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800105
Andy Hunga2e69002023-09-06 17:37:34 -0700106 audio_io_handle_t io() const final { return AUDIO_IO_HANDLE_NONE; }
107 bool isOutput() const final { return false; }
108 bool isOffload() const final { return false; }
109 bool isOffloadOrDirect() const final { return false; }
110 bool isOffloadOrMmap() const final { return false; }
111 bool isSpatializer() const final { return false; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800112
Andy Hunga2e69002023-09-06 17:37:34 -0700113 uint32_t sampleRate() const final { return 0; }
114 audio_channel_mask_t inChannelMask(int id __unused) const final {
Eric Laurentf1f22e72021-07-13 14:04:14 +0200115 return AUDIO_CHANNEL_NONE;
116 }
Andy Hunga2e69002023-09-06 17:37:34 -0700117 uint32_t inChannelCount(int id __unused) const final { return 0; }
118 audio_channel_mask_t outChannelMask() const final { return AUDIO_CHANNEL_NONE; }
119 uint32_t outChannelCount() const final { return 0; }
Eric Laurentf1f22e72021-07-13 14:04:14 +0200120
Andy Hunga2e69002023-09-06 17:37:34 -0700121 audio_channel_mask_t hapticChannelMask() const final { return AUDIO_CHANNEL_NONE; }
122 size_t frameCount() const final { return 0; }
123 uint32_t latency() const final { return 0; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800124
Andy Hunga2e69002023-09-06 17:37:34 -0700125 status_t addEffectToHal(const sp<EffectHalInterface>& /* effect */) final {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800126 return NO_ERROR;
127 }
Andy Hunga2e69002023-09-06 17:37:34 -0700128 status_t removeEffectFromHal(const sp<EffectHalInterface>& /* effect */) final {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800129 return NO_ERROR;
130 }
131
Andy Hunga2e69002023-09-06 17:37:34 -0700132 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) final;
133 void setVolumeForOutput(float left __unused, float right __unused) const final {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800134
135 // check if effects should be suspended or restored when a given effect is enable or disabled
Andy Hungbd72c542023-06-20 18:56:17 -0700136 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect __unused,
Andy Hunga2e69002023-09-06 17:37:34 -0700137 bool enabled __unused, bool threadLocked __unused) final {}
138 void resetVolume() final {}
139 product_strategy_t strategy() const final { return static_cast<product_strategy_t>(0); }
140 int32_t activeTrackCnt() const final { return 0; }
141 void onEffectEnable(const sp<IAfEffectBase>& effect __unused) final {}
142 void onEffectDisable(const sp<IAfEffectBase>& effect __unused) final {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800143
Andy Hunga2e69002023-09-06 17:37:34 -0700144 wp<IAfEffectChain> chain() const final { return nullptr; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800145
Andy Hungbf766942023-07-13 19:54:47 -0700146 bool isAudioPolicyReady() const final;
Eric Laurentd66d7a12021-07-13 13:35:32 +0200147
Andy Hungbf766942023-07-13 19:54:47 -0700148 int newEffectId() const;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800149
Mikhail Naganovd2c7f852023-06-14 18:00:13 -0700150 status_t addEffectToHal(const struct audio_port_config *device,
151 const sp<EffectHalInterface>& effect) {
152 return mManager.addEffectToHal(device, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800153 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -0700154 status_t removeEffectFromHal(const struct audio_port_config *device,
155 const sp<EffectHalInterface>& effect) {
156 return mManager.removeEffectFromHal(device, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800157 }
158private:
159 DeviceEffectManager& mManager;
160};
Andy Hungbf766942023-07-13 19:54:47 -0700161
162} // namespace android