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" 16using namespace std; 17 18const 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 36TestCameraBase::TestCameraBase() 37{ 38} 39 40uint64_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 48void 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 84void 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 113void 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 133void TestCameraBase::CloseFd() 134{ 135 close(videoFd_); 136 videoFd_ = -1; 137} 138 139void 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 188int32_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 214int 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 223unsigned 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 236void 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 271OHOS::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 303void 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, y, j; 310 int y0, u, v, r, g, b; 311 int32_t location = 0; 312 int xpos = (vinfo_.xres - width) / 2; 313 int ypos = (vinfo_.yres - height) / 2; 314 int yPos, uPos, vPos; 315 int yPosIncrement, uPosIncrement, vPosIncrement; 316 317 yPos = 0; // 0:Pixel initial value 318 uPos = 1; // 1:Pixel initial value 319 vPos = 3; // 3:Pixel initial value 320 yPosIncrement = 2; // 2:yPos increase value 321 uPosIncrement = 4; // 4:uPos increase value 322 vPosIncrement = 4; // 4:vPos increase value 323 324 for (y = ypos; y < (height + ypos); y++) { 325 for (j = 0, x = xpos; j < width; j++, x++) { 326 location = (x + vinfo_.xoffset) * (vinfo_.bits_per_pixel / 8) + // 8: The bytes for each time 327 (y + vinfo_.yoffset) * finfo_.line_length; // add one y number of rows at a time 328 329 y0 = in[yPos]; 330 u = in[uPos] - 128; // 128:display size 331 v = in[vPos] - 128; // 128:display size 332 333 r = RANGE_LIMIT(y0 + v + ((v * 103) >> 8)); // 103,8:display range 334 g = RANGE_LIMIT(y0 - ((u * 88) >> 8) - ((v * 183) >> 8)); // 88,8,183:display range 335 b = RANGE_LIMIT(y0 + u + ((u * 198) >> 8)); // 198,8:display range 336 337 fbp[location + 1] = ((r & 0xF8) | (g >> 5)); // 5:display range 338 fbp[location + 0] = (((g & 0x1C) << 3) | (b >> 3)); // 3:display range 339 340 yPos += yPosIncrement; 341 342 if (j & 0x01) { 343 uPos += uPosIncrement; 344 vPos += vPosIncrement; 345 } 346 } 347 348 yPos = 0; // 0:Pixel initial value 349 uPos = 1; // 1:Pixel initial value 350 vPos = 3; // 3:Pixel initial value 351 in += istride; // add one y number of rows at a time 352 } 353} 354 355void TestCameraBase::LcdDrawScreen(unsigned char* displayBuf, unsigned char* addr) 356{ 357 ProcessImage(addr, displayBuf); 358} 359 360void TestCameraBase::BufferCallback(unsigned char* addr, int choice) 361{ 362 if (choice == PREVIEW_MODE) { 363 LcdDrawScreen(displayBuf_, addr); 364 return; 365 } else { 366 LcdDrawScreen(displayBuf_, addr); 367 std::cout << "==========[test log] capture start saveYuv......" << std::endl; 368 SaveYUV("capture", reinterpret_cast<unsigned char*>(addr), bufSize_); 369 std::cout << "==========[test log] capture end saveYuv......" << std::endl; 370 return; 371 } 372} 373 374void TestCameraBase::Init() 375{ 376 CAMERA_LOGD("TestCameraBase::Init()."); 377 if (cameraHost == nullptr) { 378 constexpr const char *demoServiceName = "camera_service"; 379 cameraHost = ICameraHost::Get(demoServiceName, false); 380 CAMERA_LOGI("Camera::CameraHost::CreateCameraHost()"); 381 if (cameraHost == nullptr) { 382 CAMERA_LOGE("CreateCameraHost failed."); 383 return; 384 } 385 CAMERA_LOGI("CreateCameraHost success."); 386 } 387 388 OHOS::sptr<DemoCameraHostCallback> cameraHostCallback = new DemoCameraHostCallback(); 389 OHOS::Camera::RetCode ret = cameraHost->SetCallback(cameraHostCallback); 390 if (ret != HDI::Camera::V1_0::NO_ERROR) { 391 CAMERA_LOGE("SetCallback failed."); 392 return; 393 } else { 394 CAMERA_LOGI("SetCallback success."); 395 } 396 397 if (cameraDevice == nullptr) { 398 cameraHost->GetCameraIds(cameraIds); 399 cameraHost->GetCameraAbility(cameraIds.front(), ability_); 400 MetadataUtils::ConvertVecToMetadata(ability_, ability); 401 const OHOS::sptr<DemoCameraDeviceCallback> callback = new DemoCameraDeviceCallback(); 402 rc = (CamRetCode)cameraHost->OpenCamera(cameraIds.front(), callback, cameraDevice); 403 if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) { 404 CAMERA_LOGE("OpenCamera failed, rc = %{public}d", rc); 405 return; 406 } 407 CAMERA_LOGI("OpenCamera success."); 408 } 409} 410 411void TestCameraBase::UsbInit() 412{ 413 if (cameraHost == nullptr) { 414 constexpr const char *demoServiceName = "camera_service"; 415 cameraHost = ICameraHost::Get(demoServiceName, false); 416 if (cameraHost == nullptr) { 417 std::cout << "==========[test log] CreateCameraHost failed." << std::endl; 418 return; 419 } 420 std::cout << "==========[test log] CreateCameraHost success." << std::endl; 421 } 422 423 OHOS::sptr<DemoCameraHostCallback> cameraHostCallback = new DemoCameraHostCallback(); 424 OHOS::Camera::RetCode ret = cameraHost->SetCallback(cameraHostCallback); 425 if (ret != HDI::Camera::V1_0::NO_ERROR) { 426 std::cout << "==========[test log] SetCallback failed." << std::endl; 427 return; 428 } else { 429 std::cout << "==========[test log] SetCallback success." << std::endl; 430 } 431} 432 433std::shared_ptr<CameraAbility> TestCameraBase::GetCameraAbility() 434{ 435 if (cameraDevice == nullptr) { 436 OHOS::Camera::RetCode ret = cameraHost->GetCameraIds(cameraIds); 437 if (ret != HDI::Camera::V1_0::NO_ERROR) { 438 std::cout << "==========[test log]GetCameraIds failed." << std::endl; 439 return ability; 440 } else { 441 std::cout << "==========[test log]GetCameraIds success." << std::endl; 442 } 443 if (cameraIds.size() == 0) { 444 std::cout << "==========[test log]camera device list is empty." << std::endl; 445 return ability; 446 } 447 if (cameraIds.size() > 0) { 448 ret = cameraHost->GetCameraAbility(cameraIds.back(), ability_); 449 if (ret != HDI::Camera::V1_0::NO_ERROR) { 450 std::cout << "==========[test log]GetCameraAbility failed, rc = " << rc << std::endl; 451 } 452 MetadataUtils::ConvertVecToMetadata(ability_, ability); 453 } 454 } 455 return ability; 456} 457 458std::shared_ptr<CameraAbility> TestCameraBase::GetCameraAbilityById(std::string cameraId) 459{ 460 int ret = cameraHost->GetCameraAbility(cameraId, ability_); 461 if (ret != HDI::Camera::V1_0::NO_ERROR) { 462 CAMERA_LOGD("==========[test log]GetCameraAbility failed, ret = %{public}d", ret); 463 } 464 MetadataUtils::ConvertVecToMetadata(ability_, ability); 465 return ability; 466} 467 468void TestCameraBase::OpenUsbCamera() 469{ 470 if (cameraDevice == nullptr) { 471 cameraHost->GetCameraIds(cameraIds); 472 if (cameraIds.size() > 0) { 473 cameraHost->GetCameraAbility(cameraIds.back(), ability_); 474 MetadataUtils::ConvertVecToMetadata(ability_, ability); 475 const OHOS::sptr<DemoCameraDeviceCallback> callback = new DemoCameraDeviceCallback(); 476 rc = (CamRetCode)cameraHost->OpenCamera(cameraIds.back(), callback, cameraDevice); 477 if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) { 478 std::cout << "OpenCamera failed, rc = " << rc << std::endl; 479 return; 480 } 481 std::cout << "OpenCamera success." << std::endl; 482 } else { 483 std::cout << "No usb camera plugged in" << std::endl; 484 GTEST_SKIP() << "No usb camera plugged in" << std::endl; 485 } 486 } 487} 488 489CamRetCode TestCameraBase::SelectOpenCamera(std::string cameraId) 490{ 491 cameraHost->GetCameraAbility(cameraId, ability_); 492 MetadataUtils::ConvertVecToMetadata(ability_, ability); 493 const OHOS::sptr<DemoCameraDeviceCallback> callback = new DemoCameraDeviceCallback(); 494 rc = (CamRetCode)cameraHost->OpenCamera(cameraId, callback, cameraDevice); 495 if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) { 496 std::cout << "OpenCamera failed, rc = " << rc << std::endl; 497 return rc; 498 } 499 std::cout << "OpenCamera success." << std::endl; 500 return rc; 501} 502 503void TestCameraBase::Close() 504{ 505 CAMERA_LOGD("cameraDevice->Close()."); 506 if (cameraDevice != nullptr) { 507 cameraDevice->Close(); 508 cameraDevice = nullptr; 509 } 510} 511 512void TestCameraBase::OpenCamera() 513{ 514 if (cameraDevice == nullptr) { 515 cameraHost->GetCameraIds(cameraIds); 516 const OHOS::sptr<OHOS::Camera::CameraDeviceCallback> callback = new CameraDeviceCallback(); 517 rc = (CamRetCode)cameraHost->OpenCamera(cameraIds.front(), callback, cameraDevice); 518 if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) { 519 std::cout << "==========[test log] OpenCamera failed, rc = " << rc << std::endl; 520 return; 521 } 522 std::cout << "==========[test log] OpenCamera success." << std::endl; 523 } 524} 525 526float TestCameraBase::CalTime(struct timeval start, struct timeval end) 527{ 528 float timeUse = 0; 529 timeUse = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); // 1000000:time 530 return timeUse; 531} 532 533void TestCameraBase::AchieveStreamOperator() 534{ 535 // Create and get streamOperator information 536 OHOS::sptr<DemoStreamOperatorCallback> streamOperatorCallback_ = new DemoStreamOperatorCallback(); 537 rc = (CamRetCode)cameraDevice->GetStreamOperator(streamOperatorCallback_, streamOperator); 538 EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR); 539 if (rc == HDI::Camera::V1_0::NO_ERROR) { 540 CAMERA_LOGI("AchieveStreamOperator success."); 541 } else { 542 CAMERA_LOGE("AchieveStreamOperator fail, rc = %{public}d", rc); 543 } 544} 545 546void TestCameraBase::StartStream(std::vector<StreamIntent> intents) 547{ 548 for (auto& intent : intents) { 549 if (intent == PREVIEW) { 550 if (streamCustomerPreview_ == nullptr) { 551 streamCustomerPreview_ = std::make_shared<StreamCustomer>(); 552 } 553 streamInfoPre.streamId_ = STREAM_ID_PREVIEW; 554 streamInfoPre.width_ = PREVIEW_WIDTH; // 640:picture width 555 streamInfoPre.height_ = PREVIEW_HEIGHT; // 480:picture height 556 streamInfoPre.format_ = PIXEL_FMT_RGBA_8888; 557 streamInfoPre.dataspace_ = 8; // 8:picture dataspace 558 streamInfoPre.intent_ = intent; 559 streamInfoPre.tunneledMode_ = 5; // 5:tunnel mode 560 streamInfoPre.bufferQueue_ = new BufferProducerSequenceable(streamCustomerPreview_->CreateProducer()); 561 ASSERT_NE(streamInfoPre.bufferQueue_, nullptr); 562 streamInfoPre.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size 563 CAMERA_LOGD("preview success."); 564 std::vector<StreamInfo>().swap(streamInfos); 565 streamInfos.push_back(streamInfoPre); 566 } else if (intent == VIDEO) { 567 if (streamCustomerVideo_ == nullptr) { 568 streamCustomerVideo_ = std::make_shared<StreamCustomer>(); 569 } 570 streamInfoVideo.streamId_ = STREAM_ID_VIDEO; 571 streamInfoVideo.width_ = VIDEO_WIDTH; // 1280:picture width 572 streamInfoVideo.height_ = VIDEO_HEIGHT; // 960:picture height 573 streamInfoVideo.format_ = PIXEL_FMT_YCRCB_420_SP; 574 streamInfoVideo.dataspace_ = 8; // 8:picture dataspace 575 streamInfoVideo.intent_ = intent; 576 streamInfoVideo.encodeType_ = ENCODE_TYPE_H264; 577 streamInfoVideo.tunneledMode_ = 5; // 5:tunnel mode 578 streamInfoVideo.bufferQueue_ = new BufferProducerSequenceable(streamCustomerVideo_->CreateProducer()); 579 ASSERT_NE(streamInfoVideo.bufferQueue_, nullptr); 580 streamInfoVideo.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size 581 CAMERA_LOGD("video success."); 582 std::vector<StreamInfo>().swap(streamInfos); 583 streamInfos.push_back(streamInfoVideo); 584 } else if (intent == STILL_CAPTURE) { 585 if (streamCustomerCapture_ == nullptr) { 586 streamCustomerCapture_ = std::make_shared<StreamCustomer>(); 587 } 588 streamInfoCapture.streamId_ = STREAM_ID_CAPTURE; 589 streamInfoCapture.width_ = CAPTURE_WIDTH; // 1280:picture width 590 streamInfoCapture.height_ = CAPTURE_HEIGHT; // 960:picture height 591 streamInfoCapture.format_ = PIXEL_FMT_RGBA_8888; 592 streamInfoCapture.dataspace_ = 8; // 8:picture dataspace 593 streamInfoCapture.intent_ = intent; 594 streamInfoCapture.encodeType_ = ENCODE_TYPE_JPEG; 595 streamInfoCapture.tunneledMode_ = 5; // 5:tunnel mode 596 streamInfoCapture.bufferQueue_ = new BufferProducerSequenceable(streamCustomerCapture_->CreateProducer()); 597 ASSERT_NE(streamInfoCapture.bufferQueue_, nullptr); 598 streamInfoCapture.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size 599 CAMERA_LOGD("capture success."); 600 std::vector<StreamInfo>().swap(streamInfos); 601 streamInfos.push_back(streamInfoCapture); 602 } else if (intent == ANALYZE) { 603 if (streamCustomerAnalyze_ == nullptr) { 604 streamCustomerAnalyze_ = std::make_shared<StreamCustomer>(); 605 } 606 streamInfoAnalyze.streamId_ = STREAM_ID_ANALYZE; 607 streamInfoAnalyze.width_ = ANALYZE_WIDTH; // 640:picture width 608 streamInfoAnalyze.height_ = ANALYZE_HEIGHT; // 480:picture height 609 streamInfoAnalyze.format_ = PIXEL_FMT_RGBA_8888; 610 streamInfoAnalyze.dataspace_ = 8; // 8:picture dataspace 611 streamInfoAnalyze.intent_ = intent; 612 streamInfoAnalyze.tunneledMode_ = 5; // 5:tunnel mode 613 streamInfoAnalyze.bufferQueue_ = new BufferProducerSequenceable(streamCustomerAnalyze_->CreateProducer()); 614 ASSERT_NE(streamInfoAnalyze.bufferQueue_, nullptr); 615 streamInfoAnalyze.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size 616 CAMERA_LOGD("analyze success."); 617 std::vector<StreamInfo>().swap(streamInfos); 618 streamInfos.push_back(streamInfoAnalyze); 619 } 620 rc = (CamRetCode)streamOperator->CreateStreams(streamInfos); 621 EXPECT_EQ(false, rc != HDI::Camera::V1_0::NO_ERROR); 622 if (rc == HDI::Camera::V1_0::NO_ERROR) { 623 CAMERA_LOGI("CreateStreams success."); 624 } else { 625 CAMERA_LOGE("CreateStreams fail, rc = %{public}d", rc); 626 } 627 628 rc = (CamRetCode)streamOperator->CommitStreams(NORMAL, ability_); 629 EXPECT_EQ(false, rc != HDI::Camera::V1_0::NO_ERROR); 630 if (rc == HDI::Camera::V1_0::NO_ERROR) { 631 CAMERA_LOGI("CommitStreams success."); 632 } else { 633 CAMERA_LOGE("CommitStreams fail, rc = %{public}d", rc); 634 } 635 } 636} 637 638void TestCameraBase::StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming) 639{ 640 // Get preview 641 captureInfo.streamIds_ = {streamId}; 642 captureInfo.captureSetting_ = ability_; 643 captureInfo.enableShutterCallback_ = shutterCallback; 644 rc = (CamRetCode)streamOperator->Capture(captureId, captureInfo, isStreaming); 645 EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR); 646 if (rc == HDI::Camera::V1_0::NO_ERROR) { 647 CAMERA_LOGI("check Capture: Capture success, captureId = %{public}d", captureId); 648 } else { 649 CAMERA_LOGE("check Capture: Capture fail, rc = %{public}d, captureId = %{public}d", rc, captureId); 650 } 651 if (captureId == CAPTURE_ID_PREVIEW) { 652 streamCustomerPreview_->ReceiveFrameOn(nullptr); 653 } else if (captureId == CAPTURE_ID_CAPTURE) { 654 streamCustomerCapture_->ReceiveFrameOn([this](const unsigned char *addr, const uint32_t size) { 655 StoreImage(addr, size); 656 }); 657 } else if (captureId == CAPTURE_ID_VIDEO) { 658 streamCustomerVideo_->ReceiveFrameOn([this](const unsigned char *addr, const uint32_t size) { 659 StoreVideo(addr, size); 660 }); 661 } else if (captureId == CAPTURE_ID_ANALYZE) { 662 streamCustomerAnalyze_->ReceiveFrameOn([this](const unsigned char *addr, const uint32_t size) { 663 PrintFaceDetectInfo(addr, size); 664 }); 665 } 666 sleep(2); // 2:sleep two second 667} 668 669void TestCameraBase::StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds) 670{ 671 constexpr uint32_t timeForWaitCancelCapture = 2; 672 sleep(timeForWaitCancelCapture); 673 if (captureIds.size() > 0) { 674 for (const auto &captureId : captureIds) { 675 if (captureId == CAPTURE_ID_PREVIEW) { 676 streamCustomerPreview_->ReceiveFrameOff(); 677 } else if (captureId == CAPTURE_ID_CAPTURE) { 678 streamCustomerCapture_->ReceiveFrameOff(); 679 } else if (captureId == CAPTURE_ID_VIDEO) { 680 streamCustomerVideo_->ReceiveFrameOff(); 681 sleep(1); 682 CloseFd(); 683 } else if (captureId == CAPTURE_ID_ANALYZE) { 684 streamCustomerAnalyze_->ReceiveFrameOff(); 685 } 686 } 687 for (const auto &captureId : captureIds) { 688 CAMERA_LOGI("check Capture: CancelCapture success, captureId = %{public}d", captureId); 689 rc = (CamRetCode)streamOperator->CancelCapture(captureId); 690 sleep(timeForWaitCancelCapture); 691 EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR); 692 if (rc == HDI::Camera::V1_0::NO_ERROR) { 693 CAMERA_LOGI("check Capture: CancelCapture success, captureId = %{public}d", captureId); 694 } else { 695 CAMERA_LOGE("check Capture: CancelCapture fail, rc = %{public}d, captureId = %{public}d", 696 rc, captureId); 697 } 698 } 699 } 700 sleep(1); 701 if (streamIds.size() > 0) { 702 // release stream 703 rc = (CamRetCode)streamOperator->ReleaseStreams(streamIds); 704 EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR); 705 if (rc == HDI::Camera::V1_0::NO_ERROR) { 706 CAMERA_LOGI("check Capture: ReleaseStreams success."); 707 } else { 708 CAMERA_LOGE("check Capture: ReleaseStreams fail, rc = %{public}d, streamIds = %{public}d", 709 rc, streamIds.front()); 710 } 711 } 712} 713 714void DemoCameraDeviceCallback::PrintStabiliInfo(const std::vector<uint8_t>& result) 715{ 716 std::shared_ptr<CameraMetadata> metaData; 717 MetadataUtils::ConvertVecToMetadata(result, metaData); 718 719 if (metaData == nullptr) { 720 CAMERA_LOGE("TestCameraBase: result is null"); 721 return; 722 } 723 common_metadata_header_t* data = metaData->get(); 724 if (data == nullptr) { 725 CAMERA_LOGE("TestCameraBase: data is null"); 726 return; 727 } 728 uint8_t videoStabiliMode; 729 camera_metadata_item_t entry; 730 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_VIDEO_STABILIZATION_MODE, &entry); 731 if (ret != 0) { 732 CAMERA_LOGE("demo test: get OHOS_CONTROL_EXPOSURE_MODE error\n"); 733 return; 734 } 735 videoStabiliMode = *(entry.data.u8); 736 CAMERA_LOGI("videoStabiliMode: %{public}d", static_cast<int>(videoStabiliMode)); 737} 738 739void DemoCameraDeviceCallback::PrintFpsInfo(const std::vector<uint8_t>& result) 740{ 741 std::shared_ptr<CameraMetadata> metaData; 742 MetadataUtils::ConvertVecToMetadata(result, metaData); 743 744 if (metaData == nullptr) { 745 CAMERA_LOGE("TestCameraBase: result is null"); 746 return; 747 } 748 common_metadata_header_t* data = metaData->get(); 749 if (data == nullptr) { 750 CAMERA_LOGE("TestCameraBase: data is null"); 751 return; 752 } 753 std::vector<int32_t> fpsRange; 754 camera_metadata_item_t entry; 755 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_FPS_RANGES, &entry); 756 if (ret != 0) { 757 CAMERA_LOGE("demo test: get OHOS_CONTROL_EXPOSURE_MODE error\n"); 758 return; 759 } 760 761 for (int i = 0; i < entry.count; i++) { 762 fpsRange.push_back(*(entry.data.i32 + i)); 763 } 764 CAMERA_LOGI("PrintFpsInfo fpsRange: [%{public}d, %{public}d]", fpsRange[0], fpsRange[1]); 765} 766 767#ifndef CAMERA_BUILT_ON_OHOS_LITE 768int32_t DemoCameraDeviceCallback::OnError(ErrorType type, int32_t errorCode) 769{ 770 CAMERA_LOGI("demo test: OnError type : %{public}d, errorMsg : %{public}d", type, errorCode); 771} 772 773int32_t DemoCameraDeviceCallback::OnResult(uint64_t timestamp, const std::vector<uint8_t>& result) 774{ 775 CAMERA_LOGI("%{public}s, enter.", __func__); 776 PrintStabiliInfo(result); 777 PrintFpsInfo(result); 778 DealCameraMetadata(result); 779 return RC_OK; 780} 781 782int32_t DemoCameraHostCallback::OnCameraStatus(const std::string& cameraId, CameraStatus status) 783{ 784 CAMERA_LOGI("%{public}s, enter.", __func__); 785 std::cout << "OnCameraStatus, enter, cameraId = " << cameraId << ", status = " << status << std::endl; 786 return RC_OK; 787} 788 789int32_t DemoCameraHostCallback::OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) 790{ 791 CAMERA_LOGI("%{public}s, enter. cameraId = %s, status = %d", 792 __func__, cameraId.c_str(), static_cast<int>(status)); 793 return RC_OK; 794} 795 796int32_t DemoCameraHostCallback::OnCameraEvent(const std::string& cameraId, CameraEvent event) 797{ 798 CAMERA_LOGI("%{public}s, enter. cameraId = %s, event = %d", 799 __func__, cameraId.c_str(), static_cast<int>(event)); 800 std::cout << "OnCameraEvent, enter, cameraId = " << cameraId << ", event = " << event<< std::endl; 801 return RC_OK; 802} 803 804int32_t DemoStreamOperatorCallback::OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds) 805{ 806 CAMERA_LOGI("%{public}s, enter.", __func__); 807 return RC_OK; 808} 809 810int32_t DemoStreamOperatorCallback::OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos) 811{ 812 CAMERA_LOGI("%{public}s, enter.", __func__); 813 return RC_OK; 814} 815 816void DemoCameraDeviceCallback::DealCameraMetadata(const std::vector<uint8_t> &settings) 817{ 818 std::shared_ptr<CameraMetadata> result; 819 MetadataUtils::ConvertVecToMetadata(settings, result); 820 if (result == nullptr) { 821 CAMERA_LOGE("TestCameraBase: result is null"); 822 return; 823 } 824 common_metadata_header_t *data = result->get(); 825 if (data == nullptr) { 826 CAMERA_LOGE("data is null"); 827 return; 828 } 829 for (auto it = DATA_BASE.cbegin(); it != DATA_BASE.cend(); it++) { 830 std::string st = {}; 831 st = MetadataItemDump(data, *it); 832 CAMERA_LOGI("%{publid}s", st.c_str()); 833 } 834} 835 836int32_t DemoStreamOperatorCallback::OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos) 837{ 838 CAMERA_LOGI("%{public}s, enter.", __func__); 839 return RC_OK; 840} 841 842int32_t DemoStreamOperatorCallback::OnFrameShutter(int32_t captureId, 843 const std::vector<int32_t>& streamIds, uint64_t timestamp) 844{ 845 CAMERA_LOGI("%{public}s, enter.", __func__); 846 return RC_OK; 847} 848 849#endif 850