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