• 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 LOG_TAG
17 #define LOG_TAG "HpaeRenderEffectNode"
18 #endif
19 
20 #include <cinttypes>
21 #include "audio_errors.h"
22 #include "hpae_render_effect_node.h"
23 #include "hpae_pcm_buffer.h"
24 #include "audio_effect_chain_manager.h"
25 #include "audio_effect_map.h"
26 #include "audio_utils.h"
27 #include "audio_effect_log.h"
28 
29 static constexpr uint32_t DEFUALT_EFFECT_RATE = 48000;
30 static constexpr uint32_t DEFAULT_EFFECT_FRAMELEN = 960;
31 static constexpr int32_t COLLABORATIVE_OUTPUT_CHANNELS = 4;
32 static constexpr int32_t DIRECT_CHANNELS = 2;
33 static constexpr int32_t COLLABORATIVE_CHANNELS = 2;
34 static constexpr int32_t COLLABORATIVE_OUTPUT_CHANNEL_1_INDEX = 2;
35 static constexpr int32_t COLLABORATIVE_OUTPUT_CHANNEL_2_INDEX = 3;
36 static constexpr int64_t WAIT_CLOSE_EFFECT_TIME = 4; // 4s
37 static constexpr int64_t MONITOR_CLOSE_EFFECT_TIME = 5 * 60; // 5m
38 static constexpr int64_t TIME_IN_US = 1000000;
39 
40 namespace OHOS {
41 namespace AudioStandard {
42 namespace HPAE {
43 
HpaeRenderEffectNode(HpaeNodeInfo & nodeInfo)44 HpaeRenderEffectNode::HpaeRenderEffectNode(HpaeNodeInfo &nodeInfo) : HpaeNode(nodeInfo), HpaePluginNode(nodeInfo),
45     // DEFAUT effect out format
46     pcmBufferInfo_(nodeInfo.channels, DEFAULT_EFFECT_FRAMELEN, DEFUALT_EFFECT_RATE, nodeInfo.channelLayout),
47     effectOutput_(pcmBufferInfo_)
48 {
49     if (nodeInfo.sceneType == HPAE_SCENE_DEFAULT) {
50         sceneType_ = "SCENE_DEFAULT";
51     } else {
52         const std::unordered_map<AudioEffectScene, std::string> &audioSupportedSceneTypes = GetSupportedSceneType();
53         if (audioSupportedSceneTypes.find(nodeInfo.effectInfo.effectScene) !=
54             audioSupportedSceneTypes.end()) {
55             sceneType_ = audioSupportedSceneTypes.at(nodeInfo.effectInfo.effectScene);
56         }
57         if (sceneType_ == "SCENE_COLLABORATIVE") {
58             PcmBufferInfo pcmBufferInfo(STEREO, DEFAULT_EFFECT_FRAMELEN, DEFUALT_EFFECT_RATE, CH_LAYOUT_STEREO);
59             directOutput_ = std::make_unique<HpaePcmBuffer>(pcmBufferInfo);
60             collaborativeOutput_ = std::make_unique<HpaePcmBuffer>(pcmBufferInfo);
61         }
62     }
63     AUDIO_INFO_LOG("render effect node created, scene type: %{public}s", sceneType_.c_str());
64 #ifdef ENABLE_HOOK_PCM
65     if (sceneType_ == "SCENE_COLLABORATIVE") {
66         directPcmDumper_ = std::make_unique<HpaePcmDumper>(
67             "HpaeRenderEffectNodeDirect_id_" + std::to_string(GetNodeId()) + "_scene_" + sceneType_ + ".pcm");
68         collaborativePcmDumper_ = std::make_unique<HpaePcmDumper>(
69             "HpaeRenderEffectNodeCollaborative_id_" + std::to_string(GetNodeId()) + "_scene_" + sceneType_ + ".pcm");
70     }
71 #endif
72 #ifdef ENABLE_HIDUMP_DFX
73     SetNodeName("hpaeRenderEffectNode");
74 #endif
75 }
76 
~HpaeRenderEffectNode()77 HpaeRenderEffectNode::~HpaeRenderEffectNode()
78 {
79 #ifdef ENABLE_HIDUMP_DFX
80     AUDIO_INFO_LOG("NodeId: %{public}u NodeName: %{public}s destructed.",
81         GetNodeId(), GetNodeName().c_str());
82 #endif
83 }
84 
DoProcess()85 void HpaeRenderEffectNode::DoProcess()
86 {
87     HpaePcmBuffer *tempOut = nullptr;
88     std::vector<HpaePcmBuffer *>& preOutputs = inputStream_.ReadPreOutputData();
89     // if buffer is not valid, write silence data(invalid) to output
90     if (sceneType_ != "SCENE_COLLABORATIVE") {
91         if (enableProcess_ && !preOutputs.empty()) {
92             tempOut = SignalProcess(preOutputs);
93             outputStream_.WriteDataToOutput(tempOut);
94         } else if (!preOutputs.empty()) {
95             outputStream_.WriteDataToOutput(preOutputs[0]);
96         } else {
97             outputStream_.WriteDataToOutput(&silenceData_);
98         }
99         return;
100     }
101 
102     if (enableProcess_ && !preOutputs.empty() && directOutput_ && collaborativeOutput_) {
103         SignalProcess(preOutputs);
104         int32_t ret = SplitCollaborativeData();
105         if (ret != SUCCESS) {
106             outputStream_.WriteDataToOutput(&silenceData_);
107             outputStream_.WriteDataToOutput(&silenceData_, HPAE_BUFFER_TYPE_COBUFFER);
108         } else {
109             outputStream_.WriteDataToOutput(directOutput_.get());
110             outputStream_.WriteDataToOutput(collaborativeOutput_.get(), HPAE_BUFFER_TYPE_COBUFFER);
111         }
112     } else if (!preOutputs.empty()) {
113         outputStream_.WriteDataToOutput(preOutputs[0]);
114         outputStream_.WriteDataToOutput(preOutputs[0], HPAE_BUFFER_TYPE_COBUFFER);
115     } else {
116         outputStream_.WriteDataToOutput(&silenceData_);
117         outputStream_.WriteDataToOutput(&silenceData_, HPAE_BUFFER_TYPE_COBUFFER);
118     }
119 #ifdef ENABLE_HOOK_PCM
120     if (directPcmDumper_ && directOutput_) {
121         directPcmDumper_->Dump((int8_t *)directOutput_->GetPcmDataBuffer(),
122             directOutput_->GetFrameLen() * sizeof(float) * directOutput_->GetChannelCount());
123     }
124     if (collaborativePcmDumper_ && collaborativeOutput_) {
125         collaborativePcmDumper_->Dump((int8_t *)collaborativeOutput_->GetPcmDataBuffer(),
126             collaborativeOutput_->GetFrameLen() * sizeof(float) * collaborativeOutput_->GetChannelCount());
127     }
128 #endif
129 }
130 
SignalProcess(const std::vector<HpaePcmBuffer * > & inputs)131 HpaePcmBuffer *HpaeRenderEffectNode::SignalProcess(const std::vector<HpaePcmBuffer *> &inputs)
132 {
133     AUDIO_DEBUG_LOG("render effect node signal process in");
134     if (inputs.empty()) {
135         AUDIO_WARNING_LOG("HpaeRenderEffectNode inputs size is empty");
136         return nullptr;
137     }
138     auto rate = "rate[" + std::to_string(inputs[0]->GetSampleRate()) + "]_";
139     auto ch = "ch[" + std::to_string(inputs[0]->GetChannelCount()) + "]_";
140     auto len = "len[" + std::to_string(inputs[0]->GetFrameLen()) + "]";
141     Trace trace("[" + sceneType_ + "]HpaeRenderEffectNode::SignalProcess " + rate + ch + len);
142 
143     if (AudioEffectChainManager::GetInstance()->GetOffloadEnabled()) {
144         return inputs[0];
145     }
146     if (IsByPassEffectZeroVolume(inputs[0])) {
147         return inputs[0];
148     }
149 
150     ReconfigOutputBuffer();
151 
152     auto eBufferAttr = std::make_unique<EffectBufferAttr>(
153         inputs[0]->GetPcmDataBuffer(),
154         effectOutput_.GetPcmDataBuffer(),
155         static_cast<int32_t>(inputs[0]->GetChannelCount()),
156         static_cast<int32_t>(inputs[0]->GetFrameLen()),
157         0,
158         0
159     );
160 
161     int32_t ret = AudioEffectChainManager::GetInstance()->ApplyAudioEffectChain(sceneType_, eBufferAttr);
162     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, inputs[0], "apply audio effect chain fail");
163 
164     effectOutput_.SetBufferState(inputs[0]->GetBufferState());
165     return &effectOutput_;
166 }
167 
SplitCollaborativeData()168 int32_t HpaeRenderEffectNode::SplitCollaborativeData()
169 {
170     CHECK_AND_RETURN_RET_LOG(effectOutput_.GetChannelCount() == static_cast<uint32_t>(CHANNEL_4), ERROR,
171         "collaborative channel count is invalid, count: %{public}d", CHANNEL_4);
172     // need to check whether the sample rate or frame length changes
173     // currently, sample rate and frame length will not change
174     float *tempOutput = effectOutput_.GetPcmDataBuffer();
175     float *directOutput = directOutput_->GetPcmDataBuffer();
176     float *collaborativeOutput = collaborativeOutput_->GetPcmDataBuffer();
177     for (uint32_t i = 0; i < effectOutput_.GetFrameLen(); ++i) {
178         directOutput[DIRECT_CHANNELS * i] = tempOutput[COLLABORATIVE_OUTPUT_CHANNELS * i];
179         directOutput[DIRECT_CHANNELS * i + 1] = tempOutput[COLLABORATIVE_OUTPUT_CHANNELS * i + 1];
180         collaborativeOutput[COLLABORATIVE_CHANNELS * i] =
181             tempOutput[COLLABORATIVE_OUTPUT_CHANNELS * i + COLLABORATIVE_OUTPUT_CHANNEL_1_INDEX];
182         collaborativeOutput[COLLABORATIVE_CHANNELS * i + 1] =
183             tempOutput[COLLABORATIVE_OUTPUT_CHANNELS * i + COLLABORATIVE_OUTPUT_CHANNEL_2_INDEX];
184     }
185     return SUCCESS;
186 }
187 
AudioRendererCreate(HpaeNodeInfo & nodeInfo)188 int32_t HpaeRenderEffectNode::AudioRendererCreate(HpaeNodeInfo &nodeInfo)
189 {
190     AUDIO_INFO_LOG("notify audio effect when audio renderer create in");
191     int32_t ret = CreateAudioEffectChain(nodeInfo);
192     if (ret != 0) {
193         AUDIO_WARNING_LOG("create audio effect chain failed, ret: %{public}d", ret);
194     }
195     AUDIO_INFO_LOG("notify audio effect when audio renderer create out");
196     return SUCCESS;
197 }
198 
AudioRendererStart(HpaeNodeInfo & nodeInfo)199 int32_t HpaeRenderEffectNode::AudioRendererStart(HpaeNodeInfo &nodeInfo)
200 {
201     AUDIO_INFO_LOG("notify audio effect when audio renderer start in");
202     ModifyAudioEffectChainInfo(nodeInfo, ADD_AUDIO_EFFECT_CHAIN_INFO);
203     AUDIO_INFO_LOG("notify audio effect when audio renderer start out");
204     return SUCCESS;
205 }
206 
AudioRendererStop(HpaeNodeInfo & nodeInfo)207 int32_t HpaeRenderEffectNode::AudioRendererStop(HpaeNodeInfo &nodeInfo)
208 {
209     AUDIO_INFO_LOG("notify audio effect when audio renderer stop in");
210     ModifyAudioEffectChainInfo(nodeInfo, REMOVE_AUDIO_EFFECT_CHAIN_INFO);
211     AUDIO_INFO_LOG("notify audio effect when audio renderer stop out");
212     return SUCCESS;
213 }
214 
AudioRendererRelease(HpaeNodeInfo & nodeInfo)215 int32_t HpaeRenderEffectNode::AudioRendererRelease(HpaeNodeInfo &nodeInfo)
216 {
217     AUDIO_INFO_LOG("notify audio effect when audio renderer release in");
218     int32_t ret = ReleaseAudioEffectChain(nodeInfo);
219     if (ret != 0) {
220         AUDIO_WARNING_LOG("release audio effect chain failed, ret: %{public}d", ret);
221     }
222     ModifyAudioEffectChainInfo(nodeInfo, REMOVE_AUDIO_EFFECT_CHAIN_INFO);
223     AUDIO_INFO_LOG("notify audio effect when audio renderer release out");
224     return SUCCESS;
225 }
226 
CreateAudioEffectChain(HpaeNodeInfo & nodeInfo)227 int32_t HpaeRenderEffectNode::CreateAudioEffectChain(HpaeNodeInfo &nodeInfo)
228 {
229     AUDIO_INFO_LOG("Create Audio Effect Chain In, sessionID is %{public}u, sceneType is %{public}d",
230         nodeInfo.sessionId, nodeInfo.effectInfo.effectScene);
231     // todo: deal with remote case
232     // todo: if boot music, do not create audio effect
233     AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
234     CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERR_INVALID_HANDLE, "null audioEffectChainManager");
235     std::string sceneType = "EFFECT_NONE";
236     const std::unordered_map<AudioEffectScene, std::string> &audioSupportedSceneTypes = GetSupportedSceneType();
237     if (audioSupportedSceneTypes.find(nodeInfo.effectInfo.effectScene) !=
238         audioSupportedSceneTypes.end()) {
239         sceneType = audioSupportedSceneTypes.at(nodeInfo.effectInfo.effectScene);
240     }
241     // todo: could be removed
242     if (!audioEffectChainManager->CheckAndAddSessionID(std::to_string(nodeInfo.sessionId))) {
243         return SUCCESS;
244     }
245     audioEffectChainManager->UpdateSceneTypeList(sceneType, ADD_SCENE_TYPE);
246     // todo: should be considered
247     if (audioEffectChainManager->GetOffloadEnabled()) {
248         return SUCCESS;
249     }
250     int32_t ret = audioEffectChainManager->CreateAudioEffectChainDynamic(sceneType);
251     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERROR, "create effect chain fail");
252     AUDIO_INFO_LOG("Create Audio Effect Chain Success, sessionID is %{public}u, sceneType is %{public}d",
253         nodeInfo.sessionId, nodeInfo.effectInfo.effectScene);
254     return SUCCESS;
255 }
256 
ReleaseAudioEffectChain(HpaeNodeInfo & nodeInfo)257 int32_t HpaeRenderEffectNode::ReleaseAudioEffectChain(HpaeNodeInfo &nodeInfo)
258 {
259     AUDIO_INFO_LOG("Release Audio Effect Chain In, sessionID is %{public}u, sceneType is %{public}d",
260         nodeInfo.sessionId, nodeInfo.effectInfo.effectScene);
261     // todo: deal with remote case
262     // todo: if boot music, do not release audio effect
263     AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
264     CHECK_AND_RETURN_RET_LOG(audioEffectChainManager != nullptr, ERR_INVALID_HANDLE, "null audioEffectChainManager");
265     std::string sceneType = "EFFECT_NONE";
266     const std::unordered_map<AudioEffectScene, std::string> &audioSupportedSceneTypes = GetSupportedSceneType();
267     if (audioSupportedSceneTypes.find(nodeInfo.effectInfo.effectScene) !=
268         audioSupportedSceneTypes.end()) {
269         sceneType = audioSupportedSceneTypes.at(nodeInfo.effectInfo.effectScene);
270     }
271     // todo: could be removed
272     if (!audioEffectChainManager->CheckAndRemoveSessionID(std::to_string(nodeInfo.sessionId))) {
273         return SUCCESS;
274     }
275     audioEffectChainManager->UpdateSceneTypeList(sceneType, REMOVE_SCENE_TYPE);
276     // todo: should be considered
277     if (audioEffectChainManager->GetOffloadEnabled()) {
278         return SUCCESS;
279     }
280     int32_t ret = audioEffectChainManager->ReleaseAudioEffectChainDynamic(sceneType);
281     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ERROR, "release effect chain fail");
282     AUDIO_INFO_LOG("Release Audio Effect Chain Success, sessionID is %{public}u, sceneType is %{public}d",
283         nodeInfo.sessionId, nodeInfo.effectInfo.effectScene);
284     return SUCCESS;
285 }
286 
ModifyAudioEffectChainInfo(HpaeNodeInfo & nodeInfo,ModifyAudioEffectChainInfoReason reason)287 void HpaeRenderEffectNode::ModifyAudioEffectChainInfo(HpaeNodeInfo &nodeInfo,
288     ModifyAudioEffectChainInfoReason reason)
289 {
290     std::string sessionID = std::to_string(nodeInfo.sessionId);
291     std::string sceneType = "EFFECT_NONE";
292     const std::unordered_map<AudioEffectScene, std::string> &audioSupportedSceneTypes = GetSupportedSceneType();
293     if (audioSupportedSceneTypes.find(nodeInfo.effectInfo.effectScene) !=
294         audioSupportedSceneTypes.end()) {
295         sceneType = audioSupportedSceneTypes.at(nodeInfo.effectInfo.effectScene);
296     }
297     int32_t ret = 0;
298     switch (reason) {
299         case ADD_AUDIO_EFFECT_CHAIN_INFO: {
300             const std::unordered_map<AudioEffectMode, std::string> &audioSupportedSceneModes =
301                 GetAudioSupportedSceneModes();
302             SessionEffectInfo info;
303             auto sceneMode = audioSupportedSceneModes.find(nodeInfo.effectInfo.effectMode);
304             if (sceneMode != audioSupportedSceneModes.end()) {
305                 info.sceneMode = sceneMode->second;
306             } else {
307                 AUDIO_WARNING_LOG("sceneMode: %{public}d is not supported", nodeInfo.effectInfo.effectMode);
308                 info.sceneMode = "EFFECT_NONE";
309             }
310             info.sceneType = sceneType;
311             info.channels = static_cast<uint32_t>(nodeInfo.channels);
312             info.channelLayout = nodeInfo.channelLayout;
313             info.streamUsage = nodeInfo.effectInfo.streamUsage;
314             info.systemVolumeType = static_cast<int32_t>(nodeInfo.effectInfo.systemVolumeType);
315             ret = AudioEffectChainManager::GetInstance()->SessionInfoMapAdd(sessionID, info);
316             break;
317         }
318         case REMOVE_AUDIO_EFFECT_CHAIN_INFO:
319             ret = AudioEffectChainManager::GetInstance()->SessionInfoMapDelete(sceneType, sessionID);
320             break;
321         default:
322             break;
323     }
324     CHECK_AND_RETURN_LOG(ret == SUCCESS, "modify session info failed");
325     UpdateAudioEffectChainInfo(nodeInfo);
326 }
327 
UpdateAudioEffectChainInfo(HpaeNodeInfo & nodeInfo)328 void HpaeRenderEffectNode::UpdateAudioEffectChainInfo(HpaeNodeInfo &nodeInfo)
329 {
330     AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
331     CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "null audioEffectChainManager");
332     std::string sceneType = "EFFECT_NONE";
333     const std::unordered_map<AudioEffectScene, std::string> &audioSupportedSceneTypes = GetSupportedSceneType();
334     if (audioSupportedSceneTypes.find(nodeInfo.effectInfo.effectScene) !=
335         audioSupportedSceneTypes.end()) {
336         sceneType = audioSupportedSceneTypes.at(nodeInfo.effectInfo.effectScene);
337     }
338     audioEffectChainManager->UpdateMultichannelConfig(sceneType);
339     audioEffectChainManager->EffectVolumeUpdate();
340     audioEffectChainManager->UpdateDefaultAudioEffect();
341     audioEffectChainManager->UpdateStreamUsage();
342 }
343 
ReconfigOutputBuffer()344 void HpaeRenderEffectNode::ReconfigOutputBuffer()
345 {
346     HpaeNodeInfo &effectNodeInfo = GetNodeInfo();
347     uint32_t channels = static_cast<uint32_t>(effectNodeInfo.channels);
348     uint64_t channelLayout = effectNodeInfo.channelLayout;
349     int32_t ret = AudioEffectChainManager::GetInstance()->GetOutputChannelInfo(sceneType_, channels, channelLayout);
350     if (ret != SUCCESS || channels == 0 || channelLayout == 0) {
351         AUDIO_WARNING_LOG("output channel info incorrect, scene type: %{public}s, "
352             "channels: %{public}u, channelLayout: %{public}" PRIu64, sceneType_.c_str(), channels, channelLayout);
353     } else if (static_cast<uint32_t>(effectNodeInfo.channels) != channels ||
354         static_cast<uint64_t>(effectNodeInfo.channelLayout) != channelLayout) {
355         AUDIO_INFO_LOG("output channel info changed, scene type: %{public}s, "
356             "channels change from %{public}u to %{public}u, "
357             "channelLayout change from %{public}" PRIu64 " to %{public}" PRIu64,
358             sceneType_.c_str(), effectNodeInfo.channels, channels, effectNodeInfo.channelLayout, channelLayout);
359         PcmBufferInfo pcmBufferInfo = PcmBufferInfo(channels, DEFAULT_EFFECT_FRAMELEN,
360             DEFUALT_EFFECT_RATE, channelLayout, effectOutput_.GetFrames());
361         pcmBufferInfo.isMultiFrames = effectOutput_.IsMultiFrames();
362         effectOutput_.ReConfig(pcmBufferInfo);
363         effectNodeInfo.channels = static_cast<AudioChannel>(channels);
364         effectNodeInfo.channelLayout = static_cast<AudioChannelLayout>(channelLayout);
365         effectNodeInfo.samplingRate = static_cast<AudioSamplingRate>(DEFUALT_EFFECT_RATE);
366         effectNodeInfo.frameLen = static_cast<uint32_t>(DEFAULT_EFFECT_FRAMELEN);
367         SetNodeInfo(effectNodeInfo);
368 #ifdef ENABLE_HIDUMP_DFX
369         if (auto callBack = GetNodeStatusCallback().lock()) {
370             callBack->OnNotifyDfxNodeInfoChanged(GetNodeId(), GetNodeInfo());
371         }
372 #endif
373     }
374 }
375 
GetExpectedInputChannelInfo(AudioBasicFormat & basicFormat)376 int32_t HpaeRenderEffectNode::GetExpectedInputChannelInfo(AudioBasicFormat &basicFormat)
377 {
378     basicFormat.rate = static_cast<AudioSamplingRate>(DEFUALT_EFFECT_RATE);
379     uint64_t channelLayout = 0;
380     int32_t ret = AudioEffectChainManager::GetInstance()->QueryEffectChannelInfo(sceneType_,
381         basicFormat.audioChannelInfo.numChannels, channelLayout);
382     basicFormat.audioChannelInfo.channelLayout = static_cast<AudioChannelLayout>(channelLayout);
383     return ret;
384 }
385 
IsByPassEffectZeroVolume(HpaePcmBuffer * pcmBuffer)386 bool HpaeRenderEffectNode::IsByPassEffectZeroVolume(HpaePcmBuffer *pcmBuffer)
387 {
388     if (!pcmBuffer->IsValid()) {
389         return false;
390     }
391     if (pcmBuffer->IsSilence()) {
392         if (!isDisplayEffectZeroVolume_) {
393             AUDIO_INFO_LOG("Timing begins, will close [%{public}s] effect after [%{public}" PRId64 "]s",
394                 sceneType_.c_str(), WAIT_CLOSE_EFFECT_TIME);
395             isDisplayEffectZeroVolume_ = true;
396         }
397         silenceDataUs_ += static_cast<int64_t>(pcmBuffer->GetFrameLen() * TIME_IN_US / pcmBuffer->GetSampleRate());
398         if (!isByPassEffect_ && silenceDataUs_ >= WAIT_CLOSE_EFFECT_TIME * TIME_IN_US) {
399             AUDIO_INFO_LOG("Volume change to zero over %{public}" PRId64 "s, close effect:%{public}s success.",
400                 WAIT_CLOSE_EFFECT_TIME, sceneType_.c_str());
401             isByPassEffect_ = true;
402             silenceDataUs_ = 0;
403         } else if (isByPassEffect_ && silenceDataUs_ >= MONITOR_CLOSE_EFFECT_TIME * TIME_IN_US) {
404             silenceDataUs_ = 0;
405             AUDIO_INFO_LOG("Effect [%{public}s] have closed [%{public}" PRId64 "]s.",
406                 sceneType_.c_str(), MONITOR_CLOSE_EFFECT_TIME);
407         }
408     } else {
409         if (isDisplayEffectZeroVolume_) {
410             AUDIO_INFO_LOG("Volume change to non zero, open effect:%{public}s success.", sceneType_.c_str());
411             isDisplayEffectZeroVolume_ = false;
412         }
413         silenceDataUs_ = 0;
414         isByPassEffect_ = false;
415     }
416     return isByPassEffect_;
417 }
418 
InitEffectBuffer(const uint32_t sessionId)419 void HpaeRenderEffectNode::InitEffectBuffer(const uint32_t sessionId)
420 {
421     AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
422     CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "null audioEffectChainManager");
423     audioEffectChainManager->InitEffectBuffer(std::to_string(sessionId));
424 }
425 
InitEffectBufferFromDisConnect()426 void HpaeRenderEffectNode::InitEffectBufferFromDisConnect()
427 {
428     AudioEffectChainManager *audioEffectChainManager = AudioEffectChainManager::GetInstance();
429     CHECK_AND_RETURN_LOG(audioEffectChainManager != nullptr, "null audioEffectChainManager");
430     audioEffectChainManager->InitAudioEffectChainDynamic(sceneType_);
431     AUDIO_INFO_LOG("begin InitEffectBuffer from DisConnect, sceneType:%{public}s", sceneType_.c_str());
432 }
433 } // namespace HPAE
434 } // namespace AudioStandard
435 } // namespace OHOS