blob: 5bad39bdc42c2b5659fe00094083d75f23e2dd9a [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(
31 const struct audio_port_config* device, const sp<EffectHalInterface>& effect) = 0;
32 virtual status_t removeEffectFromHal(
33 const struct audio_port_config* device, const sp<EffectHalInterface>& effect) = 0;
34};
35
Andy Hungbf766942023-07-13 19:54:47 -070036class DeviceEffectManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -080037
38// DeviceEffectManager is concealed within AudioFlinger, their lifetimes are the same.
Vlad Popa5161f8a2022-10-10 16:17:20 +020039class DeviceEffectManager : public PatchCommandThread::PatchCommandListener {
Eric Laurentb82e6b72019-11-22 17:25:04 -080040public:
Andy Hunge9a1c7a2023-07-17 13:45:55 -070041 explicit DeviceEffectManager(
42 const sp<IAfDeviceEffectManagerCallback>& afDeviceEffectManagerCallback);
Eric Laurentb82e6b72019-11-22 17:25:04 -080043
Andy Hungbf766942023-07-13 19:54:47 -070044 void onFirstRef() override;
Eric Laurentb82e6b72019-11-22 17:25:04 -080045
Andy Hungbd72c542023-06-20 18:56:17 -070046 sp<IAfEffectHandle> createEffect_l(effect_descriptor_t *descriptor,
Eric Laurentb82e6b72019-11-22 17:25:04 -080047 const AudioDeviceTypeAddr& device,
Andy Hungd65869f2023-06-27 17:05:02 -070048 const sp<Client>& client,
Ytai Ben-Tsvi9cd89812020-07-01 17:12:06 -070049 const sp<media::IEffectClient>& effectClient,
Andy Hung07434ef2023-07-13 18:11:33 -070050 const std::map<audio_patch_handle_t, IAfPatchPanel::Patch>& patches,
Eric Laurentb82e6b72019-11-22 17:25:04 -080051 int *enabled,
Eric Laurent2fe0acd2020-03-13 14:30:46 -070052 status_t *status,
Eric Laurentde8caf42021-08-11 17:19:25 +020053 bool probe,
54 bool notifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -080055
Andy Hungbd72c542023-06-20 18:56:17 -070056 size_t removeEffect(const sp<IAfDeviceEffectProxy>& effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -080057 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
58 int32_t sessionId, int32_t deviceId,
59 sp<EffectHalInterface> *effect);
Mikhail Naganovd2c7f852023-06-14 18:00:13 -070060 status_t addEffectToHal(const struct audio_port_config *device,
Andy Hungbf766942023-07-13 19:54:47 -070061 const sp<EffectHalInterface>& effect);
Mikhail Naganovd2c7f852023-06-14 18:00:13 -070062 status_t removeEffectFromHal(const struct audio_port_config *device,
Andy Hungbf766942023-07-13 19:54:47 -070063 const sp<EffectHalInterface>& effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -080064
Andy Hunge9a1c7a2023-07-17 13:45:55 -070065 const auto& afDeviceEffectManagerCallback() const { return mAfDeviceEffectManagerCallback; }
Eric Laurentb82e6b72019-11-22 17:25:04 -080066
67 void dump(int fd);
68
Vlad Popa5161f8a2022-10-10 16:17:20 +020069 // PatchCommandThread::PatchCommandListener implementation
70
71 void onCreateAudioPatch(audio_patch_handle_t handle,
Andy Hungbf766942023-07-13 19:54:47 -070072 const IAfPatchPanel::Patch& patch) final;
73 void onReleaseAudioPatch(audio_patch_handle_t handle) final;
Vlad Popa5161f8a2022-10-10 16:17:20 +020074
Eric Laurentb82e6b72019-11-22 17:25:04 -080075private:
Eric Laurentb82e6b72019-11-22 17:25:04 -080076 status_t checkEffectCompatibility(const effect_descriptor_t *desc);
77
Andy Hung60a6c3d2023-08-29 12:19:17 -070078 audio_utils::mutex& mutex() const { return mMutex; }
79 mutable audio_utils::mutex mMutex;
Andy Hunge9a1c7a2023-07-17 13:45:55 -070080 const sp<IAfDeviceEffectManagerCallback> mAfDeviceEffectManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -080081 const sp<DeviceEffectManagerCallback> mMyCallback;
Andy Hungbd72c542023-06-20 18:56:17 -070082 std::map<AudioDeviceTypeAddr, sp<IAfDeviceEffectProxy>> mDeviceEffects;
Eric Laurentb82e6b72019-11-22 17:25:04 -080083};
84
Vlad Popa5161f8a2022-10-10 16:17:20 +020085class DeviceEffectManagerCallback : public EffectCallbackInterface {
Eric Laurentb82e6b72019-11-22 17:25:04 -080086public:
Andy Hung920f6572022-10-06 12:09:49 -070087 explicit DeviceEffectManagerCallback(DeviceEffectManager& manager)
Vlad Popa5161f8a2022-10-10 16:17:20 +020088 : mManager(manager) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -080089
90 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
91 int32_t sessionId, int32_t deviceId,
92 sp<EffectHalInterface> *effect) override {
93 return mManager.createEffectHal(pEffectUuid, sessionId, deviceId, effect);
94 }
95 status_t allocateHalBuffer(size_t size __unused,
96 sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; }
Andy Hungbd72c542023-06-20 18:56:17 -070097 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect __unused) override {
98 return false;
99 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800100
101 audio_io_handle_t io() const override { return AUDIO_IO_HANDLE_NONE; }
102 bool isOutput() const override { return false; }
103 bool isOffload() const override { return false; }
104 bool isOffloadOrDirect() const override { return false; }
105 bool isOffloadOrMmap() const override { return false; }
Eric Laurentb62d0362021-10-26 17:40:18 +0200106 bool isSpatializer() const override { return false; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800107
108 uint32_t sampleRate() const override { return 0; }
Eric Laurentf1f22e72021-07-13 14:04:14 +0200109 audio_channel_mask_t inChannelMask(int id __unused) const override {
110 return AUDIO_CHANNEL_NONE;
111 }
112 uint32_t inChannelCount(int id __unused) const override { return 0; }
113 audio_channel_mask_t outChannelMask() const override { return AUDIO_CHANNEL_NONE; }
114 uint32_t outChannelCount() const override { return 0; }
115
jiabineb3bda02020-06-30 14:07:03 -0700116 audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800117 size_t frameCount() const override { return 0; }
118 uint32_t latency() const override { return 0; }
119
Andy Hung920f6572022-10-06 12:09:49 -0700120 status_t addEffectToHal(const sp<EffectHalInterface>& /* effect */) override {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800121 return NO_ERROR;
122 }
Andy Hung920f6572022-10-06 12:09:49 -0700123 status_t removeEffectFromHal(const sp<EffectHalInterface>& /* effect */) override {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800124 return NO_ERROR;
125 }
126
Andy Hungbd72c542023-06-20 18:56:17 -0700127 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800128 void setVolumeForOutput(float left __unused, float right __unused) const override {}
129
130 // check if effects should be suspended or restored when a given effect is enable or disabled
Andy Hungbd72c542023-06-20 18:56:17 -0700131 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect __unused,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800132 bool enabled __unused, bool threadLocked __unused) override {}
133 void resetVolume() override {}
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800134 product_strategy_t strategy() const override { return static_cast<product_strategy_t>(0); }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800135 int32_t activeTrackCnt() const override { return 0; }
Andy Hungbd72c542023-06-20 18:56:17 -0700136 void onEffectEnable(const sp<IAfEffectBase>& effect __unused) override {}
137 void onEffectDisable(const sp<IAfEffectBase>& effect __unused) override {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800138
Andy Hungbd72c542023-06-20 18:56:17 -0700139 wp<IAfEffectChain> chain() const override { return nullptr; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800140
Andy Hungbf766942023-07-13 19:54:47 -0700141 bool isAudioPolicyReady() const final;
Eric Laurentd66d7a12021-07-13 13:35:32 +0200142
Andy Hungbf766942023-07-13 19:54:47 -0700143 int newEffectId() const;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800144
Mikhail Naganovd2c7f852023-06-14 18:00:13 -0700145 status_t addEffectToHal(const struct audio_port_config *device,
146 const sp<EffectHalInterface>& effect) {
147 return mManager.addEffectToHal(device, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800148 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -0700149 status_t removeEffectFromHal(const struct audio_port_config *device,
150 const sp<EffectHalInterface>& effect) {
151 return mManager.removeEffectFromHal(device, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800152 }
153private:
154 DeviceEffectManager& mManager;
155};
Andy Hungbf766942023-07-13 19:54:47 -0700156
157} // namespace android