blob: 78b5ba0274633bd26b2aa584cfca409a4f5cd97c [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,
56 bool notifyFramesProcessed);
Eric Laurentb82e6b72019-11-22 17:25:04 -080057
Andy Hung6ac17eb2023-06-20 18:56:17 -070058 size_t removeEffect(const sp<IAfDeviceEffectProxy>& effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -080059 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
60 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 Hung55a74fd2023-07-13 19:54:47 -070074 const IAfPatchPanel::Patch& patch) final;
75 void onReleaseAudioPatch(audio_patch_handle_t handle) final;
François Gaffie58e73af2023-02-15 11:47:24 +010076 void onUpdateAudioPatch(audio_patch_handle_t oldHandle,
77 audio_patch_handle_t newHandle,
Andy Hung55a74fd2023-07-13 19:54:47 -070078 const IAfPatchPanel::Patch& patch) final;
Vlad Popa5161f8a2022-10-10 16:17:20 +020079
Eric Laurentb82e6b72019-11-22 17:25:04 -080080private:
Eric Laurentb82e6b72019-11-22 17:25:04 -080081 status_t checkEffectCompatibility(const effect_descriptor_t *desc);
82
Andy Hungf65f5a72023-08-29 12:19:17 -070083 audio_utils::mutex& mutex() const { return mMutex; }
84 mutable audio_utils::mutex mMutex;
Andy Hung692f0452023-07-17 13:45:55 -070085 const sp<IAfDeviceEffectManagerCallback> mAfDeviceEffectManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -080086 const sp<DeviceEffectManagerCallback> mMyCallback;
François Gaffie0f660582023-06-27 15:15:26 +020087 std::map<AudioDeviceTypeAddr, std::vector<sp<IAfDeviceEffectProxy>>> mDeviceEffects;
Eric Laurentb82e6b72019-11-22 17:25:04 -080088};
89
Vlad Popa5161f8a2022-10-10 16:17:20 +020090class DeviceEffectManagerCallback : public EffectCallbackInterface {
Eric Laurentb82e6b72019-11-22 17:25:04 -080091public:
Andy Hung920f6572022-10-06 12:09:49 -070092 explicit DeviceEffectManagerCallback(DeviceEffectManager& manager)
Vlad Popa5161f8a2022-10-10 16:17:20 +020093 : mManager(manager) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -080094
95 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
96 int32_t sessionId, int32_t deviceId,
97 sp<EffectHalInterface> *effect) override {
98 return mManager.createEffectHal(pEffectUuid, sessionId, deviceId, effect);
99 }
100 status_t allocateHalBuffer(size_t size __unused,
101 sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700102 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect __unused) override {
103 return false;
104 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800105
106 audio_io_handle_t io() const override { return AUDIO_IO_HANDLE_NONE; }
107 bool isOutput() const override { return false; }
108 bool isOffload() const override { return false; }
109 bool isOffloadOrDirect() const override { return false; }
110 bool isOffloadOrMmap() const override { return false; }
Eric Laurentb62d0362021-10-26 17:40:18 +0200111 bool isSpatializer() const override { return false; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800112
113 uint32_t sampleRate() const override { return 0; }
Eric Laurentf1f22e72021-07-13 14:04:14 +0200114 audio_channel_mask_t inChannelMask(int id __unused) const override {
115 return AUDIO_CHANNEL_NONE;
116 }
117 uint32_t inChannelCount(int id __unused) const override { return 0; }
118 audio_channel_mask_t outChannelMask() const override { return AUDIO_CHANNEL_NONE; }
119 uint32_t outChannelCount() const override { return 0; }
120
jiabineb3bda02020-06-30 14:07:03 -0700121 audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800122 size_t frameCount() const override { return 0; }
123 uint32_t latency() const override { return 0; }
124
Andy Hung920f6572022-10-06 12:09:49 -0700125 status_t addEffectToHal(const sp<EffectHalInterface>& /* effect */) override {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800126 return NO_ERROR;
127 }
Andy Hung920f6572022-10-06 12:09:49 -0700128 status_t removeEffectFromHal(const sp<EffectHalInterface>& /* effect */) override {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800129 return NO_ERROR;
130 }
131
Andy Hung6ac17eb2023-06-20 18:56:17 -0700132 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800133 void setVolumeForOutput(float left __unused, float right __unused) const override {}
134
135 // check if effects should be suspended or restored when a given effect is enable or disabled
Andy Hung6ac17eb2023-06-20 18:56:17 -0700136 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect __unused,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800137 bool enabled __unused, bool threadLocked __unused) override {}
138 void resetVolume() override {}
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800139 product_strategy_t strategy() const override { return static_cast<product_strategy_t>(0); }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800140 int32_t activeTrackCnt() const override { return 0; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700141 void onEffectEnable(const sp<IAfEffectBase>& effect __unused) override {}
142 void onEffectDisable(const sp<IAfEffectBase>& effect __unused) override {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800143
Andy Hung6ac17eb2023-06-20 18:56:17 -0700144 wp<IAfEffectChain> chain() const override { return nullptr; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800145
Andy Hung55a74fd2023-07-13 19:54:47 -0700146 bool isAudioPolicyReady() const final;
Eric Laurentd66d7a12021-07-13 13:35:32 +0200147
Andy Hung55a74fd2023-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 Hung55a74fd2023-07-13 19:54:47 -0700161
162} // namespace android