• 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 <gtest/gtest.h>
16 #include <cmath>
17 #include <memory>
18 #include "hpae_sink_input_node.h"
19 #include "hpae_mixer_node.h"
20 #include "hpae_sink_output_node.h"
21 #include "test_case_common.h"
22 #include "audio_errors.h"
23 
24 using namespace OHOS;
25 using namespace AudioStandard;
26 using namespace HPAE;
27 using namespace testing::ext;
28 using namespace testing;
29 namespace {
30 static constexpr int32_t TEST_VALUE1 = 100;
31 static constexpr int32_t TEST_VALUE2 = 200;
32 static constexpr uint32_t TEST_ID = 1243;
33 static constexpr uint32_t TEST_FRAMELEN = 960;
34 class HpaeMixerNodeTest : public testing::Test {
35 public:
36     void SetUp();
37     void TearDown();
38 };
39 
SetUp()40 void HpaeMixerNodeTest::SetUp()
41 {}
42 
TearDown()43 void HpaeMixerNodeTest::TearDown()
44 {}
45 
46 static int32_t g_testValue = 0;
47 
48 HWTEST_F(HpaeMixerNodeTest, constructHpaeMixerNode, TestSize.Level0)
49 {
50     HpaeNodeInfo nodeInfo;
51     nodeInfo.nodeId = TEST_ID;
52     nodeInfo.frameLen = TEST_FRAMELEN;
53     nodeInfo.samplingRate = SAMPLE_RATE_48000;
54     nodeInfo.channels = STEREO;
55     nodeInfo.format = SAMPLE_F32LE;
56     std::shared_ptr<HpaeMixerNode> hpaeMixerNode = std::make_shared<HpaeMixerNode>(nodeInfo);
57     EXPECT_EQ(hpaeMixerNode->GetSampleRate(), nodeInfo.samplingRate);
58     EXPECT_EQ(hpaeMixerNode->GetFrameLen(), nodeInfo.frameLen);
59     EXPECT_EQ(hpaeMixerNode->GetChannelCount(), nodeInfo.channels);
60     EXPECT_EQ(hpaeMixerNode->GetBitWidth(), nodeInfo.format);
61     HpaeNodeInfo &retNi = hpaeMixerNode->GetNodeInfo();
62     EXPECT_EQ(retNi.samplingRate, nodeInfo.samplingRate);
63     EXPECT_EQ(retNi.frameLen, nodeInfo.frameLen);
64     EXPECT_EQ(retNi.channels, nodeInfo.channels);
65     EXPECT_EQ(retNi.format, nodeInfo.format);
66 }
TestRendererRenderFrame(const char * data,uint64_t len)67 static int32_t TestRendererRenderFrame(const char *data, uint64_t len)
68 {
69     for (int32_t i = 0; i < len / SAMPLE_F32LE; i++) {
70         float diff = *((float*)data + i) - g_testValue;
71         EXPECT_EQ(fabs(diff) < TEST_VALUE_PRESION, true);
72     }
73     return 0;
74 }
75 
76 HWTEST_F(HpaeMixerNodeTest, testHpaePlayOutConnectNode, TestSize.Level0)
77 {
78     HpaeNodeInfo nodeInfo;
79     nodeInfo.nodeId = TEST_ID;
80     nodeInfo.frameLen = TEST_FRAMELEN;
81     nodeInfo.samplingRate = SAMPLE_RATE_48000;
82     nodeInfo.channels = STEREO;
83     nodeInfo.format = SAMPLE_F32LE;
84     std::shared_ptr<HpaeSinkOutputNode> hpaeSinkOutputNode = std::make_shared<HpaeSinkOutputNode>(nodeInfo);
85     std::shared_ptr<HpaeSinkInputNode> hpaeSinkInputNode0 = std::make_shared<HpaeSinkInputNode>(nodeInfo);
86     std::shared_ptr<HpaeSinkInputNode> hpaeSinkInputNode1 = std::make_shared<HpaeSinkInputNode>(nodeInfo);
87     std::shared_ptr<HpaeMixerNode> hpaeMixerNode = std::make_shared<HpaeMixerNode>(nodeInfo);
88     hpaeMixerNode->Connect(hpaeSinkInputNode0);
89     hpaeMixerNode->Connect(hpaeSinkInputNode1);
90     std::cout << "hpaeMixerNode->GetPreOutNum():" << hpaeMixerNode->GetPreOutNum() << std::endl;
91     EXPECT_EQ(hpaeSinkOutputNode->GetPreOutNum(), 0);
92     hpaeSinkOutputNode->Connect(hpaeMixerNode);
93     std::string deviceClass = "file_io";
94     std::string deviceNetId = "LocalDevice";
95     EXPECT_EQ(hpaeSinkOutputNode->GetPreOutNum(), 1);
96     EXPECT_EQ(hpaeSinkOutputNode->GetRenderSinkInstance(deviceClass, deviceNetId), 0);
97     EXPECT_EQ(hpaeSinkInputNode0.use_count(), 1 + 1);
98     EXPECT_EQ(hpaeSinkInputNode1.use_count(), 1 + 1);
99     EXPECT_EQ(hpaeMixerNode.use_count(), 1 + 1);
100     g_testValue = 0;
101     int32_t testValue = TEST_VALUE1;
102     std::shared_ptr<WriteFixedValueCb> writeFixedValueCb0 =
103         std::make_shared<WriteFixedValueCb>(SAMPLE_F32LE, testValue);
104     g_testValue = g_testValue + testValue;
105     hpaeSinkInputNode0->RegisterWriteCallback(writeFixedValueCb0);
106     testValue = TEST_VALUE2;
107     std::shared_ptr<WriteFixedValueCb> writeFixedValueCb1 =
108         std::make_shared<WriteFixedValueCb>(SAMPLE_F32LE, testValue);
109     g_testValue = g_testValue + testValue;
110     hpaeSinkInputNode1->RegisterWriteCallback(writeFixedValueCb1);
111     hpaeSinkOutputNode->DoProcess();
112     TestRendererRenderFrame(hpaeSinkOutputNode->GetRenderFrameData(), nodeInfo.frameLen * nodeInfo.channels *
113         GetSizeFromFormat(nodeInfo.format));
114     hpaeSinkOutputNode->DisConnect(hpaeMixerNode);
115     EXPECT_EQ(hpaeSinkOutputNode->GetPreOutNum(), 0);
116     EXPECT_EQ(hpaeMixerNode.use_count(), 1);
117     hpaeMixerNode->DisConnect(hpaeSinkInputNode0);
118     EXPECT_EQ(hpaeSinkInputNode0.use_count(), 1);
119     EXPECT_EQ(hpaeMixerNode->GetPreOutNum(), 1);
120     hpaeMixerNode->DisConnect(hpaeSinkInputNode1);
121     EXPECT_EQ(hpaeSinkInputNode1.use_count(), 1);
122     EXPECT_EQ(hpaeMixerNode->GetPreOutNum(), 0);
123 }
124 
125 } // namespace