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