• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (C) 2025 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <audio_hdi_codec_inner_unit_test.h>
18 #include <gtest/gtest.h>
19 #include <atomic>
20 #include <fstream>
21 #include <thread>
22 #include <queue>
23 #include <string>
24 #include <hdi_codec.h>
25 #include "avcodec_common.h"
26 #include "avcodec_audio_decoder.h"
27 #include "avcodec_info.h"
28 #include "avcodec_codec_name.h"
29 
30 namespace {
31 using namespace::std;
32 using namespace testing::ext;
33 using namespace OHOS::Media;
34 using namespace OHOS::MediaAVCodec;
35 using namespace OHOS::Media::Plugins;
36 using namespace Hdi;
37 using namespace OHOS::HDI::Codec::V4_0;
38 
39 const string LBVC_DECODER_COMPONENT_NAME = "OMX.audio.decoder.lbvc";
40 constexpr uint32_t OMX_AUDIO_CODEC_PARAM_INDEX = 0x6F000000 + 0x00A0000B;
41 uint32_t g_bufferSize = 8192;
42 CapabilityData audioLbvcCapability;
43 
44 class AudioHdiCodecInnerUnitTest : public testing::Test {
45 public:
46     static void SetUpTestCase(void);
47     static void TearDownTestCase(void);
48     void SetUp();
49     void TearDown();
50     Status CheckInit();
51 protected:
52     std::shared_ptr<Hdi::HdiCodec> hdiCodec_;
53     std::shared_ptr<HdiCodecInner> hdiCodecInner_;
54     std::shared_ptr<CapabilityData> cap_;
55     std::unique_ptr<std::ifstream> soFile_;
56     std::shared_ptr<AVCodecAudioDecoder> audioDec_ = {nullptr};
57 };
58 
SetUpTestCase(void)59 void AudioHdiCodecInnerUnitTest::SetUpTestCase(void)
60 {
61     cout << "[SetUpTestCase]: " << endl;
62 }
63 
TearDownTestCase(void)64 void AudioHdiCodecInnerUnitTest::TearDownTestCase(void)
65 {
66     cout << "[TearDownTestCase]: " << endl;
67 }
68 
SetUp(void)69 void AudioHdiCodecInnerUnitTest::SetUp(void)
70 {
71     hdiCodecInner_ = std::make_shared<HdiCodecInner>();
72     cout << "[SetUp]: SetUp!!!" << endl;
73 }
74 
TearDown(void)75 void AudioHdiCodecInnerUnitTest::TearDown(void)
76 {
77     hdiCodecInner_->Release();
78     if (audioDec_) {
79         audioDec_->Release();
80     }
81     cout << "[TearDown]: over!!!" << endl;
82 }
83 
CheckInit()84 Status AudioHdiCodecInnerUnitTest::CheckInit()
85 {
86     std::shared_ptr<Hdi::HdiCodec> codec = std::make_shared<HdiCodec>();
87     Status ret = codec->InitComponent(LBVC_DECODER_COMPONENT_NAME);
88     codec->Release();
89     codec = nullptr;
90     return ret;
91 }
92 
93 /**
94  * @tc.name: InitComponent_001
95  * @tc.desc: ret == HDF_SUCCESS && compNode_ == nullptr
96  * @tc.type: FUNC
97  */
98 HWTEST_F(AudioHdiCodecInnerUnitTest, InitComponent_001, TestSize.Level1)
99 {
100     if (CheckInit() != Status::OK) {
101         return;
102     }
103     auto& compNode = hdiCodecInner_->GetCompNode();
104     compNode = nullptr;
105     Status ret = hdiCodecInner_->HdiCodec::InitComponent(LBVC_DECODER_COMPONENT_NAME);
106     EXPECT_EQ(Status::OK, ret);
107     EXPECT_NE(compNode, nullptr);
108     auto& compCb = hdiCodecInner_->GetCompCb();
109     EXPECT_NE(compCb, nullptr);
110 }
111 
112 /**
113  * @tc.name: InitComponent_002
114  * @tc.desc: ret != HDF_SUCCESS && compNode_ == nullptr
115  * @tc.type: FUNC
116  */
117 HWTEST_F(AudioHdiCodecInnerUnitTest, InitComponent_002, TestSize.Level1)
118 {
119     if (CheckInit() != Status::OK) {
120         return;
121     }
122     auto& compNode = hdiCodecInner_->GetCompNode();
123     compNode = nullptr;
124     Status ret = hdiCodecInner_->HdiCodec::InitComponent("INVALID_COMPONENT_NAME");
125     EXPECT_NE(Status::OK, ret);
126     EXPECT_EQ(compNode, nullptr);
127     auto& compCb = hdiCodecInner_->GetCompCb();
128     EXPECT_EQ(compCb, nullptr);
129 }
130 
131 /**
132  * @tc.name: InitComponent_003
133  * @tc.desc: ret == HDF_SUCCESS && compNode_ != nullptr
134  * @tc.type: FUNC
135  */
136 HWTEST_F(AudioHdiCodecInnerUnitTest, InitComponent_003, TestSize.Level1)
137 {
138     if (CheckInit() != Status::OK) {
139         return;
140     }
141     auto& compNode = hdiCodecInner_->GetCompNode();
142     Status ret = hdiCodecInner_->HdiCodec::InitComponent(LBVC_DECODER_COMPONENT_NAME);
143     EXPECT_EQ(Status::OK, ret);
144     AudioCodecOmxParam param;
145     hdiCodecInner_->InitParameter(param);
146     param.sampleRate = static_cast<uint32_t>(16000); // sampleRate
147     param.sampleFormat = static_cast<uint32_t>(1); // SAMPLE_S16LE
148     param.channels = static_cast<uint32_t>(1); // channel num
149     int8_t *p = reinterpret_cast<int8_t *>(&param);
150     std::vector<int8_t> paramVec(p, p + sizeof(AudioCodecOmxParam));
151     ret = hdiCodecInner_->SetParameter(OMX_AUDIO_CODEC_PARAM_INDEX, paramVec);
152     EXPECT_EQ(Status::OK, ret);
153     EXPECT_NE(compNode, nullptr);
154     auto& compCb = hdiCodecInner_->GetCompCb();
155     EXPECT_NE(compCb, nullptr);
156 }
157 
158 /**
159  * @tc.name: IsSupportCodecType_001
160  * @tc.desc: compMgr_ == nullptr
161  * @tc.type: FUNC
162  */
163 HWTEST_F(AudioHdiCodecInnerUnitTest, IsSupportCodecType_001, TestSize.Level1)
164 {
165     if (CheckInit() != Status::OK) {
166         return;
167     }
168     auto& compMgr = hdiCodecInner_->GetCompMgr();
169     compMgr = nullptr;
170     bool ret = hdiCodecInner_->IsSupportCodecType("OMX.audio.decoder.lbvc", &audioLbvcCapability);
171     EXPECT_EQ(true, ret);
172 }
173 
174 /**
175  * @tc.name: IsSupportCodecType_002
176  * @tc.desc: compMgr_ != nullptr
177  * @tc.type: FUNC
178  */
179 HWTEST_F(AudioHdiCodecInnerUnitTest, IsSupportCodecType_002, TestSize.Level1)
180 {
181     if (CheckInit() != Status::OK) {
182         return;
183     }
184     auto& compMgr = hdiCodecInner_->GetCompMgr();
185     compMgr = nullptr;
186     hdiCodecInner_->IsSupportCodecType("OMX.audio.decoder.lbvc", &audioLbvcCapability); // GetComponentManager called
187     bool ret = hdiCodecInner_->IsSupportCodecType("OMX.audio.decoder.lbvc", &audioLbvcCapability);
188     EXPECT_EQ(true, ret);
189 }
190 
191 /**
192  * @tc.name: InitBuffersByPort_001
193  * @tc.desc: portIndex equals to PortIndex::INPUT_PORT or PortIndex::OUTPUT_PORT
194  * @tc.type: FUNC
195  */
196 HWTEST_F(AudioHdiCodecInnerUnitTest, InitBuffersByPort_001, TestSize.Level1)
197 {
198     if (CheckInit() != Status::OK) {
199         return;
200     }
201     hdiCodecInner_->InitComponent(LBVC_DECODER_COMPONENT_NAME);
202     Status ret = hdiCodecInner_->InitBuffers(g_bufferSize);
203     EXPECT_EQ(Status::OK, ret);
204 }
205 
206 /**
207  * @tc.name: FreeBuffer_001
208  * @tc.desc: omxBuffer != nullptr
209  * @tc.type: FUNC
210  */
211 HWTEST_F(AudioHdiCodecInnerUnitTest, FreeBuffer_001, TestSize.Level1)
212 {
213     if (CheckInit() != Status::OK) {
214         return;
215     }
216     hdiCodecInner_->InitComponent(LBVC_DECODER_COMPONENT_NAME);
217     Status ret = hdiCodecInner_->InitBuffers(g_bufferSize);
218     EXPECT_EQ(Status::OK, ret);
219     ret = hdiCodecInner_->Reset();
220     EXPECT_EQ(Status::OK, ret);
221 }
222 
223 /**
224  * @tc.name: FreeBuffer_002
225  * @tc.desc: omxBuffer == nullptr
226  * @tc.type: FUNC
227  */
228 HWTEST_F(AudioHdiCodecInnerUnitTest, FreeBuffer_002, TestSize.Level1)
229 {
230     if (CheckInit() != Status::OK) {
231         return;
232     }
233     hdiCodecInner_->InitComponent(LBVC_DECODER_COMPONENT_NAME);
234     Status ret = hdiCodecInner_->Reset();
235     EXPECT_EQ(Status::OK, ret);
236 }
237 
238 /**
239  * @tc.name: Release_001
240  * @tc.desc: compMgr_ != nullptr && componentId_ > 0
241  * @tc.type: FUNC
242  */
243 HWTEST_F(AudioHdiCodecInnerUnitTest, Release_001, TestSize.Level1)
244 {
245     if (CheckInit() != Status::OK) {
246         return;
247     }
248     auto& compMgr = hdiCodecInner_->GetCompMgr();
249     compMgr = nullptr;
250     hdiCodecInner_->IsSupportCodecType("OMX.audio.decoder.lbvc", &audioLbvcCapability); // GetComponentManager called
251     hdiCodecInner_->InitComponent(LBVC_DECODER_COMPONENT_NAME); // get componentId_
252     auto& componentId = hdiCodecInner_->GetCompId();
253     EXPECT_GT(componentId, 0);
254     hdiCodecInner_->Release();
255     EXPECT_EQ(compMgr, nullptr);
256 }
257 
258 /**
259  * @tc.name: Release_002
260  * @tc.desc: compMgr_ == nullptr && componentId_ > 0
261  * @tc.type: FUNC
262  */
263 HWTEST_F(AudioHdiCodecInnerUnitTest, Release_002, TestSize.Level1)
264 {
265     if (CheckInit() != Status::OK) {
266         return;
267     }
268     auto& compMgr = hdiCodecInner_->GetCompMgr();
269     compMgr = nullptr;
270     hdiCodecInner_->InitComponent(LBVC_DECODER_COMPONENT_NAME); // get componentId_
271     auto& componentId = hdiCodecInner_->GetCompId();
272     EXPECT_GT(componentId, 0);
273     hdiCodecInner_->Release();
274     EXPECT_EQ(compMgr, nullptr);
275 }
276 
277 /**
278  * @tc.name: Release_003
279  * @tc.desc: compMgr_ != nullptr && componentId_ <= 0
280  * @tc.type: FUNC
281  */
282 HWTEST_F(AudioHdiCodecInnerUnitTest, Release_003, TestSize.Level1)
283 {
284     if (CheckInit() != Status::OK) {
285         return;
286     }
287     auto& compMgr = hdiCodecInner_->GetCompMgr();
288     compMgr = nullptr;
289     hdiCodecInner_->IsSupportCodecType("OMX.audio.decoder.lbvc", &audioLbvcCapability); // GetComponentManager called
290     auto& componentId = hdiCodecInner_->GetCompId();
291     EXPECT_LE(componentId, 0);
292     hdiCodecInner_->Release();
293     EXPECT_EQ(compMgr, nullptr);
294 }
295 
296 /**
297  * @tc.name: Release_004
298  * @tc.desc: compMgr_ == nullptr && componentId_ <= 0
299  * @tc.type: FUNC
300  */
301 HWTEST_F(AudioHdiCodecInnerUnitTest, Release_004, TestSize.Level1)
302 {
303     if (CheckInit() != Status::OK) {
304         return;
305     }
306     auto& compMgr = hdiCodecInner_->GetCompMgr();
307     compMgr = nullptr;
308     auto& componentId = hdiCodecInner_->GetCompId();
309     EXPECT_LE(componentId, 0);
310     hdiCodecInner_->Release();
311     EXPECT_EQ(compMgr, nullptr);
312 }
313 
314 /**
315  * @tc.name: Release_005
316  * @tc.desc: compCb_ == nullptr
317  * @tc.type: FUNC
318  */
319 HWTEST_F(AudioHdiCodecInnerUnitTest, Release_005, TestSize.Level1)
320 {
321     if (CheckInit() != Status::OK) {
322         return;
323     }
324     auto& compMgr = hdiCodecInner_->GetCompMgr();
325     compMgr = nullptr;
326     hdiCodecInner_->IsSupportCodecType("OMX.audio.decoder.lbvc", &audioLbvcCapability); // GetComponentManager called
327     hdiCodecInner_->InitComponent(LBVC_DECODER_COMPONENT_NAME); // get componentId_
328     auto& componentId = hdiCodecInner_->GetCompId();
329     EXPECT_GT(componentId, 0);
330     auto& compCb = hdiCodecInner_->GetCompCb();
331     compCb = nullptr;
332     hdiCodecInner_->Release();
333     EXPECT_EQ(compMgr, nullptr);
334 }
335 
336 /**
337  * @tc.name: Release_006
338  * @tc.desc: compCb_ != nullptr
339  * @tc.type: FUNC
340  */
341 HWTEST_F(AudioHdiCodecInnerUnitTest, Release_006, TestSize.Level1)
342 {
343     if (CheckInit() != Status::OK) {
344         return;
345     }
346     auto& compMgr = hdiCodecInner_->GetCompMgr();
347     compMgr = nullptr;
348     hdiCodecInner_->IsSupportCodecType("OMX.audio.decoder.lbvc", &audioLbvcCapability); // GetComponentManager called
349     hdiCodecInner_->InitComponent(LBVC_DECODER_COMPONENT_NAME); // get componentId_
350     auto& componentId = hdiCodecInner_->GetCompId();
351     EXPECT_GT(componentId, 0);
352     auto& compCb = hdiCodecInner_->GetCompCb();
353     EXPECT_NE(compCb, nullptr);
354     hdiCodecInner_->Release();
355     EXPECT_EQ(compMgr, nullptr);
356 }
357 
358 /**
359  * @tc.name: EventHandler_001
360  * @tc.desc: hdiCodec_ == nullptr
361  * @tc.type: FUNC
362  */
363 HWTEST_F(AudioHdiCodecInnerUnitTest, EventHandler_001, TestSize.Level1)
364 {
365     if (CheckInit() != Status::OK) {
366         return;
367     }
368     auto hdiCodec = std::make_shared<HdiCodec>();
369     auto hdiCodecCb = std::make_shared<HdiCodec::HdiCallback>(hdiCodec);
370     auto& hdiCodec_ = hdiCodecCb->HdiCallback::GetHdiCodec();
371     hdiCodec_ = nullptr;
372     auto& event = hdiCodecInner_->GetCompEvent();
373     const EventInfo info = {};
374     int32_t ret =hdiCodecCb->EventHandler(event, info);
375     EXPECT_EQ(Status::OK, static_cast<Status>(ret));
376 }
377 
378 /**
379  * @tc.name: EventHandler_002
380  * @tc.desc: hdiCodec_ != nullptr
381  * @tc.type: FUNC
382  */
383 HWTEST_F(AudioHdiCodecInnerUnitTest, EventHandler_002, TestSize.Level1)
384 {
385     if (CheckInit() != Status::OK) {
386         return;
387     }
388     auto hdiCodec = std::make_shared<HdiCodec>();
389     auto hdiCodecCb = std::make_shared<HdiCodec::HdiCallback>(hdiCodec);
390     auto& hdiCodec_ = hdiCodecCb->HdiCallback::GetHdiCodec();
391     EXPECT_NE(hdiCodec_, nullptr);
392     auto& event = hdiCodecInner_->GetCompEvent();
393     const EventInfo info = {};
394     int32_t ret =hdiCodecCb->EventHandler(event, info);
395     EXPECT_EQ(Status::OK, static_cast<Status>(ret));
396 }
397 
398 /**
399  * @tc.name: EmptyBufferDone_001
400  * @tc.desc: hdiCodec_ != nullptr
401  * @tc.type: FUNC
402  */
403 HWTEST_F(AudioHdiCodecInnerUnitTest, EmptyBufferDone_001, TestSize.Level1)
404 {
405     if (CheckInit() != Status::OK) {
406         return;
407     }
408     auto hdiCodec = std::make_shared<HdiCodec>();
409     auto hdiCodecCb = std::make_shared<HdiCodec::HdiCallback>(hdiCodec);
410     auto& hdiCodec_ = hdiCodecCb->HdiCallback::GetHdiCodec();
411     EXPECT_NE(hdiCodec_, nullptr);
412     int64_t appData = 0;
413     const OmxCodecBuffer& buffer = {};
414     int32_t ret =hdiCodecCb->EmptyBufferDone(appData, buffer);
415     EXPECT_EQ(Status::OK, static_cast<Status>(ret));
416 }
417 
418 /**
419  * @tc.name: EmptyBufferDone_002
420  * @tc.desc: hdiCodec_ == nullptr
421  * @tc.type: FUNC
422  */
423 HWTEST_F(AudioHdiCodecInnerUnitTest, EmptyBufferDone_002, TestSize.Level1)
424 {
425     if (CheckInit() != Status::OK) {
426         return;
427     }
428     auto hdiCodec = std::make_shared<HdiCodec>();
429     auto hdiCodecCb = std::make_shared<HdiCodec::HdiCallback>(hdiCodec);
430     auto& hdiCodec_ = hdiCodecCb->HdiCallback::GetHdiCodec();
431     hdiCodec_ = nullptr;
432     int64_t appData = 0;
433     const OmxCodecBuffer& buffer = {};
434     int32_t ret =hdiCodecCb->EmptyBufferDone(appData, buffer);
435     EXPECT_EQ(Status::OK, static_cast<Status>(ret));
436 }
437 
438 /**
439  * @tc.name: FillBufferDone_001
440  * @tc.desc: hdiCodec_ != nullptr
441  * @tc.type: FUNC
442  */
443 HWTEST_F(AudioHdiCodecInnerUnitTest, FillBufferDone_001, TestSize.Level1)
444 {
445     if (CheckInit() != Status::OK) {
446         return;
447     }
448     auto hdiCodec = std::make_shared<HdiCodec>();
449     auto hdiCodecCb = std::make_shared<HdiCodec::HdiCallback>(hdiCodec);
450     auto& hdiCodec_ = hdiCodecCb->HdiCallback::GetHdiCodec();
451     EXPECT_NE(hdiCodec_, nullptr);
452     int64_t appData = 0;
453     const OmxCodecBuffer& buffer = {};
454     int32_t ret =hdiCodecCb->FillBufferDone(appData, buffer);
455     EXPECT_EQ(Status::OK, static_cast<Status>(ret));
456 }
457 
458 /**
459  * @tc.name: FillBufferDone_002
460  * @tc.desc: hdiCodec_ == nullptr
461  * @tc.type: FUNC
462  */
463 HWTEST_F(AudioHdiCodecInnerUnitTest, FillBufferDone_002, TestSize.Level1)
464 {
465     if (CheckInit() != Status::OK) {
466         return;
467     }
468     auto hdiCodec = std::make_shared<HdiCodec>();
469     auto hdiCodecCb = std::make_shared<HdiCodec::HdiCallback>(hdiCodec);
470     auto& hdiCodec_ = hdiCodecCb->HdiCallback::GetHdiCodec();
471     hdiCodec_ = nullptr;
472     int64_t appData = 0;
473     const OmxCodecBuffer& buffer = {};
474     int32_t ret =hdiCodecCb->FillBufferDone(appData, buffer);
475     EXPECT_EQ(Status::OK, static_cast<Status>(ret));
476 }
477 }