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 #include <gtest/gtest.h>
17 #include <cmath>
18 #include <memory>
19 #include "hpae_sink_input_node.h"
20 #include "hpae_sink_output_node.h"
21 #include "hpae_gain_node.h"
22 #include "test_case_common.h"
23 #include "audio_errors.h"
24
25 using namespace OHOS;
26 using namespace AudioStandard;
27 using namespace HPAE;
28 using namespace testing::ext;
29 using namespace testing;
30
31 class HpaeGainNodeTest : public testing::Test {
32 public:
33 void SetUp();
34 void TearDown();
35 };
36
SetUp()37 void HpaeGainNodeTest::SetUp()
38 {}
39
TearDown()40 void HpaeGainNodeTest::TearDown()
41 {}
42
43 static int32_t g_testValue = 0;
44
45 namespace {
46
47 constexpr uint32_t DEFAULT_NODE_ID = 1234;
48 constexpr uint32_t DEFAULT_FRAME_LEN = 960;
49 constexpr uint32_t DEFAULT_NUM_TWO = 2;
50
51 HWTEST_F(HpaeGainNodeTest, constructHpaeGainNode, TestSize.Level0)
52 {
53 HpaeNodeInfo nodeInfo;
54 nodeInfo.nodeId = DEFAULT_NODE_ID;
55 nodeInfo.frameLen = DEFAULT_FRAME_LEN;
56 nodeInfo.samplingRate = SAMPLE_RATE_48000;
57 nodeInfo.channels = STEREO;
58 nodeInfo.format = SAMPLE_F32LE;
59 nodeInfo.deviceClass = "primary";
60 std::shared_ptr<HpaeGainNode> hpaeGainNode = std::make_shared<HpaeGainNode>(nodeInfo);
61 EXPECT_EQ(hpaeGainNode->GetSampleRate(), nodeInfo.samplingRate);
62 EXPECT_EQ(hpaeGainNode->GetFrameLen(), nodeInfo.frameLen);
63 EXPECT_EQ(hpaeGainNode->GetChannelCount(), nodeInfo.channels);
64 EXPECT_EQ(hpaeGainNode->GetBitWidth(), nodeInfo.format);
65 std::cout << "HpaeGainNodeTest::GetNodeInfo" << std::endl;
66 HpaeNodeInfo &retNi = hpaeGainNode->GetNodeInfo();
67 EXPECT_EQ(retNi.samplingRate, nodeInfo.samplingRate);
68 std::cout << "samplingRate: " << retNi.samplingRate << std::endl;
69 EXPECT_EQ(retNi.frameLen, nodeInfo.frameLen);
70 std::cout << "frameLen: " << retNi.frameLen << std::endl;
71 EXPECT_EQ(retNi.channels, nodeInfo.channels);
72 std::cout << "channels: " << retNi.channels << std::endl;
73 EXPECT_EQ(retNi.format, nodeInfo.format);
74 std::cout << "format: " << retNi.format << std::endl;
75 EXPECT_EQ(retNi.deviceClass, nodeInfo.deviceClass);
76 std::cout << "deviceClass: " << retNi.deviceClass << std::endl;
77 std::cout << "HpaeGainNodeTest::GetNodeInfo end" << std::endl;
78 }
TestRendererRenderFrame(const char * data,uint64_t len)79 static int32_t TestRendererRenderFrame(const char *data, uint64_t len)
80 {
81 float curGain = 0.0f;
82 float targetGain = 1.0f;
83 float stepGain = targetGain - curGain;
84 uint64_t frameLen = len / (SAMPLE_F32LE * STEREO);
85 stepGain = stepGain / frameLen;
86 const float *tempData = reinterpret_cast<const float *>(data);
87 for (int32_t i = 0; i < frameLen; i++) {
88 const float left = tempData[DEFAULT_NUM_TWO * i];
89 const float right = tempData[DEFAULT_NUM_TWO * i + 1];
90 const float expectedValue = g_testValue * (curGain + i * stepGain);
91 EXPECT_EQ(left, expectedValue);
92 EXPECT_EQ(right, expectedValue);
93 }
94 return 0;
95 }
96
97 HWTEST_F(HpaeGainNodeTest, testHpaeGainTestNode, TestSize.Level0)
98 {
99 HpaeNodeInfo nodeInfo;
100 nodeInfo.nodeId = DEFAULT_NODE_ID;
101 nodeInfo.frameLen = DEFAULT_FRAME_LEN;
102 nodeInfo.samplingRate = SAMPLE_RATE_48000;
103 nodeInfo.channels = STEREO;
104 nodeInfo.format = SAMPLE_F32LE;
105 nodeInfo.streamType = STREAM_MUSIC;
106 std::shared_ptr<HpaeSinkOutputNode> hpaeSinkOutputNode = std::make_shared<HpaeSinkOutputNode>(nodeInfo);
107 std::shared_ptr<HpaeSinkInputNode> hpaeSinkInputNode = std::make_shared<HpaeSinkInputNode>(nodeInfo);
108 std::shared_ptr<HpaeGainNode> hpaeGainNode = std::make_shared<HpaeGainNode>(nodeInfo);
109 hpaeGainNode->Connect(hpaeSinkInputNode);
110 hpaeSinkOutputNode->Connect(hpaeGainNode);
111 std::string deviceClass = "file_io";
112 std::string deviceNetId = "LocalDevice";
113 EXPECT_EQ(hpaeSinkOutputNode->GetRenderSinkInstance(deviceClass, deviceNetId), 0);
114 EXPECT_EQ(hpaeSinkInputNode.use_count(), DEFAULT_NUM_TWO);
115 EXPECT_EQ(hpaeGainNode.use_count(), DEFAULT_NUM_TWO);
116 EXPECT_EQ(hpaeSinkOutputNode.use_count(), 1);
117 g_testValue = 0;
118 int32_t testValue = 100;
119 std::shared_ptr<WriteFixedValueCb> writeFixedValueCb0 =
120 std::make_shared<WriteFixedValueCb>(SAMPLE_F32LE, testValue);
121 g_testValue = testValue;
122 hpaeSinkInputNode->RegisterWriteCallback(writeFixedValueCb0);
123 hpaeSinkOutputNode->DoProcess();
124 TestRendererRenderFrame(hpaeSinkOutputNode->GetRenderFrameData(),
125 nodeInfo.frameLen * nodeInfo.channels * GetSizeFromFormat(nodeInfo.format));
126 hpaeSinkOutputNode->DoProcess();
127 TestRendererRenderFrame(hpaeSinkOutputNode->GetRenderFrameData(),
128 nodeInfo.frameLen * nodeInfo.channels * GetSizeFromFormat(nodeInfo.format));
129 hpaeSinkOutputNode->DisConnect(hpaeGainNode);
130 EXPECT_EQ(hpaeGainNode.use_count(), 1);
131 hpaeGainNode->DisConnect(hpaeSinkInputNode);
132 EXPECT_EQ(hpaeSinkInputNode.use_count(), 1);
133 }
134 } // namespace
135