• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef HDF_LOG_TAG
19 #undef HDF_LOG_TAG
20 #endif
21 
22 #define HDF_LOG_TAG camera_service_test
23 
24 constexpr const char *TEST_SERVICE_NAME = "camera_service";
25 
SetUpTestCase(void)26 void CameraHdiBaseTest::SetUpTestCase(void)
27 {
28 }
29 
TearDownTestCase(void)30 void CameraHdiBaseTest::TearDownTestCase(void)
31 {
32 }
33 
SetUp(void)34 void CameraHdiBaseTest::SetUp(void)
35 {
36 }
37 
TearDown(void)38 void CameraHdiBaseTest::TearDown(void)
39 {
40 }
41 
InitCameraHost()42 bool CameraHdiBaseTest::InitCameraHost()
43 {
44     if (cameraHost_ != nullptr) {
45         return true;
46     }
47 #ifdef CAMERA_BUILT_ON_OHOS_LITE
48     cameraHost_ = OHOS::Camera::CameraHost::CreateCameraHost();
49 #else
50     cameraHost_ = ICameraHost::Get(TEST_SERVICE_NAME, false);
51 #endif
52     if (cameraHost_ == nullptr) {
53         return false;
54     }
55     return true;
56 }
57 
GetCameraDevice()58 bool CameraHdiBaseTest::GetCameraDevice()
59 {
60     if (cameraDevice_ != nullptr) {
61         return true;
62     }
63 
64     if (cameraIds_.empty()) {
65         return false;
66     }
67 
68     std::string cameraId = cameraIds_.front();
69 #ifdef CAMERA_BUILT_ON_OHOS_LITE
70     std::shared_ptr<CameraDeviceCallback> deviceCallback = std::make_shared<CameraDeviceCallback>();
71 #else
72     sptr<DemoCameraDeviceCallback> deviceCallback = new DemoCameraDeviceCallback();
73 #endif
74     CamRetCode rc = (CamRetCode)cameraHost_->OpenCamera(cameraId, deviceCallback, cameraDevice_);
75     if (cameraDevice_ == nullptr) {
76         return false;
77     }
78     return true;
79 }
80 
GetStreamOperator()81 bool CameraHdiBaseTest::GetStreamOperator()
82 {
83     if (streamOperator_ != nullptr) {
84         return true;
85     }
86 
87     if (cameraDevice_ == nullptr) {
88         return false;
89     }
90 
91 #ifdef CAMERA_BUILT_ON_OHOS_LITE
92     std::shared_ptr<StreamOperatorCallback> streamOperatorCallback = std::make_shared<StreamOperatorCallback>();
93 #else
94     OHOS::sptr<IStreamOperatorCallback> streamOperatorCallback = new DemoStreamOperatorCallback();
95 #endif
96     (void)cameraDevice_->GetStreamOperator(streamOperatorCallback, streamOperator_);
97     if (streamOperator_ == nullptr) {
98         return false;
99     }
100     return true;
101 }
102 
GetCameraIds()103 bool CameraHdiBaseTest::GetCameraIds()
104 {
105     if (InitCameraHost()) {
106         (void)cameraHost_->GetCameraIds(cameraIds_);
107     }
108     if (cameraIds_.empty()) {
109         return false;
110     }
111     return true;
112 }
113 
SaveToFile(const std::string & path,const void * buffer,int32_t size) const114 int32_t CameraHdiBaseTest::SaveToFile(const std::string& path, const void* buffer, int32_t size) const
115 {
116     char checkPath[PATH_MAX] = {0};
117     if (::realpath(path.c_str(), checkPath) == nullptr) {
118         return -1;
119     }
120     int imgFd = open(path.c_str(), O_RDWR | O_CREAT, 00766);
121     if (imgFd == -1) {
122         std::cout << "open file failed." << std::endl;
123         return -1;
124     }
125 
126     int ret = write(imgFd, buffer, size);
127     if (ret == -1) {
128         std::cout << "write failed." << std::endl;
129         close(imgFd);
130         return -1;
131     }
132     close(imgFd);
133     return 0;
134 }
135 
GetCurrentLocalTimeStamp() const136 uint64_t CameraHdiBaseTest::GetCurrentLocalTimeStamp() const
137 {
138     std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp =
139         std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
140     auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
141     return tmp.count();
142 }
143 
144 #ifdef CAMERA_BUILT_ON_OHOS_LITE
145 #define YUV_SAVE_PATH "/userdata/camera"
146 #else
147 #define YUV_SAVE_PATH "/data/log/camera"
148 #endif
149 
SaveYUV(const char * type,const void * buffer,int32_t size)150 int32_t CameraHdiBaseTest::SaveYUV(const char* type, const void* buffer, int32_t size)
151 {
152     if (strncmp(type, "preview", strlen(type)) == 0) {
153         previewBufCnt += 1;
154         if (previewBufCnt % 8 != 0) { // 8:Save one frame every eight frames
155             std::cout << "receive preview buffer not save" << std::endl;
156             return 0;
157         }
158     }
159 
160     if (access(YUV_SAVE_PATH, F_OK) != 0) {
161         std::cout << "save path: " << YUV_SAVE_PATH << " not exist" << std::endl;
162         return 0;
163     }
164 
165     char path[PATH_MAX] = {0};
166     int ret;
167     if (strncmp(type, "preview", strlen(type)) == 0) {
168         system("mkdir -p " YUV_SAVE_PATH "/preview/");
169         char prefix[] = YUV_SAVE_PATH "/preview/";
170         ret = sprintf_s(path, sizeof(path) / sizeof(path[0]), "%s%s_%lld.yuv",
171             prefix, type, GetCurrentLocalTimeStamp());
172     } else {
173         system("mkdir -p " YUV_SAVE_PATH "/capture/");
174         char prefix[] = YUV_SAVE_PATH "/capture/";
175         ret = sprintf_s(path, sizeof(path) / sizeof(path[0]), "%s%s_%lld.jpg",
176             prefix, type, GetCurrentLocalTimeStamp());
177     }
178     if (ret < 0) {
179         std::cout << "sprintf path failed: " << path << std::endl;
180         CAMERA_LOGE("%s: sprintf path failed", __func__);
181         return -1;
182     }
183 
184     std::cout << "save yuv to file:" << path << std::endl;
185     int imgFd = open(path, O_RDWR | O_CREAT | O_APPEND, 00766); // 00766:file jurisdiction
186     if (imgFd == -1) {
187         std::cout << "open file failed, errno = " << strerror(errno) << std::endl;
188         return -1;
189     }
190 
191     ret = write(imgFd, buffer, size);
192     if (ret == -1) {
193         std::cout << "write file failed, error = " << strerror(errno) << std::endl;
194         close(imgFd);
195         return -1;
196     }
197     close(imgFd);
198     return 0;
199 }
200 
OnCameraStatus(const std::string & cameraId,CameraStatus status)201 int32_t DemoCameraHostCallback::OnCameraStatus(const std::string& cameraId, CameraStatus status)
202 {
203     (void)cameraId;
204     (void)status;
205     CAMERA_LOGI("%{public}s, enter.", __func__);
206     return RC_OK;
207 }
208 
OnFlashlightStatus(const std::string & cameraId,FlashlightStatus status)209 int32_t DemoCameraHostCallback::OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status)
210 {
211     CAMERA_LOGI("%{public}s, enter. cameraId = %s, status = %d",
212         __func__, cameraId.c_str(), static_cast<int>(status));
213     return RC_OK;
214 }
215 
OnCameraEvent(const std::string & cameraId,CameraEvent event)216 int32_t DemoCameraHostCallback::OnCameraEvent(const std::string& cameraId, CameraEvent event)
217 {
218     CAMERA_LOGI("%{public}s, enter. cameraId = %s, event = %d",
219         __func__, cameraId.c_str(), static_cast<int>(event));
220     return RC_OK;
221 }
222 
OnCaptureStarted(int32_t captureId,const std::vector<int32_t> & streamIds)223 int32_t DemoStreamOperatorCallback::OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds)
224 {
225     (void)captureId;
226     (void)streamIds;
227     CAMERA_LOGI("%{public}s, enter.", __func__);
228     return RC_OK;
229 }
230 
OnCaptureEnded(int32_t captureId,const std::vector<CaptureEndedInfo> & infos)231 int32_t DemoStreamOperatorCallback::OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos)
232 {
233     (void)captureId;
234     (void)infos;
235     CAMERA_LOGI("%{public}s, enter.", __func__);
236     return RC_OK;
237 }
238 
OnCaptureError(int32_t captureId,const std::vector<CaptureErrorInfo> & infos)239 int32_t DemoStreamOperatorCallback::OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos)
240 {
241     (void)captureId;
242     (void)infos;
243     CAMERA_LOGI("%{public}s, enter.", __func__);
244     return RC_OK;
245 }
246 
OnFrameShutter(int32_t captureId,const std::vector<int32_t> & streamIds,uint64_t timestamp)247 int32_t DemoStreamOperatorCallback::OnFrameShutter(int32_t captureId,
248     const std::vector<int32_t>& streamIds, uint64_t timestamp)
249 {
250     (void)captureId;
251     (void)streamIds;
252     (void)timestamp;
253     CAMERA_LOGI("%{public}s, enter.", __func__);
254     return RC_OK;
255 }
256 
OnError(ErrorType type,int32_t errorCode)257 int32_t DemoCameraDeviceCallback::OnError(ErrorType type, int32_t errorCode)
258 {
259     (void)type;
260     (void)errorCode;
261     CAMERA_LOGI("%{public}s, enter.", __func__);
262     return RC_OK;
263 }
264 
OnResult(uint64_t timestamp,const std::vector<uint8_t> & result)265 int32_t DemoCameraDeviceCallback::OnResult(uint64_t timestamp, const std::vector<uint8_t>& result)
266 {
267     (void)timestamp;
268     (void)result;
269     CAMERA_LOGI("%{public}s, enter.", __func__);
270     return RC_OK;
271 }
272