1 /*
2 * Copyright (c) 2021 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 "utest_camera_hdi_base.h"
17
18 #define HDF_LOG_TAG camera_service_test
19
20 constexpr const char *TEST_SERVICE_NAME = "camera_service";
21
SetUpTestCase(void)22 void CameraHdiBaseTest::SetUpTestCase(void)
23 {
24 }
25
TearDownTestCase(void)26 void CameraHdiBaseTest::TearDownTestCase(void)
27 {
28 }
29
SetUp(void)30 void CameraHdiBaseTest::SetUp(void)
31 {
32 }
33
TearDown(void)34 void CameraHdiBaseTest::TearDown(void)
35 {
36 }
37
InitCameraHost()38 bool CameraHdiBaseTest::InitCameraHost()
39 {
40 if (cameraHost_ != nullptr) {
41 return true;
42 }
43 #ifdef CAMERA_BUILT_ON_OHOS_LITE
44 cameraHost_ = OHOS::Camera::CameraHost::CreateCameraHost();
45 #else
46 cameraHost_ = ICameraHost::Get(TEST_SERVICE_NAME);
47 #endif
48 if (cameraHost_ == nullptr) {
49 return false;
50 }
51 return true;
52 }
53
GetCameraDevice()54 bool CameraHdiBaseTest::GetCameraDevice()
55 {
56 if (cameraDevice_ != nullptr) {
57 return true;
58 }
59
60 if (cameraIds_.empty()) {
61 return false;
62 }
63
64 std::string cameraId = cameraIds_.front();
65 #ifdef CAMERA_BUILT_ON_OHOS_LITE
66 std::shared_ptr<CameraDeviceCallback> deviceCallback = std::make_shared<CameraDeviceCallback>();
67 #else
68 OHOS::sptr<CameraDeviceCallback> deviceCallback = new CameraDeviceCallback();
69 #endif
70 CamRetCode rc = cameraHost_->OpenCamera(cameraId, deviceCallback, cameraDevice_);
71 if (cameraDevice_ == nullptr) {
72 return false;
73 }
74 return true;
75 }
76
GetStreamOperator()77 bool CameraHdiBaseTest::GetStreamOperator()
78 {
79 if (streamOperator_ != nullptr) {
80 return true;
81 }
82
83 if (cameraDevice_ == nullptr) {
84 return false;
85 }
86
87 #ifdef CAMERA_BUILT_ON_OHOS_LITE
88 std::shared_ptr<StreamOperatorCallback> streamOperatorCallback = std::make_shared<StreamOperatorCallback>();
89 #else
90 OHOS::sptr<StreamOperatorCallback> streamOperatorCallback = new StreamOperatorCallback();
91 #endif
92 (void)cameraDevice_->GetStreamOperator(streamOperatorCallback, streamOperator_);
93 if (streamOperator_ == nullptr) {
94 return false;
95 }
96 return true;
97 }
98
GetCameraIds()99 bool CameraHdiBaseTest::GetCameraIds()
100 {
101 if (InitCameraHost()) {
102 (void)cameraHost_->GetCameraIds(cameraIds_);
103 }
104 if (cameraIds_.empty()) {
105 return false;
106 }
107 return true;
108 }
109
SaveToFile(const std::string path,const void * buffer,int32_t size)110 int32_t CameraHdiBaseTest::SaveToFile(const std::string path, const void* buffer, int32_t size)
111 {
112 char checkPath[PATH_MAX] = {0};
113 if (::realpath(path.c_str(), checkPath) == nullptr) {
114 return -1;
115 }
116 int imgFd = open(path.c_str(), O_RDWR | O_CREAT, 00766);
117 if (imgFd == -1) {
118 std::cout << "open file failed." << std::endl;
119 return -1;
120 }
121
122 int ret = write(imgFd, buffer, size);
123 if (ret == -1) {
124 std::cout << "write failed." << std::endl;
125 close(imgFd);
126 return -1;
127 }
128 close(imgFd);
129 return 0;
130 }
131
GetCurrentLocalTimeStamp()132 uint64_t CameraHdiBaseTest::GetCurrentLocalTimeStamp()
133 {
134 std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp =
135 std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
136 auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
137 return tmp.count();
138 }
139