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_source_input_node.h"
20 #include "hpae_source_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
30 namespace OHOS {
31 namespace AudioStandard {
32 namespace HPAE {
33
34 const uint32_t DEFAULT_FRAME_LENGTH = 960;
35 const uint32_t DEFAULT_NODE_ID = 1243;
36 static std::string g_rootCapturerPath = "/data/source_file_io_48000_2_s16le.pcm";
37
38 class HpaeSourceOutputNodeTest : public testing::Test {
39 public:
40 void SetUp();
41 void TearDown();
42 };
43
SetUp()44 void HpaeSourceOutputNodeTest::SetUp()
45 {}
46
TearDown()47 void HpaeSourceOutputNodeTest::TearDown()
48 {}
49
50 class TestReadDataCb : public ICapturerStreamCallback, public std::enable_shared_from_this<TestReadDataCb> {
51 public:
OnStreamData(AudioCallBackCapturerStreamInfo & callBackStreamInfo)52 int32_t OnStreamData(AudioCallBackCapturerStreamInfo &callBackStreamInfo) override
53 {
54 return SUCCESS;
55 }
TestReadDataCb()56 TestReadDataCb()
57 {}
~TestReadDataCb()58 virtual ~TestReadDataCb()
59 {}
60 };
61
62 HWTEST_F(HpaeSourceOutputNodeTest, constructHpaeSourceOutputNode, TestSize.Level0)
63 {
64 HpaeNodeInfo nodeInfo;
65 nodeInfo.nodeId = DEFAULT_NODE_ID;
66 nodeInfo.frameLen = DEFAULT_FRAME_LENGTH;
67 nodeInfo.samplingRate = SAMPLE_RATE_48000;
68 nodeInfo.channels = STEREO;
69 nodeInfo.format = SAMPLE_F32LE;
70 std::shared_ptr<HpaeSourceOutputNode> hpaeSoruceOutputNode = std::make_shared<HpaeSourceOutputNode>(nodeInfo);
71 EXPECT_EQ(hpaeSoruceOutputNode->GetSampleRate(), nodeInfo.samplingRate);
72 EXPECT_EQ(hpaeSoruceOutputNode->GetFrameLen(), nodeInfo.frameLen);
73 EXPECT_EQ(hpaeSoruceOutputNode->GetChannelCount(), nodeInfo.channels);
74 EXPECT_EQ(hpaeSoruceOutputNode->GetBitWidth(), nodeInfo.format);
75 HpaeNodeInfo &retNi = hpaeSoruceOutputNode->GetNodeInfo();
76 EXPECT_EQ(retNi.samplingRate, nodeInfo.samplingRate);
77 EXPECT_EQ(retNi.frameLen, nodeInfo.frameLen);
78 EXPECT_EQ(retNi.channels, nodeInfo.channels);
79 EXPECT_EQ(retNi.format, nodeInfo.format);
80 }
81
GetTestAudioSourceAttr(IAudioSourceAttr & attr)82 static void GetTestAudioSourceAttr(IAudioSourceAttr &attr)
83 {
84 attr.adapterName = "";
85 attr.openMicSpeaker = 0;
86 attr.format = AudioSampleFormat::INVALID_WIDTH;
87 attr.sampleRate = SAMPLE_RATE_48000;
88 attr.channel = STEREO;
89 attr.volume = 0.0f;
90 attr.bufferSize = 0;
91 attr.isBigEndian = false;
92 attr.filePath = g_rootCapturerPath;
93 attr.deviceNetworkId = "";
94 attr.deviceType = 0;
95 attr.sourceType = 0;
96 attr.channelLayout = 0;
97 attr.audioStreamFlag = 0;
98 }
99
100 HWTEST_F(HpaeSourceOutputNodeTest, connectHpaeSourceInputAndOutputNode, TestSize.Level0)
101 {
102 HpaeNodeInfo nodeInfo;
103 nodeInfo.nodeId = DEFAULT_NODE_ID;
104 nodeInfo.frameLen = DEFAULT_FRAME_LENGTH;
105 nodeInfo.samplingRate = SAMPLE_RATE_48000;
106 nodeInfo.channels = STEREO;
107 nodeInfo.format = SAMPLE_S16LE;
108 nodeInfo.sourceBufferType = HPAE_SOURCE_BUFFER_TYPE_MIC;
109 nodeInfo.sourceInputNodeType = HPAE_SOURCE_MIC;
110 std::shared_ptr<HpaeSourceInputNode> hpaeSoruceInputNode = std::make_shared<HpaeSourceInputNode>(nodeInfo);
111 std::string deviceClass = "file_io";
112 std::string deviceNetId = "LocalDevice";
113 SourceType sourceType = SOURCE_TYPE_MIC;
114 std::string sourceName = "mic";
115 EXPECT_EQ(hpaeSoruceInputNode->GetCapturerSourceInstance(deviceClass, deviceNetId, sourceType, sourceName), 0);
116 IAudioSourceAttr attr;
117 GetTestAudioSourceAttr(attr);
118 std::shared_ptr<HpaeSourceOutputNode> hpaeSoruceOutputNode = std::make_shared<HpaeSourceOutputNode>(nodeInfo);
119 EXPECT_EQ(hpaeSoruceInputNode->CapturerSourceInit(attr), SUCCESS);
120 EXPECT_EQ(hpaeSoruceInputNode->CapturerSourceStart(), 0);
121 EXPECT_EQ(hpaeSoruceInputNode->GetSourceState() == STREAM_MANAGER_RUNNING, true);
122
123 hpaeSoruceOutputNode->Connect(hpaeSoruceInputNode);
124 EXPECT_EQ(hpaeSoruceInputNode.use_count(), 2); // 2 for test
125 hpaeSoruceOutputNode->DoProcess();
126
127 std::shared_ptr<TestReadDataCb> testReadDataCb = std::make_shared<TestReadDataCb>();
128 hpaeSoruceOutputNode->RegisterReadCallback(testReadDataCb);
129 hpaeSoruceOutputNode->DoProcess();
130
131 hpaeSoruceOutputNode->DisConnect(hpaeSoruceInputNode);
132 EXPECT_EQ(hpaeSoruceInputNode.use_count(), 1);
133 EXPECT_EQ(hpaeSoruceInputNode->CapturerSourceStop(), 0);
134 EXPECT_EQ(hpaeSoruceInputNode->GetSourceState() == STREAM_MANAGER_SUSPENDED, true);
135 }
136 } // namespace HPAE
137 } // namespace AudioStandard
138 } // namespace OHOS