blob: 7045c8b539ae2e8b0192452f96360cd3793dd083 [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 Hung55a74fd2023-07-13 19:54:47 -070018#pragma once
19
Andy Hung0a51b5c2023-07-18 20:54:44 -070020#include "IAfEffect.h"
21#include "PatchCommandThread.h"
22
Andy Hung55a74fd2023-07-13 19:54:47 -070023namespace android {
24
Andy Hung692f0452023-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 Hungc2cf7ec2023-08-30 18:19:00 -070031 const struct audio_port_config* device, const sp<EffectHalInterface>& effect)
32 EXCLUDES_AudioFlinger_HardwareMutex = 0;
Andy Hung692f0452023-07-17 13:45:55 -070033 virtual status_t removeEffectFromHal(
Andy Hungc2cf7ec2023-08-30 18:19:00 -070034 const struct audio_port_config* device, const sp<EffectHalInterface>& effect)
35 EXCLUDES_AudioFlinger_HardwareMutex= 0;
Andy Hung692f0452023-07-17 13:45:55 -070036};
37
Andy Hung55a74fd2023-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 Hung692f0452023-07-17 13:45:55 -070043 explicit DeviceEffectManager(
44 const sp<IAfDeviceEffectManagerCallback>& afDeviceEffectManagerCallback);
Eric Laurentb82e6b72019-11-22 17:25:04 -080045
Andy Hung55a74fd2023-07-13 19:54:47 -070046 void onFirstRef() override;
Eric Laurentb82e6b72019-11-22 17:25:04 -080047
Andy Hung6ac17eb2023-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 Hung59867e42023-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 Hung8e6b62a2023-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 Hung3e07ef02023-09-06 17:37:34 -070056 bool notifyFramesProcessed) REQUIRES(audio_utils::AudioFlinger_Mutex);
Eric Laurentb82e6b72019-11-22 17:25:04 -080057
Andy Hung6ac17eb2023-06-20 18:56:17 -070058 size_t removeEffect(const sp<IAfDeviceEffectProxy>& effect);
Andy Hung3e07ef02023-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 Hung55a74fd2023-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 Hung55a74fd2023-07-13 19:54:47 -070065 const sp<EffectHalInterface>& effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -080066
Andy Hung692f0452023-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 Hung3e07ef02023-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;
François Gaffie58e73af2023-02-15 11:47:24 +010078 void onUpdateAudioPatch(audio_patch_handle_t oldHandle,
Andy Hung3e07ef02023-09-06 17:37:34 -070079 audio_patch_handle_t newHandle, const IAfPatchPanel::Patch& patch) final
80 EXCLUDES_DeviceEffectManager_Mutex;
Vlad Popa5161f8a2022-10-10 16:17:20 +020081
Eric Laurentb82e6b72019-11-22 17:25:04 -080082private:
Andy Hung3e07ef02023-09-06 17:37:34 -070083 static status_t checkEffectCompatibility(const effect_descriptor_t *desc);
Eric Laurentb82e6b72019-11-22 17:25:04 -080084
Andy Hung3e07ef02023-09-06 17:37:34 -070085 audio_utils::mutex& mutex() const RETURN_CAPABILITY(audio_utils::DeviceEffectManager_Mutex) {
86 return mMutex;
87 }
Andy Hunga6f1cbb2023-10-12 18:18:13 -070088 mutable audio_utils::mutex mMutex{audio_utils::MutexOrder::kDeviceEffectManager_Mutex};
Andy Hung692f0452023-07-17 13:45:55 -070089 const sp<IAfDeviceEffectManagerCallback> mAfDeviceEffectManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -080090 const sp<DeviceEffectManagerCallback> mMyCallback;
Andy Hung3e07ef02023-09-06 17:37:34 -070091 std::map<AudioDeviceTypeAddr, std::vector<sp<IAfDeviceEffectProxy>>>
92 mDeviceEffects GUARDED_BY(mutex());
Eric Laurentb82e6b72019-11-22 17:25:04 -080093};
94
Vlad Popa5161f8a2022-10-10 16:17:20 +020095class DeviceEffectManagerCallback : public EffectCallbackInterface {
Eric Laurentb82e6b72019-11-22 17:25:04 -080096public:
Andy Hung920f6572022-10-06 12:09:49 -070097 explicit DeviceEffectManagerCallback(DeviceEffectManager& manager)
Vlad Popa5161f8a2022-10-10 16:17:20 +020098 : mManager(manager) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -080099
100 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
Andy Hung3e07ef02023-09-06 17:37:34 -0700101 int32_t sessionId, int32_t deviceId, sp<EffectHalInterface> *effect) final {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800102 return mManager.createEffectHal(pEffectUuid, sessionId, deviceId, effect);
103 }
104 status_t allocateHalBuffer(size_t size __unused,
Andy Hung3e07ef02023-09-06 17:37:34 -0700105 sp<EffectBufferHalInterface>* buffer __unused) final { return NO_ERROR; }
106 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect __unused) final {
Andy Hung6ac17eb2023-06-20 18:56:17 -0700107 return false;
108 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800109
Andy Hung3e07ef02023-09-06 17:37:34 -0700110 audio_io_handle_t io() const final { return AUDIO_IO_HANDLE_NONE; }
111 bool isOutput() const final { return false; }
112 bool isOffload() const final { return false; }
113 bool isOffloadOrDirect() const final { return false; }
114 bool isOffloadOrMmap() const final { return false; }
115 bool isSpatializer() const final { return false; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800116
Andy Hung3e07ef02023-09-06 17:37:34 -0700117 uint32_t sampleRate() const final { return 0; }
118 audio_channel_mask_t inChannelMask(int id __unused) const final {
Eric Laurentf1f22e72021-07-13 14:04:14 +0200119 return AUDIO_CHANNEL_NONE;
120 }
Andy Hung3e07ef02023-09-06 17:37:34 -0700121 uint32_t inChannelCount(int id __unused) const final { return 0; }
122 audio_channel_mask_t outChannelMask() const final { return AUDIO_CHANNEL_NONE; }
123 uint32_t outChannelCount() const final { return 0; }
Eric Laurentf1f22e72021-07-13 14:04:14 +0200124
Andy Hung3e07ef02023-09-06 17:37:34 -0700125 audio_channel_mask_t hapticChannelMask() const final { return AUDIO_CHANNEL_NONE; }
126 size_t frameCount() const final { return 0; }
127 uint32_t latency() const final { return 0; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800128
Andy Hung3e07ef02023-09-06 17:37:34 -0700129 status_t addEffectToHal(const sp<EffectHalInterface>& /* effect */) final {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800130 return NO_ERROR;
131 }
Andy Hung3e07ef02023-09-06 17:37:34 -0700132 status_t removeEffectFromHal(const sp<EffectHalInterface>& /* effect */) final {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800133 return NO_ERROR;
134 }
135
Andy Hung3e07ef02023-09-06 17:37:34 -0700136 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) final;
137 void setVolumeForOutput(float left __unused, float right __unused) const final {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800138
139 // check if effects should be suspended or restored when a given effect is enable or disabled
Andy Hung6ac17eb2023-06-20 18:56:17 -0700140 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect __unused,
Andy Hung3e07ef02023-09-06 17:37:34 -0700141 bool enabled __unused, bool threadLocked __unused) final {}
142 void resetVolume() final {}
143 product_strategy_t strategy() const final { return static_cast<product_strategy_t>(0); }
144 int32_t activeTrackCnt() const final { return 0; }
145 void onEffectEnable(const sp<IAfEffectBase>& effect __unused) final {}
146 void onEffectDisable(const sp<IAfEffectBase>& effect __unused) final {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800147
Andy Hung3e07ef02023-09-06 17:37:34 -0700148 wp<IAfEffectChain> chain() const final { return nullptr; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800149
Andy Hung55a74fd2023-07-13 19:54:47 -0700150 bool isAudioPolicyReady() const final;
Eric Laurentd66d7a12021-07-13 13:35:32 +0200151
Andy Hung55a74fd2023-07-13 19:54:47 -0700152 int newEffectId() const;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800153
Mikhail Naganovd2c7f852023-06-14 18:00:13 -0700154 status_t addEffectToHal(const struct audio_port_config *device,
155 const sp<EffectHalInterface>& effect) {
156 return mManager.addEffectToHal(device, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800157 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -0700158 status_t removeEffectFromHal(const struct audio_port_config *device,
159 const sp<EffectHalInterface>& effect) {
160 return mManager.removeEffectFromHal(device, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800161 }
162private:
163 DeviceEffectManager& mManager;
164};
Andy Hung55a74fd2023-07-13 19:54:47 -0700165
166} // namespace android