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 #include "test_camera_base.h"
16 using namespace std;
17
18 const std::vector<int32_t> DATA_BASE = {
19 OHOS_CAMERA_STREAM_ID,
20 OHOS_SENSOR_COLOR_CORRECTION_GAINS,
21 OHOS_SENSOR_EXPOSURE_TIME,
22 OHOS_CONTROL_EXPOSURE_MODE,
23 OHOS_CONTROL_AE_EXPOSURE_COMPENSATION,
24 OHOS_CONTROL_FOCUS_MODE,
25 OHOS_CONTROL_METER_MODE,
26 OHOS_CONTROL_FLASH_MODE,
27 OHOS_CONTROL_FPS_RANGES,
28 OHOS_CONTROL_AWB_MODE,
29 OHOS_CONTROL_AF_REGIONS,
30 OHOS_CONTROL_METER_POINT,
31 OHOS_CONTROL_VIDEO_STABILIZATION_MODE,
32 OHOS_CONTROL_FOCUS_STATE,
33 OHOS_CONTROL_EXPOSURE_STATE,
34 };
35
TestCameraBase()36 TestCameraBase::TestCameraBase()
37 {
38 }
39
GetCurrentLocalTimeStamp()40 uint64_t TestCameraBase::GetCurrentLocalTimeStamp()
41 {
42 std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp =
43 std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
44 auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
45 return tmp.count();
46 }
47
StoreImage(const unsigned char * bufStart,const uint32_t size) const48 void TestCameraBase::StoreImage(const unsigned char *bufStart, const uint32_t size) const
49 {
50 constexpr uint32_t pathLen = 64;
51 char path[pathLen] = {0};
52 #ifdef CAMERA_BUILT_ON_OHOS_LITE
53 char prefix[] = "/userdata/photo/";
54 #else
55 char prefix[] = "/data/";
56 #endif
57
58 int imgFD = 0;
59 int ret = 0;
60
61 struct timeval start = {};
62 gettimeofday(&start, nullptr);
63 if (sprintf_s(path, sizeof(path), "%spicture_%ld.jpeg", prefix, start.tv_usec) < 0) {
64 CAMERA_LOGE("sprintf_s error .....\n");
65 return;
66 }
67
68 imgFD = open(path, O_RDWR | O_CREAT, 00766); // 00766:file operate permission
69 if (imgFD == -1) {
70 CAMERA_LOGE("demo test:open image file error %{public}s.....\n", strerror(errno));
71 return;
72 }
73
74 CAMERA_LOGD("demo test:StoreImage %{public}s size == %{public}d\n", path, size);
75
76 ret = write(imgFD, bufStart, size);
77 if (ret == -1) {
78 CAMERA_LOGE("demo test:write image file error %{public}s.....\n", strerror(errno));
79 }
80
81 close(imgFD);
82 }
83
StoreVideo(const unsigned char * bufStart,const uint32_t size) const84 void TestCameraBase::StoreVideo(const unsigned char *bufStart, const uint32_t size) const
85 {
86 constexpr uint32_t pathLen = 64;
87 char path[pathLen] = {0};
88 #ifdef CAMERA_BUILT_ON_OHOS_LITE
89 char prefix[] = "/userdata/video/";
90 #else
91 char prefix[] = "/data/";
92 #endif
93 struct timeval start = {};
94 gettimeofday(&start, nullptr);
95 if (sprintf_s(path, sizeof(path), "%svideo_%ld.yuv", prefix, start.tv_usec) < 0) {
96 CAMERA_LOGE("%{public}s: sprintf failed", __func__);
97 return;
98 }
99 int videoFd = open(path, O_RDWR | O_CREAT, 00766); // 00766:file operate permission
100 if (videoFd < 0) {
101 CAMERA_LOGE("demo test: StartVideo open %s %{public}s failed", path, strerror(errno));
102 }
103 int ret = 0;
104
105 ret = write(videoFd, bufStart, size);
106 if (ret == -1) {
107 CAMERA_LOGE("demo test:write video file error %{public}s.....\n", strerror(errno));
108 }
109 CAMERA_LOGD("demo test:StoreVideo size == %{public}d\n", size);
110 close(videoFd);
111 }
112
OpenVideoFile()113 void TestCameraBase::OpenVideoFile()
114 {
115 constexpr uint32_t pathLen = 64;
116 char path[pathLen] = {0};
117 #ifdef CAMERA_BUILT_ON_OHOS_LITE
118 char prefix[] = "/userdata/video/";
119 #else
120 char prefix[] = "/data/";
121 #endif
122 auto seconds = time(nullptr);
123 if (sprintf_s(path, sizeof(path), "%svideo%ld.h264", prefix, seconds) < 0) {
124 CAMERA_LOGE("%{public}s: sprintf failed", __func__);
125 return;
126 }
127 videoFd_ = open(path, O_RDWR | O_CREAT, 00766); // 00766:file operate permission
128 if (videoFd_ < 0) {
129 CAMERA_LOGE("demo test: StartVideo open %s %{public}s failed", path, strerror(errno));
130 }
131 }
132
CloseFd()133 void TestCameraBase::CloseFd()
134 {
135 close(videoFd_);
136 videoFd_ = -1;
137 }
138
PrintFaceDetectInfo(const unsigned char * bufStart,const uint32_t size) const139 void TestCameraBase::PrintFaceDetectInfo(const unsigned char *bufStart, const uint32_t size) const
140 {
141 common_metadata_header_t* data = reinterpret_cast<common_metadata_header_t*>(
142 const_cast<unsigned char*>(bufStart));
143 camera_metadata_item_t entry;
144 int ret = 0;
145 ret = FindCameraMetadataItem(data, OHOS_STATISTICS_FACE_DETECT_SWITCH, &entry);
146 if (ret != 0) {
147 CAMERA_LOGE("demo test: get OHOS_STATISTICS_FACE_DETECT_SWITCH error\n");
148 return;
149 }
150 uint8_t switchValue = *(entry.data.u8);
151 CAMERA_LOGI("demo test: switchValue=%{public}d", switchValue);
152
153 ret = FindCameraMetadataItem(data, OHOS_STATISTICS_FACE_RECTANGLES, &entry);
154 if (ret != 0) {
155 CAMERA_LOGE("demo test: get OHOS_STATISTICS_FACE_RECTANGLES error\n");
156 return;
157 }
158 uint32_t rectCount = entry.count;
159 CAMERA_LOGI("demo test: rectCount=%{public}d", rectCount);
160 std::vector<std::vector<float>> faceRectangles;
161 std::vector<float> faceRectangle;
162 for (int i = 0; i < rectCount; i++) {
163 faceRectangle.push_back(*(entry.data.f + i));
164 }
165 faceRectangles.push_back(faceRectangle);
166 for (std::vector<std::vector<float>>::iterator it = faceRectangles.begin(); it < faceRectangles.end(); it++) {
167 for (std::vector<float>::iterator innerIt = (*it).begin(); innerIt < (*it).end(); innerIt++) {
168 CAMERA_LOGI("demo test: innerIt : %{public}f", *innerIt);
169 }
170 }
171
172 ret = FindCameraMetadataItem(data, OHOS_STATISTICS_FACE_IDS, &entry);
173 if (ret != 0) {
174 CAMERA_LOGE("demo test: get OHOS_STATISTICS_FACE_IDS error\n");
175 return;
176 }
177 uint32_t idCount = entry.count;
178 CAMERA_LOGI("demo test: idCount=%{public}d", idCount);
179 std::vector<int32_t> faceIds;
180 for (int i = 0; i < idCount; i++) {
181 faceIds.push_back(*(entry.data.i32 + i));
182 }
183 for (auto it = faceIds.begin(); it != faceIds.end(); it++) {
184 CAMERA_LOGI("demo test: faceIds : %{public}d", *it);
185 }
186 }
187
SaveYUV(char * type,unsigned char * buffer,int32_t size)188 int32_t TestCameraBase::SaveYUV(char* type, unsigned char* buffer, int32_t size)
189 {
190 int ret;
191 char path[PATH_MAX] = {0};
192 ret = sprintf_s(path, sizeof(path) / sizeof(path[0]), "/mnt/yuv/%s_%lld.yuv", type, GetCurrentLocalTimeStamp());
193 if (ret < 0) {
194 CAMERA_LOGE("%s, sprintf_s failed, errno = %s.", __FUNCTION__, strerror(errno));
195 return -1;
196 }
197 CAMERA_LOGI("%s, save yuv to file %s", __FUNCTION__, path);
198 system("mkdir -p /mnt/yuv");
199 int imgFd = open(path, O_RDWR | O_CREAT, 00766); // 00766: file permissions
200 if (imgFd == -1) {
201 CAMERA_LOGI("%s, open file failed, errno = %s.", __FUNCTION__, strerror(errno));
202 return -1;
203 }
204 ret = write(imgFd, buffer, size);
205 if (ret == -1) {
206 CAMERA_LOGI("%s, write file failed, error = %s", __FUNCTION__, strerror(errno));
207 close(imgFd);
208 return -1;
209 }
210 close(imgFd);
211 return 0;
212 }
213
DoFbMunmap(unsigned char * addr)214 int TestCameraBase::DoFbMunmap(unsigned char* addr)
215 {
216 int ret;
217 unsigned int size = vinfo_.xres * vinfo_.yres * vinfo_.bits_per_pixel / 8; // 8:picture size;
218 CAMERA_LOGI("main test:munmapped size = %d\n", size);
219 ret = (munmap(addr, finfo_.smem_len));
220 return ret;
221 }
222
DoFbMmap(int * pmemfd)223 unsigned char* TestCameraBase::DoFbMmap(int* pmemfd)
224 {
225 unsigned char* ret;
226 int screensize = vinfo_.xres * vinfo_.yres * vinfo_.bits_per_pixel / 8; // 8:picture size
227 ret = static_cast<unsigned char*>(mmap(NULL, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, *pmemfd, 0));
228 if (ret == MAP_FAILED) {
229 CAMERA_LOGE("main test:do_mmap: pmem mmap() failed: %s (%d)\n", strerror(errno), errno);
230 return nullptr;
231 }
232 CAMERA_LOGI("main test:do_mmap: pmem mmap fd %d len %u\n", *pmemfd, screensize);
233 return ret;
234 }
235
FBLog()236 void TestCameraBase::FBLog()
237 {
238 CAMERA_LOGI("the fixed information is as follow:\n");
239 CAMERA_LOGI("id=%s\n", finfo_.id);
240 CAMERA_LOGI("sem_start=%lx\n", finfo_.smem_start);
241 CAMERA_LOGI("smem_len=%u\n", finfo_.smem_len);
242 CAMERA_LOGI("type=%u\n", finfo_.type);
243 CAMERA_LOGI("line_length=%u\n", finfo_.line_length);
244 CAMERA_LOGI("mmio_start=%lu\n", finfo_.mmio_start);
245 CAMERA_LOGI("mmio_len=%d\n", finfo_.mmio_len);
246 CAMERA_LOGI("visual=%d\n", finfo_.visual);
247
248 CAMERA_LOGI("variable information is as follow:\n");
249 CAMERA_LOGI("The xres is :%u\n", vinfo_.xres);
250 CAMERA_LOGI("The yres is :%u\n", vinfo_.yres);
251 CAMERA_LOGI("xres_virtual=%u\n", vinfo_.xres_virtual);
252 CAMERA_LOGI("yres_virtual=%u\n", vinfo_.yres_virtual);
253 CAMERA_LOGI("xoffset=%u\n", vinfo_.xoffset);
254 CAMERA_LOGI("yoffset=%u\n", vinfo_.yoffset);
255 CAMERA_LOGI("bits_per_pixel is :%u\n", vinfo_.bits_per_pixel);
256 CAMERA_LOGI("red.offset=%u\n", vinfo_.red.offset);
257 CAMERA_LOGI("red.length=%u\n", vinfo_.red.length);
258 CAMERA_LOGI("red.msb_right=%u\n", vinfo_.red.msb_right);
259 CAMERA_LOGI("green.offset=%d\n", vinfo_.green.offset);
260 CAMERA_LOGI("green.length=%d\n", vinfo_.green.length);
261 CAMERA_LOGI("green.msb_right=%d\n", vinfo_.green.msb_right);
262 CAMERA_LOGI("blue.offset=%d\n", vinfo_.blue.offset);
263 CAMERA_LOGI("blue.length=%d\n", vinfo_.blue.length);
264 CAMERA_LOGI("blue.msb_right=%d\n", vinfo_.blue.msb_right);
265 CAMERA_LOGI("transp.offset=%d\n", vinfo_.transp.offset);
266 CAMERA_LOGI("transp.length=%d\n", vinfo_.transp.length);
267 CAMERA_LOGI("transp.msb_right=%d\n", vinfo_.transp.msb_right);
268 CAMERA_LOGI("height=%x\n", vinfo_.height);
269 }
270
FBInit()271 OHOS::Camera::RetCode TestCameraBase::FBInit()
272 {
273 fbFd_ = open("/dev/fb0", O_RDWR);
274 if (fbFd_ < 0) {
275 CAMERA_LOGE("main test:cannot open framebuffer %s file node\n", "/dev/fb0");
276 return RC_ERROR;
277 }
278
279 if (ioctl(fbFd_, FBIOGET_VSCREENINFO, &vinfo_) < 0) {
280 CAMERA_LOGE("main test:cannot retrieve vscreenInfo!\n");
281 close(fbFd_);
282 return RC_ERROR;
283 }
284
285 if (ioctl(fbFd_, FBIOGET_FSCREENINFO, &finfo_) < 0) {
286 CAMERA_LOGE("main test:can't retrieve fscreenInfo!\n");
287 close(fbFd_);
288 return RC_ERROR;
289 }
290
291 FBLog();
292
293 CAMERA_LOGI("main test:allocating display buffer memory\n");
294 displayBuf_ = DoFbMmap(&fbFd_);
295 if (displayBuf_ == nullptr) {
296 CAMERA_LOGE("main test:error displayBuf_ mmap error\n");
297 close(fbFd_);
298 return RC_ERROR;
299 }
300 return RC_OK;
301 }
302
ProcessImage(unsigned char * p,unsigned char * fbp)303 void TestCameraBase::ProcessImage(unsigned char* p, unsigned char* fbp)
304 {
305 unsigned char* in = p;
306 int width = 640; // 640:Displays the size of the width
307 int height = 480; // 480:Displays the size of the height
308 int istride = 1280; // 1280:Initial value of span
309 int x;
310 int y;
311 int j;
312 int y0;
313 int u;
314 int v;
315 int r;
316 int g;
317 int b;
318 int32_t location = 0;
319 int xpos = (vinfo_.xres - width) / 2;
320 int ypos = (vinfo_.yres - height) / 2;
321 int yPos = 0; // 0:Pixel initial value
322 int uPos = 1; // 1:Pixel initial value
323 int vPos = 3; // 3:Pixel initial value
324 int yPosIncrement = 2; // 2:yPos increase value
325 int uPosIncrement = 4; // 4:uPos increase value
326 int vPosIncrement = 4; // 4:vPos increase value
327
328 for (y = ypos; y < (height + ypos); y++) {
329 for (j = 0, x = xpos; j < width; j++, x++) {
330 location = (x + vinfo_.xoffset) * (vinfo_.bits_per_pixel / 8) + // 8: The bytes for each time
331 (y + vinfo_.yoffset) * finfo_.line_length; // add one y number of rows at a time
332
333 y0 = in[yPos];
334 u = in[uPos] - 128; // 128:display size
335 v = in[vPos] - 128; // 128:display size
336
337 r = RANGE_LIMIT(y0 + v + ((v * 103) >> 8)); // 103,8:display range
338 g = RANGE_LIMIT(y0 - ((u * 88) >> 8) - ((v * 183) >> 8)); // 88,8,183:display range
339 b = RANGE_LIMIT(y0 + u + ((u * 198) >> 8)); // 198,8:display range
340
341 fbp[location + 1] = ((r & 0xF8) | (g >> 5)); // 5:display range
342 fbp[location + 0] = (((g & 0x1C) << 3) | (b >> 3)); // 3:display range
343
344 yPos += yPosIncrement;
345
346 if (j & 0x01) {
347 uPos += uPosIncrement;
348 vPos += vPosIncrement;
349 }
350 }
351
352 yPos = 0; // 0:Pixel initial value
353 uPos = 1; // 1:Pixel initial value
354 vPos = 3; // 3:Pixel initial value
355 in += istride; // add one y number of rows at a time
356 }
357 }
358
LcdDrawScreen(unsigned char * displayBuf,unsigned char * addr)359 void TestCameraBase::LcdDrawScreen(unsigned char* displayBuf, unsigned char* addr)
360 {
361 ProcessImage(addr, displayBuf);
362 }
363
BufferCallback(unsigned char * addr,int choice)364 void TestCameraBase::BufferCallback(unsigned char* addr, int choice)
365 {
366 if (choice == PREVIEW_MODE) {
367 LcdDrawScreen(displayBuf_, addr);
368 return;
369 } else {
370 LcdDrawScreen(displayBuf_, addr);
371 std::cout << "==========[test log] capture start saveYuv......" << std::endl;
372 SaveYUV("capture", reinterpret_cast<unsigned char*>(addr), bufSize_);
373 std::cout << "==========[test log] capture end saveYuv......" << std::endl;
374 return;
375 }
376 }
377
Init()378 void TestCameraBase::Init()
379 {
380 CAMERA_LOGD("TestCameraBase::Init().");
381 if (cameraHost == nullptr) {
382 constexpr const char *demoServiceName = "camera_service";
383 cameraHost = ICameraHost::Get(demoServiceName, false);
384 CAMERA_LOGI("Camera::CameraHost::CreateCameraHost()");
385 if (cameraHost == nullptr) {
386 CAMERA_LOGE("CreateCameraHost failed.");
387 return;
388 }
389 CAMERA_LOGI("CreateCameraHost success.");
390 }
391
392 OHOS::sptr<DemoCameraHostCallback> cameraHostCallback = new DemoCameraHostCallback();
393 OHOS::Camera::RetCode ret = cameraHost->SetCallback(cameraHostCallback);
394 if (ret != HDI::Camera::V1_0::NO_ERROR) {
395 CAMERA_LOGE("SetCallback failed.");
396 return;
397 } else {
398 CAMERA_LOGI("SetCallback success.");
399 }
400
401 if (cameraDevice == nullptr) {
402 cameraHost->GetCameraIds(cameraIds);
403 cameraHost->GetCameraAbility(cameraIds.front(), ability_);
404 MetadataUtils::ConvertVecToMetadata(ability_, ability);
405 const OHOS::sptr<DemoCameraDeviceCallback> callback = new DemoCameraDeviceCallback();
406 rc = (CamRetCode)cameraHost->OpenCamera(cameraIds.front(), callback, cameraDevice);
407 if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) {
408 CAMERA_LOGE("OpenCamera failed, rc = %{public}d", rc);
409 return;
410 }
411 CAMERA_LOGI("OpenCamera success.");
412 }
413 }
414
UsbInit()415 void TestCameraBase::UsbInit()
416 {
417 if (cameraHost == nullptr) {
418 constexpr const char *demoServiceName = "camera_service";
419 cameraHost = ICameraHost::Get(demoServiceName, false);
420 if (cameraHost == nullptr) {
421 std::cout << "==========[test log] CreateCameraHost failed." << std::endl;
422 return;
423 }
424 std::cout << "==========[test log] CreateCameraHost success." << std::endl;
425 }
426
427 OHOS::sptr<DemoCameraHostCallback> cameraHostCallback = new DemoCameraHostCallback();
428 OHOS::Camera::RetCode ret = cameraHost->SetCallback(cameraHostCallback);
429 if (ret != HDI::Camera::V1_0::NO_ERROR) {
430 std::cout << "==========[test log] SetCallback failed." << std::endl;
431 return;
432 } else {
433 std::cout << "==========[test log] SetCallback success." << std::endl;
434 }
435 }
436
GetCameraAbility()437 std::shared_ptr<CameraAbility> TestCameraBase::GetCameraAbility()
438 {
439 if (cameraDevice == nullptr) {
440 OHOS::Camera::RetCode ret = cameraHost->GetCameraIds(cameraIds);
441 if (ret != HDI::Camera::V1_0::NO_ERROR) {
442 std::cout << "==========[test log]GetCameraIds failed." << std::endl;
443 return ability;
444 } else {
445 std::cout << "==========[test log]GetCameraIds success." << std::endl;
446 }
447 if (cameraIds.size() == 0) {
448 std::cout << "==========[test log]camera device list is empty." << std::endl;
449 return ability;
450 }
451 if (cameraIds.size() > 0) {
452 ret = cameraHost->GetCameraAbility(cameraIds.back(), ability_);
453 if (ret != HDI::Camera::V1_0::NO_ERROR) {
454 std::cout << "==========[test log]GetCameraAbility failed, rc = " << rc << std::endl;
455 }
456 MetadataUtils::ConvertVecToMetadata(ability_, ability);
457 }
458 }
459 return ability;
460 }
461
GetCameraAbilityById(std::string cameraId)462 std::shared_ptr<CameraAbility> TestCameraBase::GetCameraAbilityById(std::string cameraId)
463 {
464 int ret = cameraHost->GetCameraAbility(cameraId, ability_);
465 if (ret != HDI::Camera::V1_0::NO_ERROR) {
466 CAMERA_LOGD("==========[test log]GetCameraAbility failed, ret = %{public}d", ret);
467 }
468 MetadataUtils::ConvertVecToMetadata(ability_, ability);
469 return ability;
470 }
471
OpenUsbCamera()472 void TestCameraBase::OpenUsbCamera()
473 {
474 if (cameraDevice == nullptr) {
475 cameraHost->GetCameraIds(cameraIds);
476 if (cameraIds.size() > 0) {
477 cameraHost->GetCameraAbility(cameraIds.back(), ability_);
478 MetadataUtils::ConvertVecToMetadata(ability_, ability);
479 const OHOS::sptr<DemoCameraDeviceCallback> callback = new DemoCameraDeviceCallback();
480 rc = (CamRetCode)cameraHost->OpenCamera(cameraIds.back(), callback, cameraDevice);
481 if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) {
482 std::cout << "OpenCamera failed, rc = " << rc << std::endl;
483 return;
484 }
485 std::cout << "OpenCamera success." << std::endl;
486 } else {
487 std::cout << "No usb camera plugged in" << std::endl;
488 GTEST_SKIP() << "No usb camera plugged in" << std::endl;
489 }
490 }
491 }
492
SelectOpenCamera(std::string cameraId)493 CamRetCode TestCameraBase::SelectOpenCamera(std::string cameraId)
494 {
495 cameraHost->GetCameraAbility(cameraId, ability_);
496 MetadataUtils::ConvertVecToMetadata(ability_, ability);
497 const OHOS::sptr<DemoCameraDeviceCallback> callback = new DemoCameraDeviceCallback();
498 rc = (CamRetCode)cameraHost->OpenCamera(cameraId, callback, cameraDevice);
499 if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) {
500 std::cout << "OpenCamera failed, rc = " << rc << std::endl;
501 return rc;
502 }
503 std::cout << "OpenCamera success." << std::endl;
504 return rc;
505 }
506
Close()507 void TestCameraBase::Close()
508 {
509 CAMERA_LOGD("cameraDevice->Close().");
510 if (cameraDevice != nullptr) {
511 cameraDevice->Close();
512 cameraDevice = nullptr;
513 }
514 }
515
OpenCamera()516 void TestCameraBase::OpenCamera()
517 {
518 if (cameraDevice == nullptr) {
519 cameraHost->GetCameraIds(cameraIds);
520 const OHOS::sptr<OHOS::Camera::CameraDeviceCallback> callback = new CameraDeviceCallback();
521 rc = (CamRetCode)cameraHost->OpenCamera(cameraIds.front(), callback, cameraDevice);
522 if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) {
523 std::cout << "==========[test log] OpenCamera failed, rc = " << rc << std::endl;
524 return;
525 }
526 std::cout << "==========[test log] OpenCamera success." << std::endl;
527 }
528 }
529
CalTime(struct timeval start,struct timeval end)530 float TestCameraBase::CalTime(struct timeval start, struct timeval end)
531 {
532 float timeUse = 0;
533 timeUse = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); // 1000000:time
534 return timeUse;
535 }
536
AchieveStreamOperator()537 void TestCameraBase::AchieveStreamOperator()
538 {
539 // Create and get streamOperator information
540 OHOS::sptr<DemoStreamOperatorCallback> streamOperatorCallback_ = new DemoStreamOperatorCallback();
541 rc = (CamRetCode)cameraDevice->GetStreamOperator(streamOperatorCallback_, streamOperator);
542 EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR);
543 if (rc == HDI::Camera::V1_0::NO_ERROR) {
544 CAMERA_LOGI("AchieveStreamOperator success.");
545 } else {
546 CAMERA_LOGE("AchieveStreamOperator fail, rc = %{public}d", rc);
547 }
548 }
549
StartStream(std::vector<StreamIntent> intents)550 void TestCameraBase::StartStream(std::vector<StreamIntent> intents)
551 {
552 for (auto& intent : intents) {
553 if (intent == PREVIEW) {
554 if (streamCustomerPreview_ == nullptr) {
555 streamCustomerPreview_ = std::make_shared<StreamCustomer>();
556 }
557 streamInfoPre.streamId_ = STREAM_ID_PREVIEW;
558 streamInfoPre.width_ = PREVIEW_WIDTH; // 640:picture width
559 streamInfoPre.height_ = PREVIEW_HEIGHT; // 480:picture height
560 streamInfoPre.format_ = PIXEL_FMT_RGBA_8888;
561 streamInfoPre.dataspace_ = 8; // 8:picture dataspace
562 streamInfoPre.intent_ = intent;
563 streamInfoPre.tunneledMode_ = 5; // 5:tunnel mode
564 streamInfoPre.bufferQueue_ = new BufferProducerSequenceable(streamCustomerPreview_->CreateProducer());
565 ASSERT_NE(streamInfoPre.bufferQueue_, nullptr);
566 streamInfoPre.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size
567 CAMERA_LOGD("preview success.");
568 std::vector<StreamInfo>().swap(streamInfos);
569 streamInfos.push_back(streamInfoPre);
570 } else if (intent == VIDEO) {
571 if (streamCustomerVideo_ == nullptr) {
572 streamCustomerVideo_ = std::make_shared<StreamCustomer>();
573 }
574 streamInfoVideo.streamId_ = STREAM_ID_VIDEO;
575 streamInfoVideo.width_ = VIDEO_WIDTH; // 1280:picture width
576 streamInfoVideo.height_ = VIDEO_HEIGHT; // 960:picture height
577 streamInfoVideo.format_ = PIXEL_FMT_YCRCB_420_SP;
578 streamInfoVideo.dataspace_ = 8; // 8:picture dataspace
579 streamInfoVideo.intent_ = intent;
580 streamInfoVideo.encodeType_ = ENCODE_TYPE_H264;
581 streamInfoVideo.tunneledMode_ = 5; // 5:tunnel mode
582 streamInfoVideo.bufferQueue_ = new BufferProducerSequenceable(streamCustomerVideo_->CreateProducer());
583 ASSERT_NE(streamInfoVideo.bufferQueue_, nullptr);
584 streamInfoVideo.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size
585 CAMERA_LOGD("video success.");
586 std::vector<StreamInfo>().swap(streamInfos);
587 streamInfos.push_back(streamInfoVideo);
588 } else if (intent == STILL_CAPTURE) {
589 if (streamCustomerCapture_ == nullptr) {
590 streamCustomerCapture_ = std::make_shared<StreamCustomer>();
591 }
592 streamInfoCapture.streamId_ = STREAM_ID_CAPTURE;
593 streamInfoCapture.width_ = CAPTURE_WIDTH; // 1280:picture width
594 streamInfoCapture.height_ = CAPTURE_HEIGHT; // 960:picture height
595 streamInfoCapture.format_ = PIXEL_FMT_RGBA_8888;
596 streamInfoCapture.dataspace_ = 8; // 8:picture dataspace
597 streamInfoCapture.intent_ = intent;
598 streamInfoCapture.encodeType_ = ENCODE_TYPE_JPEG;
599 streamInfoCapture.tunneledMode_ = 5; // 5:tunnel mode
600 streamInfoCapture.bufferQueue_ = new BufferProducerSequenceable(streamCustomerCapture_->CreateProducer());
601 ASSERT_NE(streamInfoCapture.bufferQueue_, nullptr);
602 streamInfoCapture.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size
603 CAMERA_LOGD("capture success.");
604 std::vector<StreamInfo>().swap(streamInfos);
605 streamInfos.push_back(streamInfoCapture);
606 } else if (intent == ANALYZE) {
607 if (streamCustomerAnalyze_ == nullptr) {
608 streamCustomerAnalyze_ = std::make_shared<StreamCustomer>();
609 }
610 streamInfoAnalyze.streamId_ = STREAM_ID_ANALYZE;
611 streamInfoAnalyze.width_ = ANALYZE_WIDTH; // 640:picture width
612 streamInfoAnalyze.height_ = ANALYZE_HEIGHT; // 480:picture height
613 streamInfoAnalyze.format_ = PIXEL_FMT_RGBA_8888;
614 streamInfoAnalyze.dataspace_ = 8; // 8:picture dataspace
615 streamInfoAnalyze.intent_ = intent;
616 streamInfoAnalyze.tunneledMode_ = 5; // 5:tunnel mode
617 streamInfoAnalyze.bufferQueue_ = new BufferProducerSequenceable(streamCustomerAnalyze_->CreateProducer());
618 ASSERT_NE(streamInfoAnalyze.bufferQueue_, nullptr);
619 streamInfoAnalyze.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size
620 CAMERA_LOGD("analyze success.");
621 std::vector<StreamInfo>().swap(streamInfos);
622 streamInfos.push_back(streamInfoAnalyze);
623 }
624 rc = (CamRetCode)streamOperator->CreateStreams(streamInfos);
625 EXPECT_EQ(false, rc != HDI::Camera::V1_0::NO_ERROR);
626 if (rc == HDI::Camera::V1_0::NO_ERROR) {
627 CAMERA_LOGI("CreateStreams success.");
628 } else {
629 CAMERA_LOGE("CreateStreams fail, rc = %{public}d", rc);
630 }
631
632 rc = (CamRetCode)streamOperator->CommitStreams(NORMAL, ability_);
633 EXPECT_EQ(false, rc != HDI::Camera::V1_0::NO_ERROR);
634 if (rc == HDI::Camera::V1_0::NO_ERROR) {
635 CAMERA_LOGI("CommitStreams success.");
636 } else {
637 CAMERA_LOGE("CommitStreams fail, rc = %{public}d", rc);
638 }
639 }
640 }
641
StartCapture(int streamId,int captureId,bool shutterCallback,bool isStreaming)642 void TestCameraBase::StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming)
643 {
644 // Get preview
645 captureInfo.streamIds_ = {streamId};
646 captureInfo.captureSetting_ = ability_;
647 captureInfo.enableShutterCallback_ = shutterCallback;
648 rc = (CamRetCode)streamOperator->Capture(captureId, captureInfo, isStreaming);
649 EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR);
650 if (rc == HDI::Camera::V1_0::NO_ERROR) {
651 CAMERA_LOGI("check Capture: Capture success, captureId = %{public}d", captureId);
652 } else {
653 CAMERA_LOGE("check Capture: Capture fail, rc = %{public}d, captureId = %{public}d", rc, captureId);
654 }
655 if (captureId == CAPTURE_ID_PREVIEW) {
656 streamCustomerPreview_->ReceiveFrameOn(nullptr);
657 } else if (captureId == CAPTURE_ID_CAPTURE) {
658 streamCustomerCapture_->ReceiveFrameOn([this](const unsigned char *addr, const uint32_t size) {
659 StoreImage(addr, size);
660 });
661 } else if (captureId == CAPTURE_ID_VIDEO) {
662 streamCustomerVideo_->ReceiveFrameOn([this](const unsigned char *addr, const uint32_t size) {
663 StoreVideo(addr, size);
664 });
665 } else if (captureId == CAPTURE_ID_ANALYZE) {
666 streamCustomerAnalyze_->ReceiveFrameOn([this](const unsigned char *addr, const uint32_t size) {
667 PrintFaceDetectInfo(addr, size);
668 });
669 }
670 sleep(2); // 2:sleep two second
671 }
672
StopStream(std::vector<int> & captureIds,std::vector<int> & streamIds)673 void TestCameraBase::StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds)
674 {
675 constexpr uint32_t timeForWaitCancelCapture = 2;
676 sleep(timeForWaitCancelCapture);
677 if (captureIds.size() > 0) {
678 for (const auto &captureId : captureIds) {
679 if (captureId == CAPTURE_ID_PREVIEW) {
680 streamCustomerPreview_->ReceiveFrameOff();
681 } else if (captureId == CAPTURE_ID_CAPTURE) {
682 streamCustomerCapture_->ReceiveFrameOff();
683 } else if (captureId == CAPTURE_ID_VIDEO) {
684 streamCustomerVideo_->ReceiveFrameOff();
685 sleep(1);
686 CloseFd();
687 } else if (captureId == CAPTURE_ID_ANALYZE) {
688 streamCustomerAnalyze_->ReceiveFrameOff();
689 }
690 }
691 for (const auto &captureId : captureIds) {
692 CAMERA_LOGI("check Capture: CancelCapture success, captureId = %{public}d", captureId);
693 rc = (CamRetCode)streamOperator->CancelCapture(captureId);
694 sleep(timeForWaitCancelCapture);
695 EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR);
696 if (rc == HDI::Camera::V1_0::NO_ERROR) {
697 CAMERA_LOGI("check Capture: CancelCapture success, captureId = %{public}d", captureId);
698 } else {
699 CAMERA_LOGE("check Capture: CancelCapture fail, rc = %{public}d, captureId = %{public}d",
700 rc, captureId);
701 }
702 }
703 }
704 sleep(1);
705 if (streamIds.size() > 0) {
706 // release stream
707 rc = (CamRetCode)streamOperator->ReleaseStreams(streamIds);
708 EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR);
709 if (rc == HDI::Camera::V1_0::NO_ERROR) {
710 CAMERA_LOGI("check Capture: ReleaseStreams success.");
711 } else {
712 CAMERA_LOGE("check Capture: ReleaseStreams fail, rc = %{public}d, streamIds = %{public}d",
713 rc, streamIds.front());
714 }
715 }
716 }
717
PrintStabiliInfo(const std::vector<uint8_t> & result)718 void DemoCameraDeviceCallback::PrintStabiliInfo(const std::vector<uint8_t>& result)
719 {
720 std::shared_ptr<CameraMetadata> metaData;
721 MetadataUtils::ConvertVecToMetadata(result, metaData);
722
723 if (metaData == nullptr) {
724 CAMERA_LOGE("TestCameraBase: result is null");
725 return;
726 }
727 common_metadata_header_t* data = metaData->get();
728 if (data == nullptr) {
729 CAMERA_LOGE("TestCameraBase: data is null");
730 return;
731 }
732 uint8_t videoStabiliMode;
733 camera_metadata_item_t entry;
734 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_VIDEO_STABILIZATION_MODE, &entry);
735 if (ret != 0) {
736 CAMERA_LOGE("demo test: get OHOS_CONTROL_EXPOSURE_MODE error\n");
737 return;
738 }
739 videoStabiliMode = *(entry.data.u8);
740 CAMERA_LOGI("videoStabiliMode: %{public}d", static_cast<int>(videoStabiliMode));
741 }
742
PrintFpsInfo(const std::vector<uint8_t> & result)743 void DemoCameraDeviceCallback::PrintFpsInfo(const std::vector<uint8_t>& result)
744 {
745 std::shared_ptr<CameraMetadata> metaData;
746 MetadataUtils::ConvertVecToMetadata(result, metaData);
747
748 if (metaData == nullptr) {
749 CAMERA_LOGE("TestCameraBase: result is null");
750 return;
751 }
752 common_metadata_header_t* data = metaData->get();
753 if (data == nullptr) {
754 CAMERA_LOGE("TestCameraBase: data is null");
755 return;
756 }
757 std::vector<int32_t> fpsRange;
758 camera_metadata_item_t entry;
759 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_FPS_RANGES, &entry);
760 if (ret != 0) {
761 CAMERA_LOGE("demo test: get OHOS_CONTROL_EXPOSURE_MODE error\n");
762 return;
763 }
764
765 for (int i = 0; i < entry.count; i++) {
766 fpsRange.push_back(*(entry.data.i32 + i));
767 }
768 CAMERA_LOGI("PrintFpsInfo fpsRange: [%{public}d, %{public}d]", fpsRange[0], fpsRange[1]);
769 }
770
771 #ifndef CAMERA_BUILT_ON_OHOS_LITE
OnError(ErrorType type,int32_t errorCode)772 int32_t DemoCameraDeviceCallback::OnError(ErrorType type, int32_t errorCode)
773 {
774 CAMERA_LOGI("demo test: OnError type : %{public}d, errorMsg : %{public}d", type, errorCode);
775 }
776
OnResult(uint64_t timestamp,const std::vector<uint8_t> & result)777 int32_t DemoCameraDeviceCallback::OnResult(uint64_t timestamp, const std::vector<uint8_t>& result)
778 {
779 CAMERA_LOGI("%{public}s, enter.", __func__);
780 PrintStabiliInfo(result);
781 PrintFpsInfo(result);
782 DealCameraMetadata(result);
783 return RC_OK;
784 }
785
OnCameraStatus(const std::string & cameraId,CameraStatus status)786 int32_t DemoCameraHostCallback::OnCameraStatus(const std::string& cameraId, CameraStatus status)
787 {
788 CAMERA_LOGI("%{public}s, enter.", __func__);
789 std::cout << "OnCameraStatus, enter, cameraId = " << cameraId << ", status = " << status << std::endl;
790 return RC_OK;
791 }
792
OnFlashlightStatus(const std::string & cameraId,FlashlightStatus status)793 int32_t DemoCameraHostCallback::OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status)
794 {
795 CAMERA_LOGI("%{public}s, enter. cameraId = %s, status = %d",
796 __func__, cameraId.c_str(), static_cast<int>(status));
797 return RC_OK;
798 }
799
OnCameraEvent(const std::string & cameraId,CameraEvent event)800 int32_t DemoCameraHostCallback::OnCameraEvent(const std::string& cameraId, CameraEvent event)
801 {
802 CAMERA_LOGI("%{public}s, enter. cameraId = %s, event = %d",
803 __func__, cameraId.c_str(), static_cast<int>(event));
804 std::cout << "OnCameraEvent, enter, cameraId = " << cameraId << ", event = " << event<< std::endl;
805 return RC_OK;
806 }
807
OnCaptureStarted(int32_t captureId,const std::vector<int32_t> & streamIds)808 int32_t DemoStreamOperatorCallback::OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds)
809 {
810 CAMERA_LOGI("%{public}s, enter.", __func__);
811 return RC_OK;
812 }
813
OnCaptureEnded(int32_t captureId,const std::vector<CaptureEndedInfo> & infos)814 int32_t DemoStreamOperatorCallback::OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos)
815 {
816 CAMERA_LOGI("%{public}s, enter.", __func__);
817 return RC_OK;
818 }
819
DealCameraMetadata(const std::vector<uint8_t> & settings)820 void DemoCameraDeviceCallback::DealCameraMetadata(const std::vector<uint8_t> &settings)
821 {
822 std::shared_ptr<CameraMetadata> result;
823 MetadataUtils::ConvertVecToMetadata(settings, result);
824 if (result == nullptr) {
825 CAMERA_LOGE("TestCameraBase: result is null");
826 return;
827 }
828 common_metadata_header_t *data = result->get();
829 if (data == nullptr) {
830 CAMERA_LOGE("data is null");
831 return;
832 }
833 for (auto it = DATA_BASE.cbegin(); it != DATA_BASE.cend(); it++) {
834 std::string st = {};
835 st = MetadataItemDump(data, *it);
836 CAMERA_LOGI("%{publid}s", st.c_str());
837 }
838 }
839
OnCaptureError(int32_t captureId,const std::vector<CaptureErrorInfo> & infos)840 int32_t DemoStreamOperatorCallback::OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos)
841 {
842 CAMERA_LOGI("%{public}s, enter.", __func__);
843 return RC_OK;
844 }
845
OnFrameShutter(int32_t captureId,const std::vector<int32_t> & streamIds,uint64_t timestamp)846 int32_t DemoStreamOperatorCallback::OnFrameShutter(int32_t captureId,
847 const std::vector<int32_t>& streamIds, uint64_t timestamp)
848 {
849 CAMERA_LOGI("%{public}s, enter.", __func__);
850 return RC_OK;
851 }
852
853 #endif
854