• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 <cstdlib>
17 #include <cstdio>
18 #include <ctime>
19 #include <dlfcn.h>
20 #include <fstream>
21 #include <memory>
22 #include <string>
23 
24 #include <gtest/gtest.h>
25 
26 #include "algorithm_common.h"
27 #include "algorithm_errors.h"
28 
29 #include "detail_enhancer_video_impl.h"
30 #include "detail_enhancer_video.h"
31 #include "surface/window.h"
32 #include "external_window.h"
33 
34 constexpr int64_t NANOS_IN_SECOND = 1000000000L;
35 constexpr int64_t NANOS_IN_MICRO = 1000L;
36 constexpr uint32_t DEFAULT_WIDTH = 1920;
37 constexpr uint32_t DEFAULT_HEIGHT = 1080;
38 
39 using namespace std;
40 using namespace testing::ext;
41 
42 namespace OHOS {
43 namespace Media {
44 namespace VideoProcessingEngine {
45 
46 class DetailEnhancerVideoCallbackImpl : public DetailEnhancerVideoCallback {
47 public:
48     DetailEnhancerVideoCallbackImpl() = default;
49     ~DetailEnhancerVideoCallbackImpl() override = default;
50     DetailEnhancerVideoCallbackImpl(const DetailEnhancerVideoCallbackImpl&) = delete;
51     DetailEnhancerVideoCallbackImpl& operator=(const DetailEnhancerVideoCallbackImpl&) = delete;
52     DetailEnhancerVideoCallbackImpl(DetailEnhancerVideoCallbackImpl&&) = delete;
53     DetailEnhancerVideoCallbackImpl& operator=(DetailEnhancerVideoCallbackImpl&&) = delete;
54 
55     void OnError(VPEAlgoErrCode errorCode) override;
56     void OnState(VPEAlgoState state) override;
57     void OnOutputBufferAvailable(uint32_t index, DetailEnhBufferFlag flag) override;
58 };
OnOutputBufferAvailable(uint32_t index,DetailEnhBufferFlag flag)59 void DetailEnhancerVideoCallbackImpl::OnOutputBufferAvailable(uint32_t index, DetailEnhBufferFlag flag)
60 {
61     switch (flag) {
62         case DETAIL_ENH_BUFFER_FLAG_NONE:
63             std::cout << "OnOutputBufferAvailable: normal" << std::endl;
64             break;
65         case DETAIL_ENH_BUFFER_FLAG_EOS:
66             std::cout << "OnOutputBufferAvailable: end of stream" << std::endl;
67             break;
68         default:
69             std::cout << "OnOutputBufferAvailable: unknown" << std::endl;
70             break;
71     }
72 }
OnError(VPEAlgoErrCode errorCode)73 void DetailEnhancerVideoCallbackImpl::OnError(VPEAlgoErrCode errorCode)
74 {
75     (void)errorCode;
76 }
OnState(VPEAlgoState state)77 void DetailEnhancerVideoCallbackImpl::OnState(VPEAlgoState state)
78 {
79     (void)state;
80 }
81 
82 class DetailEnhancerVideoUnitTest : public testing::Test {
83 public:
84     static void SetUpTestCase(void);
85     static void TearDownTestCase(void);
86     void SetUp();
87     void TearDown();
88     sptr<Surface> surface;
89     OHNativeWindow *nativeWindow;
90     uint32_t FlushSurf(OHNativeWindowBuffer *ohNativeWindowBuffer);
91 };
92 
SetUpTestCase(void)93 void DetailEnhancerVideoUnitTest::SetUpTestCase(void)
94 {
95     cout << "[SetUpTestCase]: " << endl;
96 }
97 
TearDownTestCase(void)98 void DetailEnhancerVideoUnitTest::TearDownTestCase(void)
99 {
100     cout << "[TearDownTestCase]: " << endl;
101 }
102 
SetUp(void)103 void DetailEnhancerVideoUnitTest::SetUp(void)
104 {
105     cout << "[SetUp]: SetUp!!!" << endl;
106 }
107 
TearDown(void)108 void DetailEnhancerVideoUnitTest::TearDown(void)
109 {
110     cout << "[TearDown]: over!!!" << endl;
111 }
112 
GetSystemTime()113 int64_t GetSystemTime()
114 {
115     struct timespec now;
116     (void)clock_gettime(CLOCK_BOOTTIME, &now);
117     int64_t nanoTime = reinterpret_cast<int64_t>(now.tv_sec) * NANOS_IN_SECOND + now.tv_nsec;
118 
119     return nanoTime / NANOS_IN_MICRO;
120 }
121 
FlushSurf(OHNativeWindowBuffer * ohNativeWindowBuffer)122 uint32_t DetailEnhancerVideoUnitTest::FlushSurf(OHNativeWindowBuffer *ohNativeWindowBuffer)
123 {
124     struct Region region;
125     struct Region::Rect *rect = new Region::Rect();
126     rect->x = 0;
127     rect->y = 0;
128     rect->w = DEFAULT_WIDTH;
129     rect->h = DEFAULT_HEIGHT;
130     region.rects = rect;
131     NativeWindowHandleOpt(nativeWindow, SET_UI_TIMESTAMP, GetSystemTime());
132     int32_t err = OH_NativeWindow_NativeWindowFlushBuffer(nativeWindow, ohNativeWindowBuffer, -1, region);
133     delete rect;
134     if (err != 0) {
135         cout << "FlushBuffer failed" << endl;
136         return 1;
137     }
138     return 0;
139 }
140 
141 // detail enhancer init
142 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_init_01, TestSize.Level1)
143 {
144     auto detailEnhVideo = DetailEnhancerVideo::Create();
145     EXPECT_NE(detailEnhVideo, nullptr);
146     detailEnhVideo->Release();
147 }
148 
149 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_02, TestSize.Level1)
150 {
151     auto detailEnhVideo = DetailEnhancerVideo::Create();
152     std::shared_ptr<DetailEnhancerVideoCallback> cb = nullptr;
153     auto ret = detailEnhVideo->RegisterCallback(cb);
154     EXPECT_NE(ret, VPE_ALGO_ERR_OK);
155     detailEnhVideo->Release();
156 }
157 
158 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_03, TestSize.Level1)
159 {
160     auto detailEnhVideo = DetailEnhancerVideo::Create();
161     DetailEnhancerParameters param {
162         .uri = "",
163         .level = DETAIL_ENH_LEVEL_HIGH,
164     };
165     auto ret = detailEnhVideo->SetParameter(param, VIDEO);
166     EXPECT_EQ(ret, VPE_ALGO_ERR_OK);
167     detailEnhVideo->Release();
168 }
169 
170 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_04, TestSize.Level1)
171 {
172     auto detailEnhVideo = DetailEnhancerVideo::Create();
173     DetailEnhancerParameters param {
174         .uri = "",
175         .level = DETAIL_ENH_LEVEL_LOW,
176     };
177     auto ret = detailEnhVideo->SetParameter(param, VIDEO);
178     EXPECT_EQ(ret, VPE_ALGO_ERR_OK);
179     detailEnhVideo->Release();
180 }
181 
182 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_05, TestSize.Level1)
183 {
184     auto detailEnhVideo = DetailEnhancerVideo::Create();
185     DetailEnhancerParameters param {
186         .uri = "",
187         .level = DETAIL_ENH_LEVEL_LOW,
188     };
189     auto ret = detailEnhVideo->SetParameter(param, VIDEO);
190     EXPECT_EQ(ret, VPE_ALGO_ERR_OK);
191     detailEnhVideo->Release();
192 }
193 
194 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_06, TestSize.Level1)
195 {
196     auto detailEnhVideo = DetailEnhancerVideo::Create();
197     auto ret = detailEnhVideo->GetInputSurface();
198     EXPECT_NE(ret, nullptr);
199     detailEnhVideo->Release();
200 }
201 
202 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_07, TestSize.Level1)
203 {
204     auto detailEnhVideo = DetailEnhancerVideo::Create();
205     auto ret = detailEnhVideo->GetInputSurface();
206     ret = detailEnhVideo->GetInputSurface();
207     EXPECT_EQ(ret, nullptr);
208     detailEnhVideo->Release();
209 }
210 
211 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_08, TestSize.Level1)
212 {
213     auto detailEnhVideo = DetailEnhancerVideo::Create();
214     sptr<Surface> surface = nullptr;
215     auto ret = detailEnhVideo->SetOutputSurface(surface);
216     EXPECT_NE(ret, VPE_ALGO_ERR_OK);
217     detailEnhVideo->Release();
218 }
219 
220 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_09, TestSize.Level1)
221 {
222     auto detailEnhVideo = DetailEnhancerVideo::Create();
223 
224     auto ret = detailEnhVideo->RenderOutputBuffer(0);
225     EXPECT_EQ(ret, VPE_ALGO_ERR_OK);
226     detailEnhVideo->Release();
227 }
228 
229 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_10, TestSize.Level1)
230 {
231     auto detailEnhVideo = DetailEnhancerVideo::Create();
232     auto ret = detailEnhVideo->NotifyEos();
233     EXPECT_NE(ret, VPE_ALGO_ERR_OK);
234     detailEnhVideo->Release();
235 }
236 
237 // set parameter to midium
238 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_11, TestSize.Level1)
239 {
240     auto detailEnhVideo = DetailEnhancerVideo::Create();
241     DetailEnhancerParameters param {
242         .uri = "",
243         .level = DETAIL_ENH_LEVEL_MEDIUM,
244     };
245     auto res = detailEnhVideo->SetParameter(param, VIDEO);
246     EXPECT_EQ(res, VPE_ALGO_ERR_OK);
247     detailEnhVideo->Release();
248 }
249 
250 // set parameter to low
251 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_12, TestSize.Level1)
252 {
253     auto detailEnhVideo = DetailEnhancerVideo::Create();
254     DetailEnhancerParameters param {
255         .uri = "",
256         .level = DETAIL_ENH_LEVEL_LOW,
257     };
258     auto res = detailEnhVideo->SetParameter(param, VIDEO);
259     EXPECT_EQ(res, VPE_ALGO_ERR_OK);
260     detailEnhVideo->Release();
261 }
262 
263 // set parameter to none
264 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_13, TestSize.Level1)
265 {
266     auto detailEnhVideo = DetailEnhancerVideo::Create();
267     DetailEnhancerParameters param {
268         .uri = "",
269         .level = DETAIL_ENH_LEVEL_NONE,
270     };
271     auto res = detailEnhVideo->SetParameter(param, VIDEO);
272     EXPECT_EQ(res, VPE_ALGO_ERR_OK);
273     detailEnhVideo->Release();
274 }
275 
276 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_14, TestSize.Level1)
277 {
278     auto detailEnhVideo = DetailEnhancerVideo::Create();
279     DetailEnhancerParameters param {
280         .uri = "",
281         .level = DETAIL_ENH_LEVEL_HIGH,
282     };
283     auto res = detailEnhVideo->SetParameter(param, VIDEO);
284     res = detailEnhVideo->SetParameter(param, VIDEO);
285     EXPECT_EQ(res, VPE_ALGO_ERR_OK);
286     detailEnhVideo->Release();
287 }
288 
289 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_15, TestSize.Level1)
290 {
291     auto detailEnhVideo = DetailEnhancerVideo::Create();
292     std::shared_ptr<DetailEnhancerVideoCallback> cb = std::make_shared<DetailEnhancerVideoCallbackImpl>();
293     auto res = detailEnhVideo->RegisterCallback(cb);
294     EXPECT_EQ(res, VPE_ALGO_ERR_OK);
295     detailEnhVideo->Release();
296 }
297 
298 
299 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_16, TestSize.Level1)
300 {
301     auto detailEnhVideo = DetailEnhancerVideo::Create();
302     std::shared_ptr<DetailEnhancerVideoCallback> cb = std::make_shared<DetailEnhancerVideoCallbackImpl>();
303     auto res = detailEnhVideo->RegisterCallback(cb);
304     DetailEnhancerParameters param {
305         .uri = "",
306         .level = DETAIL_ENH_LEVEL_HIGH,
307     };
308     res = detailEnhVideo->SetParameter(param, VIDEO);
309     auto ret = detailEnhVideo->GetInputSurface();
310     res = detailEnhVideo->Start();
311     EXPECT_NE(res, VPE_ALGO_ERR_OK);
312     detailEnhVideo->Release();
313 }
314 
315 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_17, TestSize.Level1)
316 {
317     auto detailEnhVideo = DetailEnhancerVideo::Create();
318     auto res = detailEnhVideo->Stop();
319     EXPECT_NE(res, VPE_ALGO_ERR_OK);
320     detailEnhVideo->Release();
321 }
322 
323 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_18, TestSize.Level1)
324 {
325     auto detailEnhVideo = DetailEnhancerVideo::Create();
326     std::shared_ptr<DetailEnhancerVideoCallback> cb = std::make_shared<DetailEnhancerVideoCallbackImpl>();
327     auto res = detailEnhVideo->RegisterCallback(cb);
328     DetailEnhancerParameters param {
329         .uri = "",
330         .level = DETAIL_ENH_LEVEL_HIGH,
331     };
332     res = detailEnhVideo->SetParameter(param, VIDEO);
333     auto surface1 = detailEnhVideo->GetInputSurface();
334     res = detailEnhVideo->SetOutputSurface(surface1);
335     res = detailEnhVideo->Start();
336     res = detailEnhVideo->NotifyEos();
337     EXPECT_EQ(res, VPE_ALGO_ERR_OK);
338     detailEnhVideo->Release();
339 }
340 
341 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_19, TestSize.Level1)
342 {
343     auto detailEnhVideo = DetailEnhancerVideo::Create();
344     std::shared_ptr<DetailEnhancerVideoCallback> cb = std::make_shared<DetailEnhancerVideoCallbackImpl>();
345     auto res = detailEnhVideo->RegisterCallback(cb);
346     DetailEnhancerParameters param {
347         .uri = "",
348         .level = DETAIL_ENH_LEVEL_HIGH,
349     };
350     res = detailEnhVideo->SetParameter(param, VIDEO);
351     auto surface1 = detailEnhVideo->GetInputSurface();
352     res = detailEnhVideo->SetOutputSurface(surface1);
353     res = detailEnhVideo->Start();
354     res = detailEnhVideo->ReleaseOutputBuffer(100, true); // 100 index
355     EXPECT_NE(res, VPE_ALGO_ERR_OK);
356     detailEnhVideo->Release();
357 }
358 
359 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_20, TestSize.Level1)
360 {
361     auto detailEnhVideo = DetailEnhancerVideo::Create();
362     std::shared_ptr<DetailEnhancerVideoCallback> cb = std::make_shared<DetailEnhancerVideoCallbackImpl>();
363     auto res = detailEnhVideo->RegisterCallback(cb);
364     DetailEnhancerParameters param {
365         .uri = "",
366         .level = DETAIL_ENH_LEVEL_HIGH,
367     };
368     res = detailEnhVideo->SetParameter(param, VIDEO);
369     auto surface1 = detailEnhVideo->GetInputSurface();
370     res = detailEnhVideo->SetOutputSurface(surface1);
371     res = detailEnhVideo->Start();
372     res = detailEnhVideo->ReleaseOutputBuffer(100, false); // 100 index
373     EXPECT_NE(res, VPE_ALGO_ERR_OK);
374     detailEnhVideo->Release();
375 }
376 
377 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_21, TestSize.Level1)
378 {
379     OHNativeWindowBuffer *ohNativeWindowBuffer;
380     auto detailEnhVideo = DetailEnhancerVideo::Create();
381     std::shared_ptr<DetailEnhancerVideoCallback> cb = std::make_shared<DetailEnhancerVideoCallbackImpl>();
382     auto res = detailEnhVideo->RegisterCallback(cb);
383     DetailEnhancerParameters param {
384         .uri = "",
385         .level = DETAIL_ENH_LEVEL_HIGH,
386     };
387     res = detailEnhVideo->SetParameter(param, VIDEO);
388     auto surface = detailEnhVideo->GetInputSurface();
389     auto detailEnh2 = DetailEnhancerVideo::Create();
390     auto surface2 = detailEnh2->GetInputSurface();
391     res = detailEnhVideo->SetOutputSurface(surface2);
392     res = detailEnhVideo->Start();
393     EXPECT_EQ(res, VPE_ALGO_ERR_OK);
394 
395     int fenceFd = -1;
396     nativeWindow = CreateNativeWindowFromSurface(&surface);
397     auto ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_FORMAT, GRAPHIC_PIXEL_FMT_YCBCR_420_SP);
398     ASSERT_EQ(ret, VPE_ALGO_ERR_OK);
399     ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
400     ASSERT_EQ(ret, VPE_ALGO_ERR_OK);
401     ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &ohNativeWindowBuffer, &fenceFd);
402     ASSERT_EQ(ret, VPE_ALGO_ERR_OK);
403     ret = FlushSurf(ohNativeWindowBuffer);
404     ASSERT_EQ(ret, VPE_ALGO_ERR_OK);
405     OH_NativeWindow_DestroyNativeWindow(nativeWindow);
406     detailEnhVideo->Release();
407 }
408 
409 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_22, TestSize.Level1)
410 {
411     OHNativeWindowBuffer *ohNativeWindowBuffer;
412     auto detailEnhVideo = DetailEnhancerVideo::Create();
413     std::shared_ptr<DetailEnhancerVideoCallback> cb = std::make_shared<DetailEnhancerVideoCallbackImpl>();
414     auto res = detailEnhVideo->RegisterCallback(cb);
415     DetailEnhancerParameters param {
416         .uri = "",
417         .level = DETAIL_ENH_LEVEL_HIGH,
418     };
419     res = detailEnhVideo->SetParameter(param, VIDEO);
420     auto surface = detailEnhVideo->GetInputSurface();
421     auto detailEnh2 = DetailEnhancerVideo::Create();
422     auto surface2 = detailEnh2->GetInputSurface();
423     surface2->SetRequestWidthAndHeight(10, 10);
424     res = detailEnhVideo->SetOutputSurface(surface2);
425     res = detailEnhVideo->Start();
426     EXPECT_EQ(res, VPE_ALGO_ERR_OK);
427 
428     int fenceFd = -1;
429     nativeWindow = CreateNativeWindowFromSurface(&surface);
430     auto ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_FORMAT, GRAPHIC_PIXEL_FMT_YCBCR_420_SP);
431     ASSERT_EQ(ret, VPE_ALGO_ERR_OK);
432     ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
433     ASSERT_EQ(ret, VPE_ALGO_ERR_OK);
434     ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &ohNativeWindowBuffer, &fenceFd);
435     ASSERT_EQ(ret, VPE_ALGO_ERR_OK);
436     ret = FlushSurf(ohNativeWindowBuffer);
437     ASSERT_EQ(ret, VPE_ALGO_ERR_OK);
438     OH_NativeWindow_DestroyNativeWindow(nativeWindow);
439     detailEnhVideo->Release();
440 }
441 
442 HWTEST_F(DetailEnhancerVideoUnitTest, detailenhancer_23, TestSize.Level1)
443 {
444     OHNativeWindowBuffer *ohNativeWindowBuffer;
445     auto detailEnhVideo = DetailEnhancerVideo::Create();
446     std::shared_ptr<DetailEnhancerVideoCallback> cb = std::make_shared<DetailEnhancerVideoCallbackImpl>();
447     auto res = detailEnhVideo->RegisterCallback(cb);
448     DetailEnhancerParameters param {
449         .uri = "",
450         .level = DETAIL_ENH_LEVEL_HIGH,
451     };
452     res = detailEnhVideo->SetParameter(param, VIDEO);
453     auto surface = detailEnhVideo->GetInputSurface();
454     auto detailEnh2 = DetailEnhancerVideo::Create();
455     auto surface2 = detailEnh2->GetInputSurface();
456     surface2->SetRequestWidthAndHeight(10, 0);
457     res = detailEnhVideo->SetOutputSurface(surface2);
458     res = detailEnhVideo->Start();
459     EXPECT_EQ(res, VPE_ALGO_ERR_OK);
460 
461     int fenceFd = -1;
462     nativeWindow = CreateNativeWindowFromSurface(&surface);
463     auto ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_FORMAT, GRAPHIC_PIXEL_FMT_YCBCR_420_SP);
464     ASSERT_EQ(ret, VPE_ALGO_ERR_OK);
465     ret = OH_NativeWindow_NativeWindowHandleOpt(nativeWindow, SET_BUFFER_GEOMETRY, DEFAULT_WIDTH, DEFAULT_HEIGHT);
466     ASSERT_EQ(ret, VPE_ALGO_ERR_OK);
467     ret = OH_NativeWindow_NativeWindowRequestBuffer(nativeWindow, &ohNativeWindowBuffer, &fenceFd);
468     ASSERT_EQ(ret, VPE_ALGO_ERR_OK);
469     ret = FlushSurf(ohNativeWindowBuffer);
470     ASSERT_EQ(ret, VPE_ALGO_ERR_OK);
471     OH_NativeWindow_DestroyNativeWindow(nativeWindow);
472     detailEnhVideo->Release();
473 }
474 
475 } // namespace VideoProcessingEngine
476 } // namespace Media
477 } // namespace OHOS
478