• 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 #include "hpae_plugin_node.h"
16 #include "audio_errors.h"
17 #include "audio_utils.h"
18 
19 namespace OHOS {
20 namespace AudioStandard {
21 namespace HPAE {
HpaePluginNode(HpaeNodeInfo & nodeInfo)22 HpaePluginNode::HpaePluginNode(HpaeNodeInfo& nodeInfo)
23     : HpaeNode(nodeInfo),  pcmBufferInfo_(nodeInfo.channels, nodeInfo.frameLen, nodeInfo.samplingRate),
24     outputStream_(this), enableProcess_(true), silenceData_(pcmBufferInfo_)
25 
26 {
27     silenceData_.Reset();
28     silenceData_.SetBufferValid(false);
29     silenceData_.SetBufferSilence(true);
30     silenceData_.SetSplitStreamType(nodeInfo.GetSplitStreamType());
31 }
32 
DoProcess()33 void HpaePluginNode::DoProcess()
34 {
35     HpaePcmBuffer *tempOut = nullptr;
36     std::vector<HpaePcmBuffer *>& preOutputs = inputStream_.ReadPreOutputData();
37     // if buffer is not valid, write silence data(invalid) to output
38     if (enableProcess_ && !preOutputs.empty()) {
39         tempOut = SignalProcess(preOutputs);
40         outputStream_.WriteDataToOutput(tempOut);
41     } else if (!preOutputs.empty()) {
42         outputStream_.WriteDataToOutput(preOutputs[0]);
43     } else if (!enableProcess_ && preOutputs.empty()) {
44         // use to drain data when disconnecting, now use for mixerNode of processCluster
45         tempOut = SignalProcess(preOutputs);
46         outputStream_.WriteDataToOutput(tempOut);
47     } else {
48         Trace trace("[sceneType:" + std::to_string(GetSceneType()) + "]" + GetNodeName() + "::DoProcess is_silence");
49         outputStream_.WriteDataToOutput(&silenceData_);
50     }
51 }
52 
Reset()53 bool HpaePluginNode::Reset()
54 {
55     const auto preOutputMap = inputStream_.GetPreOutputMap();
56     for (const auto &preOutput : preOutputMap) {
57         OutputPort<HpaePcmBuffer *> *output = preOutput.first;
58         inputStream_.DisConnect(output);
59     }
60     return true;
61 }
62 
ResetAll()63 bool HpaePluginNode::ResetAll()
64 {
65     const auto preOutputMap = inputStream_.GetPreOutputMap();
66     for (const auto &preOutput : preOutputMap) {
67         OutputPort<HpaePcmBuffer *> *output = preOutput.first;
68         std::shared_ptr<HpaeNode> hpaeNode = preOutput.second;
69         if (hpaeNode->ResetAll()) {
70             inputStream_.DisConnect(output);
71         }
72     }
73     return true;
74 }
75 
EnableProcess(bool enable)76 int32_t HpaePluginNode::EnableProcess(bool enable)
77 {
78     enableProcess_ = enable;
79     return SUCCESS;
80 }
81 
IsEnableProcess()82 bool HpaePluginNode::IsEnableProcess()
83 {
84     return enableProcess_;
85 }
86 
GetSharedInstance()87 std::shared_ptr<HpaeNode> HpaePluginNode::GetSharedInstance()
88 {
89     return shared_from_this();
90 }
91 
GetOutputPort()92 OutputPort<HpaePcmBuffer*>* HpaePluginNode::GetOutputPort()
93 {
94     return &outputStream_;
95 }
96 
Connect(const std::shared_ptr<OutputNode<HpaePcmBuffer * >> & preNode)97 void HpaePluginNode::Connect(const std::shared_ptr<OutputNode<HpaePcmBuffer*>>& preNode)
98 {
99     inputStream_.Connect(preNode->GetSharedInstance(), preNode->GetOutputPort());
100 #ifdef ENABLE_HIDUMP_DFX
101     if (auto callback = GetNodeStatusCallback().lock()) {
102         if (isSourceNode_) {
103             callback->OnNotifyDfxNodeInfo(true, preNode->GetSharedInstance()->GetNodeId(), GetNodeInfo());
104         } else {
105             callback->OnNotifyDfxNodeInfo(true, GetNodeId(), preNode->GetSharedInstance()->GetNodeInfo());
106         }
107     }
108 #endif
109 }
110 
DisConnect(const std::shared_ptr<OutputNode<HpaePcmBuffer * >> & preNode)111 void HpaePluginNode::DisConnect(const std::shared_ptr<OutputNode<HpaePcmBuffer*>>& preNode)
112 {
113     inputStream_.DisConnect(preNode->GetOutputPort());
114 #ifdef ENABLE_HIDUMP_DFX
115     if (auto callback = GetNodeStatusCallback().lock()) {
116         if (isSourceNode_) {
117             callback->OnNotifyDfxNodeInfo(false, GetNodeId(), GetNodeInfo());
118         } else {
119             auto preNodeReal = preNode->GetSharedInstance();
120             callback->OnNotifyDfxNodeInfo(false, preNodeReal->GetNodeId(), preNodeReal->GetNodeInfo());
121         }
122     }
123 #endif
124 }
125 
GetPreOutNum()126 size_t HpaePluginNode::GetPreOutNum()
127 {
128     return inputStream_.GetPreOutputNum();
129 }
130 
GetOutputPortNum()131 size_t HpaePluginNode::GetOutputPortNum()
132 {
133     return outputStream_.GetInputNum();
134 }
135 
SetSourceNode(bool isSourceNode)136 void HpaePluginNode::SetSourceNode(bool isSourceNode)
137 {
138     isSourceNode_ = isSourceNode;
139 }
140 }  // namespace HPAE
141 }  // namespace AudioStandard
142 }  // namespace OHOS