• 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 
16 #ifndef HPAE_RENDER_MANAGER_H
17 #define HPAE_RENDER_MANAGER_H
18 #include <unordered_map>
19 #include <memory>
20 #include <atomic>
21 #include <string>
22 #include <mutex>
23 #include <shared_mutex>
24 #include "hpae_signal_process_thread.h"
25 #include "hpae_sink_input_node.h"
26 #include "hpae_process_cluster.h"
27 #include "i_hpae_output_cluster.h"
28 #include "hpae_msg_channel.h"
29 #include "hpae_no_lock_queue.h"
30 #include "i_hpae_renderer_manager.h"
31 #include "hpae_co_buffer_node.h"
32 
33 namespace OHOS {
34 namespace AudioStandard {
35 namespace HPAE {
36 
37 class HpaeRendererManager : public IHpaeRendererManager {
38 public:
39     HpaeRendererManager(HpaeSinkInfo &sinkInfo);
40     virtual ~HpaeRendererManager();
41     int32_t CreateStream(const HpaeStreamInfo &streamInfo) override;
42     int32_t DestroyStream(uint32_t sessionId) override;
43 
44     int32_t Start(uint32_t sessionId) override;
45     int32_t StartWithSyncId(uint32_t sessionId, int32_t syncId) override;
46     int32_t Pause(uint32_t sessionId) override;
47     int32_t Flush(uint32_t sessionId) override;
48     int32_t Drain(uint32_t sessionId) override;
49     int32_t Stop(uint32_t sessionId) override;
50     int32_t Release(uint32_t sessionId) override;
51     int32_t MoveStream(uint32_t sessionId, const std::string &sinkName) override;
52     int32_t MoveAllStream(const std::string &sinkName, const std::vector<uint32_t>& sessionIds,
53         MoveSessionType moveType = MOVE_ALL) override;
54     int32_t SuspendStreamManager(bool isSuspend) override;
55     int32_t SetMute(bool isMute) override;
56     void Process() override;
57     void HandleMsg() override;
58     int32_t Init(bool isReload = false) override;
59     int32_t DeInit(bool isMoveDefault = false) override;
60     bool IsInit() override;
61     bool IsRunning(void) override;
62     bool IsMsgProcessing() override;
63     bool DeactivateThread() override;
64     int32_t SetClientVolume(uint32_t sessionId, float volume) override;
65     int32_t SetRate(uint32_t sessionId, int32_t rate) override;
66     int32_t SetAudioEffectMode(uint32_t sessionId, int32_t effectMode) override;
67     int32_t GetAudioEffectMode(uint32_t sessionId, int32_t &effectMode) override;
68     int32_t SetPrivacyType(uint32_t sessionId, int32_t privacyType) override;
69     int32_t GetPrivacyType(uint32_t sessionId, int32_t &privacyType) override;
70     int32_t RegisterWriteCallback(uint32_t sessionId, const std::weak_ptr<IStreamCallback> &callback) override;
71 
72     size_t GetWritableSize(uint32_t sessionId) override;
73     int32_t UpdateSpatializationState(
74         uint32_t sessionId, bool spatializationEnabled, bool headTrackingEnabled) override;
75     int32_t UpdateMaxLength(uint32_t sessionId, uint32_t maxLength) override;
76     void SetSpeed(uint32_t sessionId, float speed) override;
77     std::vector<SinkInput> GetAllSinkInputsInfo() override;
78     int32_t GetSinkInputInfo(uint32_t sessionId, HpaeSinkInputInfo &sinkInputInfo) override;
79     int32_t RefreshProcessClusterByDevice() override;
80     HpaeSinkInfo GetSinkInfo() override;
81 
82     int32_t AddNodeToSink(const std::shared_ptr<HpaeSinkInputNode> &node) override;
83     int32_t AddAllNodesToSink(
84         const std::vector<std::shared_ptr<HpaeSinkInputNode>> &sinkInputs, bool isConnect) override;
85 
86     int32_t RegisterReadCallback(uint32_t sessionId, const std::weak_ptr<ICapturerStreamCallback> &callback) override;
87     void OnNodeStatusUpdate(uint32_t sessionId, IOperation operation) override;
88     void OnFadeDone(uint32_t sessionId, IOperation operation) override;
89     void OnRequestLatency(uint32_t sessionId, uint64_t &latency) override;
90     void OnNotifyQueue() override;
91     std::string GetThreadName() override;
92     int32_t DumpSinkInfo() override;
93     int32_t ReloadRenderManager(const HpaeSinkInfo &sinkInfo, bool isReload = false) override;
94     int32_t SetOffloadPolicy(uint32_t sessionId, int32_t state) override;
95     std::string GetDeviceHDFDumpInfo() override;
96     int32_t SetLoudnessGain(uint32_t sessionId, float loudnessGain) override;
97     void OnDisConnectProcessCluster(HpaeProcessorType sceneType) override;
98     int32_t UpdateCollaborativeState(bool isCollaborationEnabled) override;
99     int32_t ConnectCoBufferNode(const std::shared_ptr<HpaeCoBufferNode> &coBufferNode) override;
100     int32_t DisConnectCoBufferNode(const std::shared_ptr<HpaeCoBufferNode> &coBufferNode) override;
101 
102 private:
103     void SendRequest(Request &&request, bool isInit = false);
104     int32_t StartRenderSink();
105     bool IsMchDevice();
106     int32_t CreateInputSession(const HpaeStreamInfo &streamInfo);
107     int32_t DeleteInputSession(uint32_t sessionId);
108     int32_t DeleteInputSessionForMove(uint32_t sessionId);
109     bool isSplitProcessorType(HpaeProcessorType sceneType);
110     int32_t ConnectInputSession(uint32_t sessionId);
111     int32_t DisConnectInputSession(uint32_t sessionId);
112     int32_t DeleteConnectInputProcessor(const std::shared_ptr<HpaeSinkInputNode> &sinkInputNode);
113     void SetSessionState(uint32_t sessionId, HpaeSessionState renderState);
114     void AddSingleNodeToSink(const std::shared_ptr<HpaeSinkInputNode> &node, bool isConnect = true);
115     void CreateEffectAndConnect(HpaeNodeInfo &nodeInfo, bool isConnect = true);
116     void MoveAllStreamToNewSink(const std::string &sinkName, const std::vector<uint32_t>& moveIds,
117         MoveSessionType moveType);
118     void UpdateProcessClusterConnection(uint32_t sessionId, int32_t effectMode);
119     void ConnectProcessCluster(uint32_t sessionId, HpaeProcessorType sceneType);
120     void DisConnectInputCluster(uint32_t sessionId, HpaeProcessorType sceneType);
121     void DisConnectProcessCluster(const HpaeNodeInfo &nodeInfo, HpaeProcessorType sceneType, uint32_t sessionId);
122     void CreateProcessCluster(HpaeNodeInfo &nodeInfo);
123     void CreateProcessClusterInner(HpaeNodeInfo &nodeInfo, int32_t processClusterDecision);
124     bool SetSessionFade(uint32_t sessionId, IOperation operation);
125     void CreateDefaultProcessCluster(HpaeNodeInfo &nodeInfo);
126     void CreateOutputClusterNodeInfo(HpaeNodeInfo &nodeInfo);
127     void InitManager(bool isReload = false);
128     void MoveStreamSync(uint32_t sessionId, const std::string &sinkName);
129     void UpdateAppsUid();
130     int32_t HandlePriPaPower(uint32_t sessionId);
131     bool CheckIsStreamRunning();
132     HpaeProcessorType GetProcessorType(uint32_t sessionId);
133     HpaeProcessorType TransToProperSceneType(StreamUsage streamUsage, AudioEffectScene effectScene);
134     void ReConnectNodeForCollaboration(uint32_t sessionID);
135     void EnableCollaboration();
136     void DisableCollaboration();
137     int32_t HandleSyncId(uint32_t sessionId, int32_t syncId);
138     int32_t DeleteProcessCluster(HpaeProcessorType sceneType);
139     void RefreshProcessClusterByDeviceInner(const std::shared_ptr<HpaeSinkInputNode> &node);
140 
141 private:
142     std::unordered_map<uint32_t, HpaeRenderSessionInfo> sessionNodeMap_;
143     std::unordered_map<HpaeProcessorType, std::shared_ptr<HpaeProcessCluster>> sceneClusterMap_;
144     std::unordered_map<uint32_t, std::shared_ptr<HpaeSinkInputNode>> sinkInputNodeMap_;
145     std::unordered_map<HpaeProcessorType, uint32_t> toBeStoppedSceneTypeToSessionMap_;
146     std::unique_ptr<IHpaeOutputCluster> outputCluster_ = nullptr;
147     HpaeNoLockQueue hpaeNoLockQueue_;
148     std::unique_ptr<HpaeSignalProcessThread> hpaeSignalProcessThread_ = nullptr;
149     std::atomic<bool> isInit_ = false;
150     std::atomic<bool> isMute_ = false;
151     std::atomic<bool> isSuspend_ = false;
152     HpaeSinkInfo sinkInfo_;
153     std::unordered_map<HpaeProcessorType, int32_t> sceneTypeToProcessClusterCountMap_;
154     std::vector<int32_t> appsUid_;
155     std::shared_ptr<HpaeCoBufferNode> hpaeCoBufferNode_;
156     bool isCollaborationEnabled_ = false;
157 };
158 }  // namespace HPAE
159 }  // namespace AudioStandard
160 }  // namespace OHOS
161 #endif