blob: a2f321a30b19edc3b47a45c81760d80e05ffe380 [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(
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 Hung55a74fd2023-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 Hung692f0452023-07-17 13:45:55 -070041 explicit DeviceEffectManager(
42 const sp<IAfDeviceEffectManagerCallback>& afDeviceEffectManagerCallback);
Eric Laurentb82e6b72019-11-22 17:25:04 -080043
Andy Hung55a74fd2023-07-13 19:54:47 -070044 void onFirstRef() override;
Eric Laurentb82e6b72019-11-22 17:25:04 -080045
Andy Hung6ac17eb2023-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 Hung59867e42023-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 Hung8e6b62a2023-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 Hung6ac17eb2023-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 Hung55a74fd2023-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 Hung55a74fd2023-07-13 19:54:47 -070063 const sp<EffectHalInterface>& effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -080064
Andy Hung692f0452023-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 Hung55a74fd2023-07-13 19:54:47 -070072 const IAfPatchPanel::Patch& patch) final;
73 void onReleaseAudioPatch(audio_patch_handle_t handle) final;
François Gaffie58e73af2023-02-15 11:47:24 +010074 void onUpdateAudioPatch(audio_patch_handle_t oldHandle,
75 audio_patch_handle_t newHandle,
Andy Hung55a74fd2023-07-13 19:54:47 -070076 const IAfPatchPanel::Patch& patch) final;
Vlad Popa5161f8a2022-10-10 16:17:20 +020077
Eric Laurentb82e6b72019-11-22 17:25:04 -080078private:
Eric Laurentb82e6b72019-11-22 17:25:04 -080079 status_t checkEffectCompatibility(const effect_descriptor_t *desc);
80
Andy Hungf65f5a72023-08-29 12:19:17 -070081 audio_utils::mutex& mutex() const { return mMutex; }
82 mutable audio_utils::mutex mMutex;
Andy Hung692f0452023-07-17 13:45:55 -070083 const sp<IAfDeviceEffectManagerCallback> mAfDeviceEffectManagerCallback;
Eric Laurentb82e6b72019-11-22 17:25:04 -080084 const sp<DeviceEffectManagerCallback> mMyCallback;
François Gaffie0f660582023-06-27 15:15:26 +020085 std::map<AudioDeviceTypeAddr, std::vector<sp<IAfDeviceEffectProxy>>> mDeviceEffects;
Eric Laurentb82e6b72019-11-22 17:25:04 -080086};
87
Vlad Popa5161f8a2022-10-10 16:17:20 +020088class DeviceEffectManagerCallback : public EffectCallbackInterface {
Eric Laurentb82e6b72019-11-22 17:25:04 -080089public:
Andy Hung920f6572022-10-06 12:09:49 -070090 explicit DeviceEffectManagerCallback(DeviceEffectManager& manager)
Vlad Popa5161f8a2022-10-10 16:17:20 +020091 : mManager(manager) {}
Eric Laurentb82e6b72019-11-22 17:25:04 -080092
93 status_t createEffectHal(const effect_uuid_t *pEffectUuid,
94 int32_t sessionId, int32_t deviceId,
95 sp<EffectHalInterface> *effect) override {
96 return mManager.createEffectHal(pEffectUuid, sessionId, deviceId, effect);
97 }
98 status_t allocateHalBuffer(size_t size __unused,
99 sp<EffectBufferHalInterface>* buffer __unused) override { return NO_ERROR; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700100 bool updateOrphanEffectChains(const sp<IAfEffectBase>& effect __unused) override {
101 return false;
102 }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800103
104 audio_io_handle_t io() const override { return AUDIO_IO_HANDLE_NONE; }
105 bool isOutput() const override { return false; }
106 bool isOffload() const override { return false; }
107 bool isOffloadOrDirect() const override { return false; }
108 bool isOffloadOrMmap() const override { return false; }
Eric Laurentb62d0362021-10-26 17:40:18 +0200109 bool isSpatializer() const override { return false; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800110
111 uint32_t sampleRate() const override { return 0; }
Eric Laurentf1f22e72021-07-13 14:04:14 +0200112 audio_channel_mask_t inChannelMask(int id __unused) const override {
113 return AUDIO_CHANNEL_NONE;
114 }
115 uint32_t inChannelCount(int id __unused) const override { return 0; }
116 audio_channel_mask_t outChannelMask() const override { return AUDIO_CHANNEL_NONE; }
117 uint32_t outChannelCount() const override { return 0; }
118
jiabineb3bda02020-06-30 14:07:03 -0700119 audio_channel_mask_t hapticChannelMask() const override { return AUDIO_CHANNEL_NONE; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800120 size_t frameCount() const override { return 0; }
121 uint32_t latency() const override { return 0; }
122
Andy Hung920f6572022-10-06 12:09:49 -0700123 status_t addEffectToHal(const sp<EffectHalInterface>& /* effect */) override {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800124 return NO_ERROR;
125 }
Andy Hung920f6572022-10-06 12:09:49 -0700126 status_t removeEffectFromHal(const sp<EffectHalInterface>& /* effect */) override {
Eric Laurentb82e6b72019-11-22 17:25:04 -0800127 return NO_ERROR;
128 }
129
Andy Hung6ac17eb2023-06-20 18:56:17 -0700130 bool disconnectEffectHandle(IAfEffectHandle *handle, bool unpinIfLast) override;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800131 void setVolumeForOutput(float left __unused, float right __unused) const override {}
132
133 // check if effects should be suspended or restored when a given effect is enable or disabled
Andy Hung6ac17eb2023-06-20 18:56:17 -0700134 void checkSuspendOnEffectEnabled(const sp<IAfEffectBase>& effect __unused,
Eric Laurentb82e6b72019-11-22 17:25:04 -0800135 bool enabled __unused, bool threadLocked __unused) override {}
136 void resetVolume() override {}
Ytai Ben-Tsvi0a4904a2021-01-06 12:57:05 -0800137 product_strategy_t strategy() const override { return static_cast<product_strategy_t>(0); }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800138 int32_t activeTrackCnt() const override { return 0; }
Andy Hung6ac17eb2023-06-20 18:56:17 -0700139 void onEffectEnable(const sp<IAfEffectBase>& effect __unused) override {}
140 void onEffectDisable(const sp<IAfEffectBase>& effect __unused) override {}
Eric Laurentb82e6b72019-11-22 17:25:04 -0800141
Andy Hung6ac17eb2023-06-20 18:56:17 -0700142 wp<IAfEffectChain> chain() const override { return nullptr; }
Eric Laurentb82e6b72019-11-22 17:25:04 -0800143
Andy Hung55a74fd2023-07-13 19:54:47 -0700144 bool isAudioPolicyReady() const final;
Eric Laurentd66d7a12021-07-13 13:35:32 +0200145
Andy Hung55a74fd2023-07-13 19:54:47 -0700146 int newEffectId() const;
Eric Laurentb82e6b72019-11-22 17:25:04 -0800147
Mikhail Naganovd2c7f852023-06-14 18:00:13 -0700148 status_t addEffectToHal(const struct audio_port_config *device,
149 const sp<EffectHalInterface>& effect) {
150 return mManager.addEffectToHal(device, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800151 }
Mikhail Naganovd2c7f852023-06-14 18:00:13 -0700152 status_t removeEffectFromHal(const struct audio_port_config *device,
153 const sp<EffectHalInterface>& effect) {
154 return mManager.removeEffectFromHal(device, effect);
Eric Laurentb82e6b72019-11-22 17:25:04 -0800155 }
156private:
157 DeviceEffectManager& mManager;
158};
Andy Hung55a74fd2023-07-13 19:54:47 -0700159
160} // namespace android