• 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_CAPTURER_MANAGER_H
16 #define HPAE_CAPTURER_MANAGER_H
17 #include <unordered_map>
18 #include <memory>
19 #include <atomic>
20 #include <string>
21 #include <mutex>
22 #include <shared_mutex>
23 #include "audio_effect.h"
24 #include "hpae_signal_process_thread.h"
25 #include "hpae_source_input_node.h"
26 #include "hpae_source_input_cluster.h"
27 #include "hpae_source_output_node.h"
28 #include "hpae_source_process_cluster.h"
29 #include "hpae_no_lock_queue.h"
30 #include "i_hpae_capturer_manager.h"
31 
32 namespace OHOS {
33 namespace AudioStandard {
34 namespace HPAE {
35 class HpaeCapturerManager : public IHpaeCapturerManager {
36 public:
37     HpaeCapturerManager(HpaeSourceInfo &sourceInfo);
38     virtual ~HpaeCapturerManager();
39     int32_t CreateStream(const HpaeStreamInfo& streamInfo) override;
40     int32_t DestroyStream(uint32_t sessionId) override;
41 
42     int32_t Start(uint32_t sessionId) override;
43     int32_t Pause(uint32_t sessionId) override;
44     int32_t Flush(uint32_t sessionId) override;
45     int32_t Drain(uint32_t sessionId) override;
46     int32_t Stop(uint32_t sessionId) override;
47     int32_t Release(uint32_t sessionId) override;
48     int32_t SetStreamMute(uint32_t sessionId, bool isMute) override;
49     int32_t MoveStream(uint32_t sessionId, const std::string& sourceName) override;
50     int32_t MoveAllStream(const std::string& sourceName, const std::vector<uint32_t>& sessionIds,
51         MoveSessionType moveType = MOVE_ALL) override;
52     int32_t SetMute(bool isMute) override;
53     void Process() override;
54     void HandleMsg() override;
55     int32_t Init(bool isReload = false) override;
56     int32_t DeInit(bool isMoveDefault = false) override;
57     bool IsInit() override;
58     bool IsRunning(void) override;
59     bool IsMsgProcessing() override;
60     bool DeactivateThread() override;
61 
62     int32_t RegisterReadCallback(uint32_t sessionId, const std::weak_ptr<ICapturerStreamCallback> &callback) override;
63     int32_t GetSourceOutputInfo(uint32_t sessionId, HpaeSourceOutputInfo &sourceOutputInfo) override;
64     HpaeSourceInfo GetSourceInfo() override;
65     std::vector<SourceOutput> GetAllSourceOutputsInfo() override;
66 
67     void OnNodeStatusUpdate(uint32_t sessionId, IOperation operation) override;
68     void OnNotifyQueue() override;
69     void OnRequestLatency(uint32_t sessionId, uint64_t &latency) override;
70 
71     int32_t AddNodeToSource(const HpaeCaptureMoveInfo &moveInfo) override;
72     int32_t AddAllNodesToSource(const std::vector<HpaeCaptureMoveInfo> &moveInfos, bool isConnect) override;
73     std::string GetThreadName() override;
74     void SetCaptureId(uint32_t captureId);
75     int32_t ReloadCaptureManager(const HpaeSourceInfo &sourceInfo) override;
76     int32_t DumpSourceInfo() override;
77     std::string GetDeviceHDFDumpInfo() override;
78 private:
79     void SendRequest(Request &&request, bool isInit = false);
80     int32_t CreateOutputSession(const HpaeStreamInfo &streamInfo);
81     int32_t DeleteOutputSession(uint32_t sessionId);
82     void ConnectProcessClusterWithEc(HpaeProcessorType &sceneType);
83     void ConnectProcessClusterWithMicRef(HpaeProcessorType &sceneType);
84     int32_t ConnectOutputSession(uint32_t sessionId);
85     int32_t DisConnectOutputSession(uint32_t sessionId);
86     void DisConnectSceneClusterFromSourceInputCluster(HpaeProcessorType &sceneType);
87     void SetSessionState(uint32_t sessionId, HpaeSessionState capturerState);
88     int32_t PrepareCapturerEc(HpaeNodeInfo &ecNodeInfo);
89     int32_t PrepareCapturerMicRef(HpaeNodeInfo &micRefNodeInfo);
90     int32_t InitCapturer();
91     void AddSingleNodeToSource(const HpaeCaptureMoveInfo &moveInfo, bool isConnect = true);
92     void MoveAllStreamToNewSource(const std::string &sourceName,
93         const std::vector<uint32_t>& moveIds, MoveSessionType moveType = MOVE_ALL);
94     int32_t CaptureEffectCreate(const HpaeProcessorType &sceneType, const AudioEnhanceScene &enhanceScene);
95     int32_t CaptureEffectRelease(const HpaeProcessorType &sceneType);
96     int32_t InitCapturerManager();
97     void CreateSourceAttr(IAudioSourceAttr &attr);
98     int32_t CapturerSourceStart();
99     int32_t CapturerSourceStop();
100     void CapturerSourceStopForRemote();
101     void CheckIfAnyStreamRunning();
102     void UpdateAppsUidAndSessionId();
103     bool CheckEcCondition(const HpaeProcessorType &sceneType, HpaeNodeInfo &ecNodeInfo,
104         HpaeSourceInputNodeType &ecNodeType);
105     bool CheckMicRefCondition(const HpaeProcessorType &sceneType, HpaeNodeInfo &micRefNodeInfo);
106 private:
107     HpaeNoLockQueue hpaeNoLockQueue_;
108     std::unique_ptr<HpaeSignalProcessThread> hpaeSignalProcessThread_ = nullptr;
109     std::unordered_map<uint32_t, HpaeCapturerSessionInfo> sessionNodeMap_;
110     std::unordered_map<HpaeProcessorType, std::shared_ptr<HpaeSourceProcessCluster>> sceneClusterMap_;
111     std::unordered_map<uint32_t, std::shared_ptr<HpaeSourceOutputNode>> sourceOutputNodeMap_;
112     std::unordered_map<HpaeSourceInputNodeType, std::shared_ptr<HpaeSourceInputCluster>> sourceInputClusterMap_;
113 
114     HpaeSourceInputNodeType mainMicType_;
115     std::atomic<bool> isInit_ = false;
116     std::atomic<bool> isMute_ = false;
117     HpaeSourceInfo sourceInfo_;
118     uint32_t captureId_ = 0;
119     uint32_t renderId_ = 0;
120 
121     std::vector<int32_t> appsUid_;
122     std::vector<int32_t> sessionsId_;
123 };
124 }  // namespace HPAE
125 }  // namespace AudioStandard
126 }  // namespace OHOS
127 #endif