1 /*
2  * Copyright (c) 2024 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 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 <hdf_log.h>
18 #include <securec.h>
19 #include <servmgr_hdi.h>
20 #include <vector>
21 #include "codec_function_utils.h"
22 #include "v3_0/codec_callback_service.h"
23 
24 #define ERR_COUNT (-1)
25 #define ERR_COUNT_2 (10000)
26 
27 using namespace std;
28 using namespace testing::ext;
29 using OHOS::sptr;
30 using OHOS::HDI::Base::NativeBuffer;
31 using namespace OHOS::HDI::Codec::V3_0;
32 using namespace OHOS::HDI::Display::Buffer::V1_0;
33 using namespace OHOS::HDI::Display::Composer::V1_0;
34 
35 namespace {
36 constexpr CodecType TYPE = CodecType::VIDEO_DECODER;
37 constexpr AvCodecRole ROLE = MEDIA_ROLETYPE_VIDEO_AVC;
38 static sptr<ICodecComponent> g_component = nullptr;
39 static sptr<ICodecCallback> g_callback = nullptr;
40 static sptr<ICodecComponentManager> g_manager = nullptr;
41 static OHOS::HDI::Codec::V3_0::CodecVersionType g_version;
42 static std::string g_compName = "";
43 
44 class CodecHdiOmxDecTest : public testing::Test {
45 public:
SetUpTestCase()46     static void SetUpTestCase()
47     {
48         g_manager = ICodecComponentManager::Get();
49         int32_t count = 0;
50         auto ret = g_manager->GetComponentNum(count);
51         ASSERT_EQ(ret, HDF_SUCCESS);
52         if (count <= 0) {
53             return;
54         }
55 
56         std::vector<CodecCompCapability> capList;
57         auto err = g_manager->GetComponentCapabilityList(capList, count);
58         ASSERT_TRUE(err == HDF_SUCCESS);
59         for (auto cap : capList) {
60             if (cap.type == TYPE && cap.role == ROLE) {
61                 g_compName = cap.compName;
62                 break;
63             }
64         }
65     }
66 
TearDownTestCase()67     static void TearDownTestCase()
68     {
69         g_manager = nullptr;
70     }
71 
SetUp()72     void SetUp()
73     {
74         ASSERT_TRUE(g_manager != nullptr && !g_compName.empty());
75         g_callback = new CodecCallbackService();
76         ASSERT_TRUE(g_callback != nullptr);
77         auto ret = g_manager->CreateComponent(g_component, componentId_, g_compName.data(), APP_DATA, g_callback);
78         ASSERT_EQ(ret, HDF_SUCCESS);
79 
80         struct CompVerInfo verInfo;
81         ret = g_component->GetComponentVersion(verInfo);
82         ASSERT_EQ(ret, HDF_SUCCESS);
83         g_version = verInfo.compVersion;
84 
85         func_ = new FunctionUtil(g_version);
86         ASSERT_TRUE(func_ != nullptr);
87     }
88 
TearDown()89     void TearDown()
90     {
91         std::vector<int8_t> cmdData;
92         if (g_component != nullptr) {
93             g_component->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_LOADED, cmdData);
94         }
95         if (g_manager != nullptr && g_component != nullptr) {
96             g_manager->DestroyComponent(componentId_);
97         }
98         g_component = nullptr;
99         g_callback = nullptr;
100         func_ = nullptr;
101     }
102 
103 public:
104     uint32_t componentId_ = 0;
105     sptr<FunctionUtil> func_ = nullptr;
106     const static uint32_t outputIndex = static_cast<uint32_t>(PortIndex::INDEX_OUTPUT);
107 };
108 
109 /**
110 * @tc.number : SUB_Driver_Codec_idlomx_0700
111 * @tc.name   : HdfCodecHdiEmptyAndFillBufferTest_001
112 * @tc.desc   : Verify the codec EmptyAndFillBuffer process.
113   @tc.type: FUNC
114 */
HWTEST_F(CodecHdiOmxDecTest, HdfCodecHdiEmptyAndFillBufferTest_001, TestSize.Level1)115 HWTEST_F(CodecHdiOmxDecTest, HdfCodecHdiEmptyAndFillBufferTest_001, TestSize.Level1)
116 {
117     ASSERT_TRUE(g_component != nullptr);
118     std::vector<int8_t> cmdData;
119     auto ret = g_component->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_IDLE, cmdData);
120     ASSERT_EQ(ret, HDF_SUCCESS);
121 
122     std::vector<int8_t> inParam, outParam;
123     ret = g_component->GetParameter(OMX_IndexMax, inParam, outParam);
124     ASSERT_TRUE(ret != HDF_SUCCESS);
125     OMX_PARAM_PORTDEFINITIONTYPE param;
126     func_->GetPortParameter(g_component, PortIndex::INDEX_INPUT, param);
127     auto err = func_->UseBufferOnPort(g_component, PortIndex::INDEX_INPUT, param.nBufferCountActual,
128         param.nBufferSize);
129     ASSERT_TRUE(err);
130 
131     err = func_->InitBufferHandleParameter(g_component, param, outputIndex, CODEC_BUFFER_TYPE_HANDLE);
132     ASSERT_TRUE(err);
133     ret = func_->GetPortParameter(g_component, PortIndex::INDEX_OUTPUT, param);
134     ASSERT_EQ(ret, HDF_SUCCESS);
135     err = func_->UseHandleBuffer(g_component, PortIndex::INDEX_OUTPUT, param.nBufferCountActual, param.nBufferSize);
136     ASSERT_TRUE(err);
137 
138     ret = g_component->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_EXECUTING, cmdData);
139     ASSERT_EQ(ret, HDF_SUCCESS);
140     err = func_->WaitState(g_component, CODEC_STATE_EXECUTING);
141     ASSERT_TRUE(err);
142 
143     func_->FillAndEmptyAllBuffer(g_component, CODEC_BUFFER_TYPE_HANDLE);
144     ret = g_component->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_IDLE, cmdData);
145     ASSERT_EQ(ret, HDF_SUCCESS);
146     err = func_->WaitState(g_component, CODEC_STATE_IDLE);
147     ASSERT_TRUE(err);
148     ret = g_component->SendCommand(CODEC_COMMAND_STATE_SET, CODEC_STATE_LOADED, cmdData);
149     ASSERT_EQ(ret, HDF_SUCCESS);
150     err = func_->WaitState(g_component, CODEC_STATE_IDLE);
151     ASSERT_TRUE(err);
152 
153     err = func_->FreeBufferOnPort(g_component, PortIndex::INDEX_INPUT);
154     ASSERT_TRUE(err);
155     err = func_->FreeBufferOnPort(g_component, PortIndex::INDEX_OUTPUT);
156     ASSERT_TRUE(err);
157     err = func_->WaitState(g_component, CODEC_STATE_LOADED);
158     ASSERT_TRUE(err);
159 }
160 
161 /**
162 * @tc.number : SUB_Driver_Codec_idlomx_0800
163 * @tc.name   : HdfCodecHdiGetComponentCapabilityListTest_001
164 * @tc.desc   : Verify the GetComponentCapabilityList function when the input parameter is ERR_COUNT.
165   @tc.type: FUNC
166 */
HWTEST_F(CodecHdiOmxDecTest, HdfCodecHdiGetComponentCapabilityListTest_001, TestSize.Level1)167 HWTEST_F(CodecHdiOmxDecTest, HdfCodecHdiGetComponentCapabilityListTest_001, TestSize.Level1)
168 {
169     ASSERT_TRUE(g_component != nullptr);
170     int32_t count = ERR_COUNT_2;
171     std::vector<CodecCompCapability> capList;
172     auto err = g_manager->GetComponentCapabilityList(capList, count);
173     count = ERR_COUNT;
174     err = g_manager->GetComponentCapabilityList(capList, count);
175     ASSERT_TRUE(err != HDF_SUCCESS);
176 }
177 }  // namespace