• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef HPAE_MANAGER_H
16 #define HPAE_MANAGER_H
17 #include <functional>
18 #include <any>
19 #include "audio_module_info.h"
20 #include "hpae_capturer_manager.h"
21 #include "hpae_renderer_manager.h"
22 #include "hpae_inner_capturer_manager.h"
23 #include "hpae_msg_channel.h"
24 #include "i_hpae_manager.h"
25 #include "i_hpae_renderer_manager.h"
26 #include "hpae_policy_manager.h"
27 
28 namespace OHOS {
29 namespace AudioStandard {
30 namespace HPAE {
31 
32 class HpaeManager;
33 
34 class HpaeManagerThread {
35 public:
HpaeManagerThread()36     HpaeManagerThread() : running_(false)
37     {}
38     ~HpaeManagerThread();
39     void ActivateThread(HpaeManager *hpaeManager);
40     void DeactivateThread();
41     void Run();
42     void Notify();
IsRunning()43     bool IsRunning() const
44     {
45         return running_.load();
46     }
IsMsgProcessing()47     bool IsMsgProcessing() const
48     {
49         return recvSignal_.load();
50     }
51 
52 private:
53     std::atomic<bool> running_;
54     std::atomic<bool> recvSignal_;
55     HpaeManager *m_hpaeManager = nullptr;
56     std::condition_variable condition_;
57     std::mutex mutex_;
58     std::thread thread_;
59 };
60 
61 class HpaeManager : public IHpaeManager, public ISendMsgCallback, public std::enable_shared_from_this<HpaeManager> {
62 public:
63     static constexpr std::string_view SPLIT_STREAM_SINK = "libmodule-split-stream-sink.z.so";
64     static constexpr std::string_view HDI_SINK = "libmodule-hdi-sink.z.so";
65     static constexpr std::string_view HDI_SOURCE = "libmodule-hdi-source.z.so";
66     static constexpr std::string_view INNER_CAPTURER_SINK = "libmodule-inner-capturer-sink.z.so";
67     HpaeManager();
68     ~HpaeManager();
69     // sync interface
70     int32_t Init() override;
71     int32_t DeInit() override;
72     int32_t RegisterSerivceCallback(const std::weak_ptr<AudioServiceHpaeCallback> &callback) override;
73     int32_t RegisterHpaeDumpCallback(const std::weak_ptr<AudioServiceHpaeDumpCallback> &callback) override;
74     void DumpSinkInfo(std::string deviceName) override;
75     void DumpSourceInfo(std::string deviceName) override;
76     void DumpAllAvailableDevice(HpaeDeviceInfo &devicesInfo) override;
77     void DumpSinkInputsInfo() override;
78     void DumpSourceOutputsInfo() override;
79     uint32_t OpenAudioPort(const AudioModuleInfo &audioModuleInfo) override;
80     uint32_t ReloadAudioPort(const AudioModuleInfo &audioModuleInfo) override;
81     int32_t CloseAudioPort(int32_t audioHandleIndex) override;
82     int32_t GetSinkInfoByIdx(const int32_t &sinkIdx,
83         std::function<void(const HpaeSinkInfo &sinkInfo, int32_t result)> callback) override;
84     int32_t GetSourceInfoByIdx(const int32_t &sourceIdx,
85         std::function<void(const HpaeSourceInfo &sourceInfo, int32_t result)> callback) override;
86 
87     int32_t GetAllSinkInputs() override;
88     int32_t GetAllSourceOutputs() override;
89     int32_t MoveSourceOutputByIndexOrName(
90         uint32_t sourceOutputId, uint32_t sourceIndex, std::string sourceName) override;
91     int32_t MoveSinkInputByIndexOrName(uint32_t sinkInputId, uint32_t sinkIndex, std::string sinkName) override;
92     void HandleMsg() override;
93     bool IsInit() override;
94     bool IsRunning() override;
95     bool IsMsgProcessing() override;
96     // async interface
97     int32_t SetDefaultSink(std::string name) override;
98     int32_t SetDefaultSource(std::string name) override;
99     int32_t SuspendAudioDevice(std::string &audioPortName, bool isSuspend) override;
100     bool SetSinkMute(const std::string &sinkName, bool isMute, bool isSync = false) override;
101     int32_t SetSourceOutputMute(int32_t uid, bool setMute) override;
102     int32_t GetAllSinks() override;
103 
104     int32_t GetMsgCount();
105 
106     void Invoke(HpaeMsgCode cmdID, const std::any &args) override;
107     // play and record stream interface
108     int32_t CreateStream(const HpaeStreamInfo &streamInfo) override;
109     int32_t DestroyStream(HpaeStreamClassType streamClassType, uint32_t sessionId) override;
110     int32_t Start(HpaeStreamClassType streamClassType, uint32_t sessionId) override;
111     int32_t StartWithSyncId(HpaeStreamClassType streamClassType, uint32_t sessionId, int32_t syncId) override;
112     int32_t Pause(HpaeStreamClassType streamClassType, uint32_t sessionId) override;
113     int32_t Flush(HpaeStreamClassType streamClassType, uint32_t sessionId) override;
114     int32_t Drain(HpaeStreamClassType streamClassType, uint32_t sessionId) override;
115     int32_t Stop(HpaeStreamClassType streamClassType, uint32_t sessionId) override;
116     int32_t Release(HpaeStreamClassType streamClassType, uint32_t sessionId) override;
117     int32_t RegisterStatusCallback(HpaeStreamClassType streamClassType, uint32_t sessionId,
118         const std::weak_ptr<IStreamStatusCallback> &callback) override;
119     // record stream interface
120     int32_t RegisterReadCallback(uint32_t sessionId, const std::weak_ptr<ICapturerStreamCallback> &callback) override;
121     int32_t GetSourceOutputInfo(uint32_t sessionId, HpaeStreamInfo &streamInfo) override;
122     // play stream interface
123     int32_t SetClientVolume(uint32_t sessionId, float volume) override;
124     int32_t SetLoudnessGain(uint32_t sessionId, float loudnessGain) override;
125     int32_t SetRate(uint32_t sessionId, int32_t rate) override;
126     int32_t SetAudioEffectMode(uint32_t sessionId, int32_t effectMode) override;
127     int32_t GetAudioEffectMode(uint32_t sessionId, int32_t &effectMode) override;
128     int32_t SetPrivacyType(uint32_t sessionId, int32_t privacyType) override;
129     int32_t GetPrivacyType(uint32_t sessionId, int32_t &privacyType) override;
130     int32_t RegisterWriteCallback(uint32_t sessionId, const std::weak_ptr<IStreamCallback> &callback) override;
131     int32_t SetOffloadPolicy(uint32_t sessionId, int32_t state) override;
132     size_t GetWritableSize(uint32_t sessionId) override;
133     int32_t UpdateSpatializationState(
134         uint32_t sessionId, bool spatializationEnabled, bool headTrackingEnabled) override;
135     int32_t UpdateMaxLength(uint32_t sessionId, uint32_t maxLength) override;
136     int32_t SetOffloadRenderCallbackType(uint32_t sessionId, int32_t type) override;
137     void SetSpeed(uint32_t sessionId, float speed) override;
138     // only interface for unit test
139     int32_t GetSessionInfo(HpaeStreamClassType streamClassType, uint32_t sessionId, HpaeSessionInfo &sessionInfo);
140 
141     // interfaces for render effect
142     void InitAudioEffectChainManager(const std::vector<EffectChain> &effectChains,
143         const EffectChainManagerParam &effectChainManagerParam,
144         const std::vector<std::shared_ptr<AudioEffectLibEntry>> &effectLibraryList) override;
145     void SetOutputDeviceSink(int32_t device, const std::string &sinkName) override;
146     int32_t UpdateSpatializationState(AudioSpatializationState spatializationState) override;
147     int32_t UpdateSpatialDeviceType(AudioSpatialDeviceType spatialDeviceType) override;
148     int32_t SetSpatializationSceneType(AudioSpatializationSceneType spatializationSceneType) override;
149     int32_t EffectRotationUpdate(const uint32_t rotationState) override;
150     int32_t SetEffectSystemVolume(const int32_t systemVolumeType, const float systemVolume) override;
151     int32_t SetAbsVolumeStateToEffect(const bool absVolumeState) override;
152     int32_t SetAudioEffectProperty(const AudioEffectPropertyArrayV3 &propertyArray) override;
153     int32_t GetAudioEffectProperty(AudioEffectPropertyArrayV3 &propertyArray) override;
154     int32_t SetAudioEffectProperty(const AudioEffectPropertyArray &propertyArray) override;
155     int32_t GetAudioEffectProperty(AudioEffectPropertyArray &propertyArray) override;
156     void InitHdiState() override;
157     void UpdateEffectBtOffloadSupported(const bool &isSupported) override;
158     void UpdateParamExtra(const std::string &mainkey, const std::string &subkey, const std::string &value) override;
159     // interfaces for capture effect
160     void InitAudioEnhanceChainManager(const std::vector<EffectChain> &enhanceChains,
161         const EffectChainManagerParam &managerParam,
162         const std::vector<std::shared_ptr<AudioEffectLibEntry>> &enhanceLibraryList) override;
163     int32_t SetOutputDevice(const uint32_t &renderId, const DeviceType &outputDevice) override;
164     int32_t SetVolumeInfo(const AudioVolumeType &volumeType, const float &systemVol) override;
165     int32_t SetMicrophoneMuteInfo(const bool &isMute) override;
166     int32_t SetStreamVolumeInfo(const uint32_t &sessionId, const float &streamVol) override;
167     int32_t SetAudioEnhanceProperty(
168         const AudioEffectPropertyArrayV3 &propertyArray, DeviceType deviceType = DEVICE_TYPE_NONE) override;
169     int32_t GetAudioEnhanceProperty(
170         AudioEffectPropertyArrayV3 &propertyArray, DeviceType deviceType = DEVICE_TYPE_NONE) override;
171     int32_t SetAudioEnhanceProperty(
172         const AudioEnhancePropertyArray &propertyArray, DeviceType deviceType = DEVICE_TYPE_NONE) override;
173     int32_t GetAudioEnhanceProperty(
174         AudioEnhancePropertyArray &propertyArray, DeviceType deviceType = DEVICE_TYPE_NONE) override;
175     void UpdateExtraSceneType(
176         const std::string &mainkey, const std::string &subkey, const std::string &extraSceneType) override;
177     void NotifySettingsDataReady() override;
178     void NotifyAccountsChanged() override;
179     bool IsAcousticEchoCancelerSupported(SourceType sourceType) override;
180     bool SetEffectLiveParameter(const std::vector<std::pair<std::string, std::string>> &params) override;
181     bool GetEffectLiveParameter(const std::vector<std::string> &subKeys,
182         std::vector<std::pair<std::string, std::string>> &result) override;
183     int32_t UpdateCollaborativeState(bool isCollaborationEnabled) override;
184     void AddStreamVolumeToEffect(const std::string stringSessionID, const float streamVolume) override;
185     void DeleteStreamVolumeToEffect(const std::string stringSessionID) override;
186 private:
187     int32_t CloseOutAudioPort(std::string sinkName);
188     int32_t CloseInAudioPort(std::string sourceName);
189     template <typename... Args>
190     void RegisterHandler(HpaeMsgCode cmdID, void (HpaeManager::*func)(Args...));
191     void HandleUpdateStatus(
192         HpaeStreamClassType streamClassType, uint32_t sessionId, HpaeSessionState status, IOperation operation);
193     void HandleReloadDeviceResult(std::string deviceName, int32_t result);
194     void HandleInitDeviceResult(std::string deviceName, int32_t result);
195     void HandleMoveSinkInput(const std::shared_ptr<HpaeSinkInputNode> sinkInputNode, std::string sinkName);
196     void HandleMoveAllSinkInputs(std::vector<std::shared_ptr<HpaeSinkInputNode>> sinkInputs, std::string sinkName,
197         MoveSessionType moveType);
198     void HandleMoveSourceOutput(HpaeCaptureMoveInfo moveInfo, std::string sourceName);
199     void HandleMoveAllSourceOutputs(const std::vector<HpaeCaptureMoveInfo> moveInfos, std::string sourceName);
200     void HandleMoveSessionFailed(HpaeStreamClassType streamClassType, uint32_t sessionId, MoveSessionType moveType,
201         std::string name);
202     void HandleDumpSinkInfo(std::string deviceName, std::string dumpStr);
203     void HandleDumpSourceInfo(std::string deviceName, std::string dumpStr);
204     void HandleConnectCoBufferNode(std::shared_ptr<HpaeCoBufferNode> hpaeCobufferNode);
205     void HandleDisConnectCoBufferNode(std::shared_ptr<HpaeCoBufferNode> hpaeCobufferNode);
206     void HandleInitSourceResult(SourceType sourceType);
207 
208     void SendRequest(Request &&request, std::string funcName);
209     int32_t OpenAudioPortInner(const AudioModuleInfo &audioModuleInfo);
210     int32_t OpenOutputAudioPort(const AudioModuleInfo &audioModuleInfo, uint32_t sinkSourceIndex);
211     int32_t OpenInputAudioPort(const AudioModuleInfo &audioModuleInfo, uint32_t sinkSourceIndex);
212     int32_t OpenVirtualAudioPort(const AudioModuleInfo &audioModuleInfo, uint32_t sinkSourceIndex);
213     void HandleRendererManager(const std::string& sinkName, const HpaeStreamInfo &streamInfo);
214     void CreateStreamForCapInner(const HpaeStreamInfo &streamInfo);
215     int32_t CreateRendererManager(const AudioModuleInfo &audioModuleInfo, uint32_t sinkSourceIndex,
216         bool isReload = false);
217     void UpdateStatus(const std::weak_ptr<IStreamStatusCallback> &callback, IOperation operation, uint32_t sessionId);
218 
219     std::shared_ptr<IHpaeRendererManager> GetRendererManagerById(uint32_t sessionId);
220     std::shared_ptr<IHpaeCapturerManager> GetCapturerManagerById(uint32_t sessionId);
221     std::shared_ptr<IHpaeRendererManager> GetRendererManagerByName(const std::string &sinkName);
222     std::shared_ptr<IHpaeCapturerManager> GetCapturerManagerByName(const std::string &sourceName);
223     void AddStreamToCollection(const HpaeStreamInfo &streamInfo, const std::string &name);
224 
225     void MoveToPreferSink(const std::string& name, std::shared_ptr<AudioServiceHpaeCallback> &serviceCallback);
226     int32_t ReloadRenderManager(const AudioModuleInfo &audioModuleInfo, bool isReload = false);
227     void DestroyCapture(uint32_t sessionId);
228     void LoadEffectLive();
229 
230     bool MovingSinkStateChange(uint32_t sessionId, const std::shared_ptr<HpaeSinkInputNode>& sinkInput);
231     bool SetMovingStreamState(HpaeStreamClassType streamType, uint32_t sessionId,
232         HpaeSessionState status, HpaeSessionState state, IOperation operation);
233     void AddPreferSinkForDefaultChange(bool isAdd, const std::string &sinkName);
234     void OnCallbackOpenOrReloadFailed(bool isReload);
235     bool ShouldNotSkipProcess(const HpaeStreamClassType &streamType, const uint32_t &sessionId);
236     bool CheckMoveSinkInput(uint32_t sinkInputId, const std::string &sinkName);
237     bool CheckMoveSourceOutput(uint32_t sourceOutputId, const std::string &sourceName);
238 
239 private:
240     std::unique_ptr<HpaeManagerThread> hpaeManagerThread_ = nullptr;
241     std::unordered_map<std::string, std::shared_ptr<IHpaeCapturerManager>> capturerManagerMap_;
242     std::unordered_map<std::string, std::shared_ptr<IHpaeRendererManager>> rendererManagerMap_;
243     std::unordered_map<uint32_t, std::string> capturerIdSourceNameMap_;
244     std::unordered_map<uint32_t, std::string> rendererIdSinkNameMap_;
245     std::unordered_map<uint32_t, std::string> idPreferSinkNameMap_;
246     std::unordered_map<uint32_t, HpaeSessionInfo> rendererIdStreamInfoMap_;
247     std::unordered_map<uint32_t, HpaeSessionInfo> capturerIdStreamInfoMap_;
248     std::unordered_map<uint32_t, SinkInput> sinkInputs_;
249     std::unordered_map<uint32_t, SourceOutput> sourceOutputs_;
250     std::unordered_map<std::string, uint32_t> sinkNameSinkIdMap_;  // todo
251     std::unordered_map<uint32_t, std::string> sinkIdSinkNameMap_;
252     std::unordered_map<uint32_t, HpaeSessionState> movingIds_;
253     std::string defaultSink_ = "";
254     std::string coreSink_ = "";
255     std::unordered_map<std::string, uint32_t> sourceNameSourceIdMap_;
256     std::unordered_map<uint32_t, std::string> sourceIdSourceNameMap_;
257     std::string defaultSource_ = "Built_in_mic";
258     std::atomic<int32_t> sinkSourceIndex_ = 0;
259     std::atomic<bool> isInit_ = false;
260 
261     HpaeNoLockQueue hpaeNoLockQueue_;
262 
263     std::atomic<int32_t> receiveMsgCount_ = 0;
264     std::weak_ptr<AudioServiceHpaeCallback> serviceCallback_;
265     std::weak_ptr<AudioServiceHpaeDumpCallback> dumpCallback_;
266     std::unordered_map<std::string, std::string> deviceDumpSinkInfoMap_;
267     std::unordered_map<HpaeMsgCode, std::function<void(const std::any &)>> handlers_;
268     std::string effectLiveState_ = "";
269 };
270 
271 }  // namespace HPAE
272 }  // namespace AudioStandard
273 }  // namespace OHOS
274 #endif  // HPAE_HDI_MANAGER_H