1 /*
2 * Copyright (C) 2023 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 "videoenc_unit_test.h"
17
18 using namespace std;
19 using namespace OHOS;
20 using namespace OHOS::MediaAVCodec;
21 using namespace testing::ext;
22 using namespace testing::mt;
23 using namespace OHOS::MediaAVCodec::VCodecTestParam;
24
25 namespace {
26 std::atomic<int32_t> vencCount = 0;
27 std::string vencName = "";
28
MultiThreadCreateVEnc()29 void MultiThreadCreateVEnc()
30 {
31 std::shared_ptr<VEncSignal> vencSignal = std::make_shared<VEncSignal>();
32 std::shared_ptr<VEncCallbackTest> vencCallback = std::make_shared<VEncCallbackTest>(vencSignal);
33 ASSERT_NE(nullptr, vencCallback);
34
35 std::shared_ptr<VideoEncSample> videoEnc = std::make_shared<VideoEncSample>(vencSignal);
36 ASSERT_NE(nullptr, videoEnc);
37
38 EXPECT_LE(vencCount.load(), 16); // 16: max instances supported
39 if (videoEnc->CreateVideoEncMockByName(vencName)) {
40 vencCount++;
41 cout << "create successed, num:" << vencCount.load() << endl;
42 } else {
43 cout << "create failed, num:" << vencCount.load() << endl;
44 return;
45 }
46 sleep(1);
47 videoEnc->Release();
48 vencCount--;
49 }
50 } // namespace
51
SetUpTestCase(void)52 void VideoEncUnitTest::SetUpTestCase(void) {}
53
TearDownTestCase(void)54 void VideoEncUnitTest::TearDownTestCase(void) {}
55
SetUp(void)56 void VideoEncUnitTest::SetUp(void)
57 {
58 std::shared_ptr<VEncSignal> vencSignal = std::make_shared<VEncSignal>();
59 vencCallback_ = std::make_shared<VEncCallbackTest>(vencSignal);
60 ASSERT_NE(nullptr, vencCallback_);
61
62 videoEnc_ = std::make_shared<VideoEncSample>(vencSignal);
63 ASSERT_NE(nullptr, videoEnc_);
64
65 format_ = FormatMockFactory::CreateFormat();
66 ASSERT_NE(nullptr, format_);
67
68 auto capability = CodecListMockFactory::GetCapabilityByCategory((CodecMimeType::VIDEO_AVC).data(), true,
69 AVCodecCategory::AVCODEC_HARDWARE);
70 ASSERT_NE(nullptr, capability) << (CodecMimeType::VIDEO_AVC).data() << " can not found!" << std::endl;
71 vencName = capability->GetName();
72 }
73
TearDown(void)74 void VideoEncUnitTest::TearDown(void)
75 {
76 if (format_ != nullptr) {
77 format_->Destroy();
78 }
79 }
80
CreateVideoCodecByMime(const std::string & encMime)81 bool VideoEncUnitTest::CreateVideoCodecByMime(const std::string &encMime)
82 {
83 if (videoEnc_->CreateVideoEncMockByMime(encMime) == false || videoEnc_->SetCallback(vencCallback_) != AV_ERR_OK) {
84 return false;
85 }
86 return true;
87 }
88
CreateVideoCodecByName(const std::string & encName)89 bool VideoEncUnitTest::CreateVideoCodecByName(const std::string &encName)
90 {
91 if (videoEnc_->CreateVideoEncMockByName(encName) == false || videoEnc_->SetCallback(vencCallback_) != AV_ERR_OK) {
92 return false;
93 }
94 return true;
95 }
96
97 /**
98 * @tc.name: videoEncoder_multithread_create_001
99 * @tc.desc: try create 100 instances
100 * @tc.type: FUNC
101 */
102 HWTEST_F(VideoEncUnitTest, videoEncoder_multithread_create_001, TestSize.Level1)
103 {
104 SET_THREAD_NUM(100);
105 vencCount = 0;
106 GTEST_RUN_TASK(MultiThreadCreateVEnc);
107 cout << "remaining num: " << vencCount.load() << endl;
108 }
109
110 /**
111 * @tc.name: videoEncoder_createWithNull_001
112 * @tc.desc: video create
113 * @tc.type: FUNC
114 */
115 HWTEST_F(VideoEncUnitTest, videoEncoder_createWithNull_001, TestSize.Level1)
116 {
117 ASSERT_FALSE(CreateVideoCodecByName(""));
118 }
119
120 /**
121 * @tc.name: videoEncoder_createWithNull_002
122 * @tc.desc: video create
123 * @tc.type: FUNC
124 */
125 HWTEST_F(VideoEncUnitTest, videoEncoder_createWithNull_002, TestSize.Level1)
126 {
127 ASSERT_FALSE(CreateVideoCodecByMime(""));
128 }
129
130 /**
131 * @tc.name: videoEncoder_create_001
132 * @tc.desc: video create
133 * @tc.type: FUNC
134 */
135 HWTEST_F(VideoEncUnitTest, videoEncoder_create_001, TestSize.Level1)
136 {
137 ASSERT_TRUE(CreateVideoCodecByName(vencName));
138 }
139
140 /**
141 * @tc.name: videoEncoder_create_002
142 * @tc.desc: video create
143 * @tc.type: FUNC
144 */
145 HWTEST_F(VideoEncUnitTest, videoEncoder_create_002, TestSize.Level1)
146 {
147 ASSERT_TRUE(CreateVideoCodecByMime((CodecMimeType::VIDEO_AVC).data()));
148 }
149