• 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 
17 #include <gtest/gtest.h>
18 #include <parcel.h>
19 
20 #include "audio_errors.h"
21 #include "audio_limiter_manager.h"
22 #include "audio_service_log.h"
23 #include "audio_stream_info.h"
24 
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace AudioStandard {
29 
30 const int32_t TEST_MAX_REQUEST = 7680; // buffer size for 20ms 2channel 48000Hz
31 const int32_t AUDIO_MS_PER_S = 1000;
32 const int32_t PROC_COUNT = 4; // process 4 times
33 static AudioLmtManager *limiterManager;
34 
35 class AudioLimiterManagerUnitTest : public testing::Test {
36 public:
37     static void SetUpTestCase(void);
38     static void TearDownTestCase(void);
39     void SetUp();
40     void TearDown();
41 };
42 
SetUpTestCase(void)43 void AudioLimiterManagerUnitTest::SetUpTestCase(void) {}
44 
TearDownTestCase(void)45 void AudioLimiterManagerUnitTest::TearDownTestCase(void) {}
46 
SetUp(void)47 void AudioLimiterManagerUnitTest::SetUp(void)
48 {
49     limiterManager = AudioLmtManager::GetInstance();
50 }
51 
TearDown(void)52 void AudioLimiterManagerUnitTest::TearDown(void) {}
53 
54 /**
55  * @tc.name  : Test CreateLimiter API
56  * @tc.type  : FUNC
57  * @tc.number: CreateLimiter_001
58  * @tc.desc  : Test CreateLimiter interface when first create.
59  */
60 HWTEST_F(AudioLimiterManagerUnitTest, CreateLimiter_001, TestSize.Level1)
61 {
62     EXPECT_NE(limiterManager, nullptr);
63 
64     int32_t sinkIndex = 0;
65     int32_t ret = limiterManager->CreateLimiter(sinkIndex);
66     EXPECT_EQ(ret, SUCCESS);
67 }
68 
69 /**
70  * @tc.name  : Test CreateLimiter API
71  * @tc.type  : FUNC
72  * @tc.number: CreateLimiter_002
73  * @tc.desc  : Test CreateLimiter interface when repeate create use the same sinkIndex.
74  */
75 HWTEST_F(AudioLimiterManagerUnitTest, CreateLimiter_002, TestSize.Level1)
76 {
77     EXPECT_NE(limiterManager, nullptr);
78 
79     int32_t sinkIndex = 0;
80     int32_t ret = limiterManager->CreateLimiter(sinkIndex);
81     EXPECT_EQ(ret, SUCCESS);
82     ret = limiterManager->CreateLimiter(sinkIndex);
83     EXPECT_EQ(ret, SUCCESS);
84 }
85 
86 /**
87  * @tc.name  : Test SetLimiterConfig API
88  * @tc.type  : FUNC
89  * @tc.number: SetLimiterConfig_001
90  * @tc.desc  : Test SetLimiterConfig interface when config is vaild.
91  */
92 HWTEST_F(AudioLimiterManagerUnitTest, SetLimiterConfig_001, TestSize.Level1)
93 {
94     EXPECT_NE(limiterManager, nullptr);
95 
96     int32_t sinkIndex = 0;
97     int32_t ret = limiterManager->CreateLimiter(sinkIndex);
98     EXPECT_EQ(ret, SUCCESS);
99     ret = limiterManager->SetLimiterConfig(sinkIndex, TEST_MAX_REQUEST, SAMPLE_F32LE, SAMPLE_RATE_48000, STEREO);
100     EXPECT_EQ(ret, SUCCESS);
101 }
102 
103 /**
104  * @tc.name  : Test SetLimiterConfig API
105  * @tc.type  : FUNC
106  * @tc.number: SetLimiterConfig_002
107  * @tc.desc  : Test SetLimiterConfig interface when config is invaild.
108  */
109 HWTEST_F(AudioLimiterManagerUnitTest, SetLimiterConfig_002, TestSize.Level1)
110 {
111     EXPECT_NE(limiterManager, nullptr);
112 
113     int32_t sinkIndex = 0;
114     int32_t ret = limiterManager->CreateLimiter(sinkIndex);
115     EXPECT_EQ(ret, SUCCESS);
116     ret = limiterManager->SetLimiterConfig(sinkIndex, TEST_MAX_REQUEST, SAMPLE_F32LE, SAMPLE_RATE_48000, MONO);
117     EXPECT_EQ(ret, ERROR);
118 }
119 
120 /**
121  * @tc.name  : Test ProcessLimiter API
122  * @tc.type  : FUNC
123  * @tc.number: ProcessLimiter_001
124  * @tc.desc  : Test ProcessLimiter interface when inBuffer or outBuffer is nullptr.
125  */
126 HWTEST_F(AudioLimiterManagerUnitTest, ProcessLimiter_001, TestSize.Level1)
127 {
128     EXPECT_NE(limiterManager, nullptr);
129 
130     int32_t sinkIndex = 0;
131     int32_t frameLen = TEST_MAX_REQUEST / SAMPLE_F32LE;
132     float *inBuffer = nullptr;
133     float *outBuffer = nullptr;
134     int32_t ret = limiterManager->ProcessLimiter(sinkIndex, frameLen, inBuffer, outBuffer);
135     EXPECT_EQ(ret, ERROR);
136 }
137 
138 /**
139  * @tc.name  : Test ProcessLimiter API
140  * @tc.type  : FUNC
141  * @tc.number: ProcessLimiter_002
142  * @tc.desc  : Test ProcessLimiter interface when frameLen is vaild.
143  */
144 HWTEST_F(AudioLimiterManagerUnitTest, ProcessLimiter_002, TestSize.Level1)
145 {
146     EXPECT_NE(limiterManager, nullptr);
147 
148     int32_t sinkIndex = 0;
149     int32_t ret = limiterManager->CreateLimiter(sinkIndex);
150     EXPECT_EQ(ret, SUCCESS);
151     ret = limiterManager->SetLimiterConfig(sinkIndex, TEST_MAX_REQUEST, SAMPLE_F32LE, SAMPLE_RATE_48000, STEREO);
152     EXPECT_EQ(ret, SUCCESS);
153     int32_t frameLen = TEST_MAX_REQUEST / SAMPLE_F32LE;
154     std::vector<float> inBufferVector(frameLen, 0);
155     std::vector<float> outBufferVector(frameLen, 0);
156     float *inBuffer = inBufferVector.data();
157     float *outBuffer = outBufferVector.data();
158     ret = limiterManager->ProcessLimiter(sinkIndex, frameLen, inBuffer, outBuffer);
159     EXPECT_EQ(ret, SUCCESS);
160 }
161 
162 /**
163  * @tc.name  : Test ProcessLimiter API
164  * @tc.type  : FUNC
165  * @tc.number: ProcessLimiter_003
166  * @tc.desc  : Test ProcessLimiter interface when frameLen is invaild.
167  */
168 HWTEST_F(AudioLimiterManagerUnitTest, ProcessLimiter_003, TestSize.Level1)
169 {
170     EXPECT_NE(limiterManager, nullptr);
171 
172     int32_t sinkIndex = 0;
173     int32_t ret = limiterManager->CreateLimiter(sinkIndex);
174     EXPECT_EQ(ret, SUCCESS);
175     ret = limiterManager->SetLimiterConfig(sinkIndex, TEST_MAX_REQUEST, SAMPLE_F32LE, SAMPLE_RATE_48000, STEREO);
176     EXPECT_EQ(ret, SUCCESS);
177     int32_t frameLen = TEST_MAX_REQUEST / SAMPLE_F32LE;
178     std::vector<float> inBufferVector(frameLen, 0);
179     std::vector<float> outBufferVector(frameLen, 0);
180     float *inBuffer = inBufferVector.data();
181     float *outBuffer = outBufferVector.data();
182     ret = limiterManager->ProcessLimiter(sinkIndex, 0, inBuffer, outBuffer);
183     EXPECT_EQ(ret, ERROR);
184 }
185 
186 /**
187  * @tc.name  : Test ReleaseLimiter API
188  * @tc.type  : FUNC
189  * @tc.number: ReleaseLimiter_001
190  * @tc.desc  : Test ReleaseLimiter interface when limiter has been created.
191  */
192 HWTEST_F(AudioLimiterManagerUnitTest, ReleaseLimiter_001, TestSize.Level1)
193 {
194     EXPECT_NE(limiterManager, nullptr);
195 
196     int32_t sinkIndex = 0;
197     int32_t ret = limiterManager->CreateLimiter(sinkIndex);
198     EXPECT_EQ(ret, SUCCESS);
199     ret = limiterManager->ReleaseLimiter(sinkIndex);
200     EXPECT_EQ(ret, SUCCESS);
201 }
202 
203 /**
204  * @tc.name  : Test ReleaseLimiter API
205  * @tc.type  : FUNC
206  * @tc.number: ReleaseLimiter_002
207  * @tc.desc  : Test ReleaseLimiter interface when limiter has been created and released.
208  */
209 HWTEST_F(AudioLimiterManagerUnitTest, ReleaseLimiter_002, TestSize.Level1)
210 {
211     EXPECT_NE(limiterManager, nullptr);
212 
213     int32_t sinkIndex = 0;
214     int32_t ret = limiterManager->CreateLimiter(sinkIndex);
215     EXPECT_EQ(ret, SUCCESS);
216     ret = limiterManager->ReleaseLimiter(sinkIndex);
217     EXPECT_EQ(ret, SUCCESS);
218     ret = limiterManager->ReleaseLimiter(sinkIndex);
219     EXPECT_EQ(ret, ERROR);
220 }
221 
222 /**
223  * @tc.name  : Test GetLatency API
224  * @tc.type  : FUNC
225  * @tc.number: GetLatency_001
226  * @tc.desc  : Test GetLatency interface when limiter has not been created.
227  */
228 HWTEST_F(AudioLimiterManagerUnitTest, GetLatency_001, TestSize.Level1)
229 {
230     EXPECT_NE(limiterManager, nullptr);
231 
232     int32_t sinkIndex = 0;
233     uint32_t latency = limiterManager->GetLatency(sinkIndex);
234     EXPECT_EQ(latency, 0);
235 }
236 
237 /**
238  * @tc.name  : Test GetLatency API
239  * @tc.type  : FUNC
240  * @tc.number: GetLatency_002
241  * @tc.desc  : Test GetLatency interface when limiter has been created.
242  */
243 HWTEST_F(AudioLimiterManagerUnitTest, GetLatency_002, TestSize.Level1)
244 {
245     EXPECT_NE(limiterManager, nullptr);
246 
247     int32_t sinkIndex = 0;
248     int32_t ret = limiterManager->CreateLimiter(sinkIndex);
249     EXPECT_EQ(ret, SUCCESS);
250     ret = limiterManager->SetLimiterConfig(sinkIndex, TEST_MAX_REQUEST, SAMPLE_F32LE, SAMPLE_RATE_48000, STEREO);
251     EXPECT_EQ(ret, SUCCESS);
252     ret = limiterManager->GetLatency(sinkIndex);
253     EXPECT_EQ(ret, TEST_MAX_REQUEST * AUDIO_MS_PER_S/ (SAMPLE_F32LE * SAMPLE_RATE_48000 * STEREO * PROC_COUNT));
254 }
255 } // namespace AudioStandard
256 } // namespace OHOS