1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci#include "test_camera_base.h" 16094332d3Sopenharmony_ciusing namespace std; 17094332d3Sopenharmony_ci 18094332d3Sopenharmony_ciconst std::vector<int32_t> DATA_BASE = { 19094332d3Sopenharmony_ci OHOS_CAMERA_STREAM_ID, 20094332d3Sopenharmony_ci OHOS_SENSOR_COLOR_CORRECTION_GAINS, 21094332d3Sopenharmony_ci OHOS_SENSOR_EXPOSURE_TIME, 22094332d3Sopenharmony_ci OHOS_CONTROL_EXPOSURE_MODE, 23094332d3Sopenharmony_ci OHOS_CONTROL_AE_EXPOSURE_COMPENSATION, 24094332d3Sopenharmony_ci OHOS_CONTROL_FOCUS_MODE, 25094332d3Sopenharmony_ci OHOS_CONTROL_METER_MODE, 26094332d3Sopenharmony_ci OHOS_CONTROL_FLASH_MODE, 27094332d3Sopenharmony_ci OHOS_CONTROL_FPS_RANGES, 28094332d3Sopenharmony_ci OHOS_CONTROL_AWB_MODE, 29094332d3Sopenharmony_ci OHOS_CONTROL_AF_REGIONS, 30094332d3Sopenharmony_ci OHOS_CONTROL_METER_POINT, 31094332d3Sopenharmony_ci OHOS_CONTROL_VIDEO_STABILIZATION_MODE, 32094332d3Sopenharmony_ci OHOS_CONTROL_FOCUS_STATE, 33094332d3Sopenharmony_ci OHOS_CONTROL_EXPOSURE_STATE, 34094332d3Sopenharmony_ci}; 35094332d3Sopenharmony_ci 36094332d3Sopenharmony_ciTestCameraBase::TestCameraBase() 37094332d3Sopenharmony_ci{ 38094332d3Sopenharmony_ci} 39094332d3Sopenharmony_ci 40094332d3Sopenharmony_ciuint64_t TestCameraBase::GetCurrentLocalTimeStamp() 41094332d3Sopenharmony_ci{ 42094332d3Sopenharmony_ci std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp = 43094332d3Sopenharmony_ci std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()); 44094332d3Sopenharmony_ci auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch()); 45094332d3Sopenharmony_ci return tmp.count(); 46094332d3Sopenharmony_ci} 47094332d3Sopenharmony_ci 48094332d3Sopenharmony_civoid TestCameraBase::StoreImage(const unsigned char *bufStart, const uint32_t size) const 49094332d3Sopenharmony_ci{ 50094332d3Sopenharmony_ci constexpr uint32_t pathLen = 64; 51094332d3Sopenharmony_ci char path[pathLen] = {0}; 52094332d3Sopenharmony_ci#ifdef CAMERA_BUILT_ON_OHOS_LITE 53094332d3Sopenharmony_ci char prefix[] = "/userdata/photo/"; 54094332d3Sopenharmony_ci#else 55094332d3Sopenharmony_ci char prefix[] = "/data/"; 56094332d3Sopenharmony_ci#endif 57094332d3Sopenharmony_ci 58094332d3Sopenharmony_ci int imgFD = 0; 59094332d3Sopenharmony_ci int ret = 0; 60094332d3Sopenharmony_ci 61094332d3Sopenharmony_ci struct timeval start = {}; 62094332d3Sopenharmony_ci gettimeofday(&start, nullptr); 63094332d3Sopenharmony_ci if (sprintf_s(path, sizeof(path), "%spicture_%ld.jpeg", prefix, start.tv_usec) < 0) { 64094332d3Sopenharmony_ci CAMERA_LOGE("sprintf_s error .....\n"); 65094332d3Sopenharmony_ci return; 66094332d3Sopenharmony_ci } 67094332d3Sopenharmony_ci 68094332d3Sopenharmony_ci imgFD = open(path, O_RDWR | O_CREAT, 00766); // 00766:file operate permission 69094332d3Sopenharmony_ci if (imgFD == -1) { 70094332d3Sopenharmony_ci CAMERA_LOGE("demo test:open image file error %{public}s.....\n", strerror(errno)); 71094332d3Sopenharmony_ci return; 72094332d3Sopenharmony_ci } 73094332d3Sopenharmony_ci 74094332d3Sopenharmony_ci CAMERA_LOGD("demo test:StoreImage %{public}s size == %{public}d\n", path, size); 75094332d3Sopenharmony_ci 76094332d3Sopenharmony_ci ret = write(imgFD, bufStart, size); 77094332d3Sopenharmony_ci if (ret == -1) { 78094332d3Sopenharmony_ci CAMERA_LOGE("demo test:write image file error %{public}s.....\n", strerror(errno)); 79094332d3Sopenharmony_ci } 80094332d3Sopenharmony_ci 81094332d3Sopenharmony_ci close(imgFD); 82094332d3Sopenharmony_ci} 83094332d3Sopenharmony_ci 84094332d3Sopenharmony_civoid TestCameraBase::StoreVideo(const unsigned char *bufStart, const uint32_t size) const 85094332d3Sopenharmony_ci{ 86094332d3Sopenharmony_ci constexpr uint32_t pathLen = 64; 87094332d3Sopenharmony_ci char path[pathLen] = {0}; 88094332d3Sopenharmony_ci#ifdef CAMERA_BUILT_ON_OHOS_LITE 89094332d3Sopenharmony_ci char prefix[] = "/userdata/video/"; 90094332d3Sopenharmony_ci#else 91094332d3Sopenharmony_ci char prefix[] = "/data/"; 92094332d3Sopenharmony_ci#endif 93094332d3Sopenharmony_ci struct timeval start = {}; 94094332d3Sopenharmony_ci gettimeofday(&start, nullptr); 95094332d3Sopenharmony_ci if (sprintf_s(path, sizeof(path), "%svideo_%ld.yuv", prefix, start.tv_usec) < 0) { 96094332d3Sopenharmony_ci CAMERA_LOGE("%{public}s: sprintf failed", __func__); 97094332d3Sopenharmony_ci return; 98094332d3Sopenharmony_ci } 99094332d3Sopenharmony_ci int videoFd = open(path, O_RDWR | O_CREAT, 00766); // 00766:file operate permission 100094332d3Sopenharmony_ci if (videoFd < 0) { 101094332d3Sopenharmony_ci CAMERA_LOGE("demo test: StartVideo open %s %{public}s failed", path, strerror(errno)); 102094332d3Sopenharmony_ci } 103094332d3Sopenharmony_ci int ret = 0; 104094332d3Sopenharmony_ci 105094332d3Sopenharmony_ci ret = write(videoFd, bufStart, size); 106094332d3Sopenharmony_ci if (ret == -1) { 107094332d3Sopenharmony_ci CAMERA_LOGE("demo test:write video file error %{public}s.....\n", strerror(errno)); 108094332d3Sopenharmony_ci } 109094332d3Sopenharmony_ci CAMERA_LOGD("demo test:StoreVideo size == %{public}d\n", size); 110094332d3Sopenharmony_ci close(videoFd); 111094332d3Sopenharmony_ci} 112094332d3Sopenharmony_ci 113094332d3Sopenharmony_civoid TestCameraBase::OpenVideoFile() 114094332d3Sopenharmony_ci{ 115094332d3Sopenharmony_ci constexpr uint32_t pathLen = 64; 116094332d3Sopenharmony_ci char path[pathLen] = {0}; 117094332d3Sopenharmony_ci#ifdef CAMERA_BUILT_ON_OHOS_LITE 118094332d3Sopenharmony_ci char prefix[] = "/userdata/video/"; 119094332d3Sopenharmony_ci#else 120094332d3Sopenharmony_ci char prefix[] = "/data/"; 121094332d3Sopenharmony_ci#endif 122094332d3Sopenharmony_ci auto seconds = time(nullptr); 123094332d3Sopenharmony_ci if (sprintf_s(path, sizeof(path), "%svideo%ld.h264", prefix, seconds) < 0) { 124094332d3Sopenharmony_ci CAMERA_LOGE("%{public}s: sprintf failed", __func__); 125094332d3Sopenharmony_ci return; 126094332d3Sopenharmony_ci } 127094332d3Sopenharmony_ci videoFd_ = open(path, O_RDWR | O_CREAT, 00766); // 00766:file operate permission 128094332d3Sopenharmony_ci if (videoFd_ < 0) { 129094332d3Sopenharmony_ci CAMERA_LOGE("demo test: StartVideo open %s %{public}s failed", path, strerror(errno)); 130094332d3Sopenharmony_ci } 131094332d3Sopenharmony_ci} 132094332d3Sopenharmony_ci 133094332d3Sopenharmony_civoid TestCameraBase::CloseFd() 134094332d3Sopenharmony_ci{ 135094332d3Sopenharmony_ci close(videoFd_); 136094332d3Sopenharmony_ci videoFd_ = -1; 137094332d3Sopenharmony_ci} 138094332d3Sopenharmony_ci 139094332d3Sopenharmony_civoid TestCameraBase::PrintFaceDetectInfo(const unsigned char *bufStart, const uint32_t size) const 140094332d3Sopenharmony_ci{ 141094332d3Sopenharmony_ci common_metadata_header_t* data = reinterpret_cast<common_metadata_header_t*>( 142094332d3Sopenharmony_ci const_cast<unsigned char*>(bufStart)); 143094332d3Sopenharmony_ci camera_metadata_item_t entry; 144094332d3Sopenharmony_ci int ret = 0; 145094332d3Sopenharmony_ci ret = FindCameraMetadataItem(data, OHOS_STATISTICS_FACE_DETECT_SWITCH, &entry); 146094332d3Sopenharmony_ci if (ret != 0) { 147094332d3Sopenharmony_ci CAMERA_LOGE("demo test: get OHOS_STATISTICS_FACE_DETECT_SWITCH error\n"); 148094332d3Sopenharmony_ci return; 149094332d3Sopenharmony_ci } 150094332d3Sopenharmony_ci uint8_t switchValue = *(entry.data.u8); 151094332d3Sopenharmony_ci CAMERA_LOGI("demo test: switchValue=%{public}d", switchValue); 152094332d3Sopenharmony_ci 153094332d3Sopenharmony_ci ret = FindCameraMetadataItem(data, OHOS_STATISTICS_FACE_RECTANGLES, &entry); 154094332d3Sopenharmony_ci if (ret != 0) { 155094332d3Sopenharmony_ci CAMERA_LOGE("demo test: get OHOS_STATISTICS_FACE_RECTANGLES error\n"); 156094332d3Sopenharmony_ci return; 157094332d3Sopenharmony_ci } 158094332d3Sopenharmony_ci uint32_t rectCount = entry.count; 159094332d3Sopenharmony_ci CAMERA_LOGI("demo test: rectCount=%{public}d", rectCount); 160094332d3Sopenharmony_ci std::vector<std::vector<float>> faceRectangles; 161094332d3Sopenharmony_ci std::vector<float> faceRectangle; 162094332d3Sopenharmony_ci for (int i = 0; i < rectCount; i++) { 163094332d3Sopenharmony_ci faceRectangle.push_back(*(entry.data.f + i)); 164094332d3Sopenharmony_ci } 165094332d3Sopenharmony_ci faceRectangles.push_back(faceRectangle); 166094332d3Sopenharmony_ci for (std::vector<std::vector<float>>::iterator it = faceRectangles.begin(); it < faceRectangles.end(); it++) { 167094332d3Sopenharmony_ci for (std::vector<float>::iterator innerIt = (*it).begin(); innerIt < (*it).end(); innerIt++) { 168094332d3Sopenharmony_ci CAMERA_LOGI("demo test: innerIt : %{public}f", *innerIt); 169094332d3Sopenharmony_ci } 170094332d3Sopenharmony_ci } 171094332d3Sopenharmony_ci 172094332d3Sopenharmony_ci ret = FindCameraMetadataItem(data, OHOS_STATISTICS_FACE_IDS, &entry); 173094332d3Sopenharmony_ci if (ret != 0) { 174094332d3Sopenharmony_ci CAMERA_LOGE("demo test: get OHOS_STATISTICS_FACE_IDS error\n"); 175094332d3Sopenharmony_ci return; 176094332d3Sopenharmony_ci } 177094332d3Sopenharmony_ci uint32_t idCount = entry.count; 178094332d3Sopenharmony_ci CAMERA_LOGI("demo test: idCount=%{public}d", idCount); 179094332d3Sopenharmony_ci std::vector<int32_t> faceIds; 180094332d3Sopenharmony_ci for (int i = 0; i < idCount; i++) { 181094332d3Sopenharmony_ci faceIds.push_back(*(entry.data.i32 + i)); 182094332d3Sopenharmony_ci } 183094332d3Sopenharmony_ci for (auto it = faceIds.begin(); it != faceIds.end(); it++) { 184094332d3Sopenharmony_ci CAMERA_LOGI("demo test: faceIds : %{public}d", *it); 185094332d3Sopenharmony_ci } 186094332d3Sopenharmony_ci} 187094332d3Sopenharmony_ci 188094332d3Sopenharmony_ciint32_t TestCameraBase::SaveYUV(char* type, unsigned char* buffer, int32_t size) 189094332d3Sopenharmony_ci{ 190094332d3Sopenharmony_ci int ret; 191094332d3Sopenharmony_ci char path[PATH_MAX] = {0}; 192094332d3Sopenharmony_ci ret = sprintf_s(path, sizeof(path) / sizeof(path[0]), "/mnt/yuv/%s_%lld.yuv", type, GetCurrentLocalTimeStamp()); 193094332d3Sopenharmony_ci if (ret < 0) { 194094332d3Sopenharmony_ci CAMERA_LOGE("%s, sprintf_s failed, errno = %s.", __FUNCTION__, strerror(errno)); 195094332d3Sopenharmony_ci return -1; 196094332d3Sopenharmony_ci } 197094332d3Sopenharmony_ci CAMERA_LOGI("%s, save yuv to file %s", __FUNCTION__, path); 198094332d3Sopenharmony_ci system("mkdir -p /mnt/yuv"); 199094332d3Sopenharmony_ci int imgFd = open(path, O_RDWR | O_CREAT, 00766); // 00766: file permissions 200094332d3Sopenharmony_ci if (imgFd == -1) { 201094332d3Sopenharmony_ci CAMERA_LOGI("%s, open file failed, errno = %s.", __FUNCTION__, strerror(errno)); 202094332d3Sopenharmony_ci return -1; 203094332d3Sopenharmony_ci } 204094332d3Sopenharmony_ci ret = write(imgFd, buffer, size); 205094332d3Sopenharmony_ci if (ret == -1) { 206094332d3Sopenharmony_ci CAMERA_LOGI("%s, write file failed, error = %s", __FUNCTION__, strerror(errno)); 207094332d3Sopenharmony_ci close(imgFd); 208094332d3Sopenharmony_ci return -1; 209094332d3Sopenharmony_ci } 210094332d3Sopenharmony_ci close(imgFd); 211094332d3Sopenharmony_ci return 0; 212094332d3Sopenharmony_ci} 213094332d3Sopenharmony_ci 214094332d3Sopenharmony_ciint TestCameraBase::DoFbMunmap(unsigned char* addr) 215094332d3Sopenharmony_ci{ 216094332d3Sopenharmony_ci int ret; 217094332d3Sopenharmony_ci unsigned int size = vinfo_.xres * vinfo_.yres * vinfo_.bits_per_pixel / 8; // 8:picture size; 218094332d3Sopenharmony_ci CAMERA_LOGI("main test:munmapped size = %d\n", size); 219094332d3Sopenharmony_ci ret = (munmap(addr, finfo_.smem_len)); 220094332d3Sopenharmony_ci return ret; 221094332d3Sopenharmony_ci} 222094332d3Sopenharmony_ci 223094332d3Sopenharmony_ciunsigned char* TestCameraBase::DoFbMmap(int* pmemfd) 224094332d3Sopenharmony_ci{ 225094332d3Sopenharmony_ci unsigned char* ret; 226094332d3Sopenharmony_ci int screensize = vinfo_.xres * vinfo_.yres * vinfo_.bits_per_pixel / 8; // 8:picture size 227094332d3Sopenharmony_ci ret = static_cast<unsigned char*>(mmap(NULL, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, *pmemfd, 0)); 228094332d3Sopenharmony_ci if (ret == MAP_FAILED) { 229094332d3Sopenharmony_ci CAMERA_LOGE("main test:do_mmap: pmem mmap() failed: %s (%d)\n", strerror(errno), errno); 230094332d3Sopenharmony_ci return nullptr; 231094332d3Sopenharmony_ci } 232094332d3Sopenharmony_ci CAMERA_LOGI("main test:do_mmap: pmem mmap fd %d len %u\n", *pmemfd, screensize); 233094332d3Sopenharmony_ci return ret; 234094332d3Sopenharmony_ci} 235094332d3Sopenharmony_ci 236094332d3Sopenharmony_civoid TestCameraBase::FBLog() 237094332d3Sopenharmony_ci{ 238094332d3Sopenharmony_ci CAMERA_LOGI("the fixed information is as follow:\n"); 239094332d3Sopenharmony_ci CAMERA_LOGI("id=%s\n", finfo_.id); 240094332d3Sopenharmony_ci CAMERA_LOGI("sem_start=%lx\n", finfo_.smem_start); 241094332d3Sopenharmony_ci CAMERA_LOGI("smem_len=%u\n", finfo_.smem_len); 242094332d3Sopenharmony_ci CAMERA_LOGI("type=%u\n", finfo_.type); 243094332d3Sopenharmony_ci CAMERA_LOGI("line_length=%u\n", finfo_.line_length); 244094332d3Sopenharmony_ci CAMERA_LOGI("mmio_start=%lu\n", finfo_.mmio_start); 245094332d3Sopenharmony_ci CAMERA_LOGI("mmio_len=%d\n", finfo_.mmio_len); 246094332d3Sopenharmony_ci CAMERA_LOGI("visual=%d\n", finfo_.visual); 247094332d3Sopenharmony_ci 248094332d3Sopenharmony_ci CAMERA_LOGI("variable information is as follow:\n"); 249094332d3Sopenharmony_ci CAMERA_LOGI("The xres is :%u\n", vinfo_.xres); 250094332d3Sopenharmony_ci CAMERA_LOGI("The yres is :%u\n", vinfo_.yres); 251094332d3Sopenharmony_ci CAMERA_LOGI("xres_virtual=%u\n", vinfo_.xres_virtual); 252094332d3Sopenharmony_ci CAMERA_LOGI("yres_virtual=%u\n", vinfo_.yres_virtual); 253094332d3Sopenharmony_ci CAMERA_LOGI("xoffset=%u\n", vinfo_.xoffset); 254094332d3Sopenharmony_ci CAMERA_LOGI("yoffset=%u\n", vinfo_.yoffset); 255094332d3Sopenharmony_ci CAMERA_LOGI("bits_per_pixel is :%u\n", vinfo_.bits_per_pixel); 256094332d3Sopenharmony_ci CAMERA_LOGI("red.offset=%u\n", vinfo_.red.offset); 257094332d3Sopenharmony_ci CAMERA_LOGI("red.length=%u\n", vinfo_.red.length); 258094332d3Sopenharmony_ci CAMERA_LOGI("red.msb_right=%u\n", vinfo_.red.msb_right); 259094332d3Sopenharmony_ci CAMERA_LOGI("green.offset=%d\n", vinfo_.green.offset); 260094332d3Sopenharmony_ci CAMERA_LOGI("green.length=%d\n", vinfo_.green.length); 261094332d3Sopenharmony_ci CAMERA_LOGI("green.msb_right=%d\n", vinfo_.green.msb_right); 262094332d3Sopenharmony_ci CAMERA_LOGI("blue.offset=%d\n", vinfo_.blue.offset); 263094332d3Sopenharmony_ci CAMERA_LOGI("blue.length=%d\n", vinfo_.blue.length); 264094332d3Sopenharmony_ci CAMERA_LOGI("blue.msb_right=%d\n", vinfo_.blue.msb_right); 265094332d3Sopenharmony_ci CAMERA_LOGI("transp.offset=%d\n", vinfo_.transp.offset); 266094332d3Sopenharmony_ci CAMERA_LOGI("transp.length=%d\n", vinfo_.transp.length); 267094332d3Sopenharmony_ci CAMERA_LOGI("transp.msb_right=%d\n", vinfo_.transp.msb_right); 268094332d3Sopenharmony_ci CAMERA_LOGI("height=%x\n", vinfo_.height); 269094332d3Sopenharmony_ci} 270094332d3Sopenharmony_ci 271094332d3Sopenharmony_ciOHOS::Camera::RetCode TestCameraBase::FBInit() 272094332d3Sopenharmony_ci{ 273094332d3Sopenharmony_ci fbFd_ = open("/dev/fb0", O_RDWR); 274094332d3Sopenharmony_ci if (fbFd_ < 0) { 275094332d3Sopenharmony_ci CAMERA_LOGE("main test:cannot open framebuffer %s file node\n", "/dev/fb0"); 276094332d3Sopenharmony_ci return RC_ERROR; 277094332d3Sopenharmony_ci } 278094332d3Sopenharmony_ci 279094332d3Sopenharmony_ci if (ioctl(fbFd_, FBIOGET_VSCREENINFO, &vinfo_) < 0) { 280094332d3Sopenharmony_ci CAMERA_LOGE("main test:cannot retrieve vscreenInfo!\n"); 281094332d3Sopenharmony_ci close(fbFd_); 282094332d3Sopenharmony_ci return RC_ERROR; 283094332d3Sopenharmony_ci } 284094332d3Sopenharmony_ci 285094332d3Sopenharmony_ci if (ioctl(fbFd_, FBIOGET_FSCREENINFO, &finfo_) < 0) { 286094332d3Sopenharmony_ci CAMERA_LOGE("main test:can't retrieve fscreenInfo!\n"); 287094332d3Sopenharmony_ci close(fbFd_); 288094332d3Sopenharmony_ci return RC_ERROR; 289094332d3Sopenharmony_ci } 290094332d3Sopenharmony_ci 291094332d3Sopenharmony_ci FBLog(); 292094332d3Sopenharmony_ci 293094332d3Sopenharmony_ci CAMERA_LOGI("main test:allocating display buffer memory\n"); 294094332d3Sopenharmony_ci displayBuf_ = DoFbMmap(&fbFd_); 295094332d3Sopenharmony_ci if (displayBuf_ == nullptr) { 296094332d3Sopenharmony_ci CAMERA_LOGE("main test:error displayBuf_ mmap error\n"); 297094332d3Sopenharmony_ci close(fbFd_); 298094332d3Sopenharmony_ci return RC_ERROR; 299094332d3Sopenharmony_ci } 300094332d3Sopenharmony_ci return RC_OK; 301094332d3Sopenharmony_ci} 302094332d3Sopenharmony_ci 303094332d3Sopenharmony_civoid TestCameraBase::ProcessImage(unsigned char* p, unsigned char* fbp) 304094332d3Sopenharmony_ci{ 305094332d3Sopenharmony_ci unsigned char* in = p; 306094332d3Sopenharmony_ci int width = 640; // 640:Displays the size of the width 307094332d3Sopenharmony_ci int height = 480; // 480:Displays the size of the height 308094332d3Sopenharmony_ci int istride = 1280; // 1280:Initial value of span 309094332d3Sopenharmony_ci int x, y, j; 310094332d3Sopenharmony_ci int y0, u, v, r, g, b; 311094332d3Sopenharmony_ci int32_t location = 0; 312094332d3Sopenharmony_ci int xpos = (vinfo_.xres - width) / 2; 313094332d3Sopenharmony_ci int ypos = (vinfo_.yres - height) / 2; 314094332d3Sopenharmony_ci int yPos, uPos, vPos; 315094332d3Sopenharmony_ci int yPosIncrement, uPosIncrement, vPosIncrement; 316094332d3Sopenharmony_ci 317094332d3Sopenharmony_ci yPos = 0; // 0:Pixel initial value 318094332d3Sopenharmony_ci uPos = 1; // 1:Pixel initial value 319094332d3Sopenharmony_ci vPos = 3; // 3:Pixel initial value 320094332d3Sopenharmony_ci yPosIncrement = 2; // 2:yPos increase value 321094332d3Sopenharmony_ci uPosIncrement = 4; // 4:uPos increase value 322094332d3Sopenharmony_ci vPosIncrement = 4; // 4:vPos increase value 323094332d3Sopenharmony_ci 324094332d3Sopenharmony_ci for (y = ypos; y < (height + ypos); y++) { 325094332d3Sopenharmony_ci for (j = 0, x = xpos; j < width; j++, x++) { 326094332d3Sopenharmony_ci location = (x + vinfo_.xoffset) * (vinfo_.bits_per_pixel / 8) + // 8: The bytes for each time 327094332d3Sopenharmony_ci (y + vinfo_.yoffset) * finfo_.line_length; // add one y number of rows at a time 328094332d3Sopenharmony_ci 329094332d3Sopenharmony_ci y0 = in[yPos]; 330094332d3Sopenharmony_ci u = in[uPos] - 128; // 128:display size 331094332d3Sopenharmony_ci v = in[vPos] - 128; // 128:display size 332094332d3Sopenharmony_ci 333094332d3Sopenharmony_ci r = RANGE_LIMIT(y0 + v + ((v * 103) >> 8)); // 103,8:display range 334094332d3Sopenharmony_ci g = RANGE_LIMIT(y0 - ((u * 88) >> 8) - ((v * 183) >> 8)); // 88,8,183:display range 335094332d3Sopenharmony_ci b = RANGE_LIMIT(y0 + u + ((u * 198) >> 8)); // 198,8:display range 336094332d3Sopenharmony_ci 337094332d3Sopenharmony_ci fbp[location + 1] = ((r & 0xF8) | (g >> 5)); // 5:display range 338094332d3Sopenharmony_ci fbp[location + 0] = (((g & 0x1C) << 3) | (b >> 3)); // 3:display range 339094332d3Sopenharmony_ci 340094332d3Sopenharmony_ci yPos += yPosIncrement; 341094332d3Sopenharmony_ci 342094332d3Sopenharmony_ci if (j & 0x01) { 343094332d3Sopenharmony_ci uPos += uPosIncrement; 344094332d3Sopenharmony_ci vPos += vPosIncrement; 345094332d3Sopenharmony_ci } 346094332d3Sopenharmony_ci } 347094332d3Sopenharmony_ci 348094332d3Sopenharmony_ci yPos = 0; // 0:Pixel initial value 349094332d3Sopenharmony_ci uPos = 1; // 1:Pixel initial value 350094332d3Sopenharmony_ci vPos = 3; // 3:Pixel initial value 351094332d3Sopenharmony_ci in += istride; // add one y number of rows at a time 352094332d3Sopenharmony_ci } 353094332d3Sopenharmony_ci} 354094332d3Sopenharmony_ci 355094332d3Sopenharmony_civoid TestCameraBase::LcdDrawScreen(unsigned char* displayBuf, unsigned char* addr) 356094332d3Sopenharmony_ci{ 357094332d3Sopenharmony_ci ProcessImage(addr, displayBuf); 358094332d3Sopenharmony_ci} 359094332d3Sopenharmony_ci 360094332d3Sopenharmony_civoid TestCameraBase::BufferCallback(unsigned char* addr, int choice) 361094332d3Sopenharmony_ci{ 362094332d3Sopenharmony_ci if (choice == PREVIEW_MODE) { 363094332d3Sopenharmony_ci LcdDrawScreen(displayBuf_, addr); 364094332d3Sopenharmony_ci return; 365094332d3Sopenharmony_ci } else { 366094332d3Sopenharmony_ci LcdDrawScreen(displayBuf_, addr); 367094332d3Sopenharmony_ci std::cout << "==========[test log] capture start saveYuv......" << std::endl; 368094332d3Sopenharmony_ci SaveYUV("capture", reinterpret_cast<unsigned char*>(addr), bufSize_); 369094332d3Sopenharmony_ci std::cout << "==========[test log] capture end saveYuv......" << std::endl; 370094332d3Sopenharmony_ci return; 371094332d3Sopenharmony_ci } 372094332d3Sopenharmony_ci} 373094332d3Sopenharmony_ci 374094332d3Sopenharmony_civoid TestCameraBase::Init() 375094332d3Sopenharmony_ci{ 376094332d3Sopenharmony_ci CAMERA_LOGD("TestCameraBase::Init()."); 377094332d3Sopenharmony_ci if (cameraHost == nullptr) { 378094332d3Sopenharmony_ci constexpr const char *demoServiceName = "camera_service"; 379094332d3Sopenharmony_ci cameraHost = ICameraHost::Get(demoServiceName, false); 380094332d3Sopenharmony_ci CAMERA_LOGI("Camera::CameraHost::CreateCameraHost()"); 381094332d3Sopenharmony_ci if (cameraHost == nullptr) { 382094332d3Sopenharmony_ci CAMERA_LOGE("CreateCameraHost failed."); 383094332d3Sopenharmony_ci return; 384094332d3Sopenharmony_ci } 385094332d3Sopenharmony_ci CAMERA_LOGI("CreateCameraHost success."); 386094332d3Sopenharmony_ci } 387094332d3Sopenharmony_ci 388094332d3Sopenharmony_ci OHOS::sptr<DemoCameraHostCallback> cameraHostCallback = new DemoCameraHostCallback(); 389094332d3Sopenharmony_ci OHOS::Camera::RetCode ret = cameraHost->SetCallback(cameraHostCallback); 390094332d3Sopenharmony_ci if (ret != HDI::Camera::V1_0::NO_ERROR) { 391094332d3Sopenharmony_ci CAMERA_LOGE("SetCallback failed."); 392094332d3Sopenharmony_ci return; 393094332d3Sopenharmony_ci } else { 394094332d3Sopenharmony_ci CAMERA_LOGI("SetCallback success."); 395094332d3Sopenharmony_ci } 396094332d3Sopenharmony_ci 397094332d3Sopenharmony_ci if (cameraDevice == nullptr) { 398094332d3Sopenharmony_ci cameraHost->GetCameraIds(cameraIds); 399094332d3Sopenharmony_ci cameraHost->GetCameraAbility(cameraIds.front(), ability_); 400094332d3Sopenharmony_ci MetadataUtils::ConvertVecToMetadata(ability_, ability); 401094332d3Sopenharmony_ci const OHOS::sptr<DemoCameraDeviceCallback> callback = new DemoCameraDeviceCallback(); 402094332d3Sopenharmony_ci rc = (CamRetCode)cameraHost->OpenCamera(cameraIds.front(), callback, cameraDevice); 403094332d3Sopenharmony_ci if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) { 404094332d3Sopenharmony_ci CAMERA_LOGE("OpenCamera failed, rc = %{public}d", rc); 405094332d3Sopenharmony_ci return; 406094332d3Sopenharmony_ci } 407094332d3Sopenharmony_ci CAMERA_LOGI("OpenCamera success."); 408094332d3Sopenharmony_ci } 409094332d3Sopenharmony_ci} 410094332d3Sopenharmony_ci 411094332d3Sopenharmony_civoid TestCameraBase::UsbInit() 412094332d3Sopenharmony_ci{ 413094332d3Sopenharmony_ci if (cameraHost == nullptr) { 414094332d3Sopenharmony_ci constexpr const char *demoServiceName = "camera_service"; 415094332d3Sopenharmony_ci cameraHost = ICameraHost::Get(demoServiceName, false); 416094332d3Sopenharmony_ci if (cameraHost == nullptr) { 417094332d3Sopenharmony_ci std::cout << "==========[test log] CreateCameraHost failed." << std::endl; 418094332d3Sopenharmony_ci return; 419094332d3Sopenharmony_ci } 420094332d3Sopenharmony_ci std::cout << "==========[test log] CreateCameraHost success." << std::endl; 421094332d3Sopenharmony_ci } 422094332d3Sopenharmony_ci 423094332d3Sopenharmony_ci OHOS::sptr<DemoCameraHostCallback> cameraHostCallback = new DemoCameraHostCallback(); 424094332d3Sopenharmony_ci OHOS::Camera::RetCode ret = cameraHost->SetCallback(cameraHostCallback); 425094332d3Sopenharmony_ci if (ret != HDI::Camera::V1_0::NO_ERROR) { 426094332d3Sopenharmony_ci std::cout << "==========[test log] SetCallback failed." << std::endl; 427094332d3Sopenharmony_ci return; 428094332d3Sopenharmony_ci } else { 429094332d3Sopenharmony_ci std::cout << "==========[test log] SetCallback success." << std::endl; 430094332d3Sopenharmony_ci } 431094332d3Sopenharmony_ci} 432094332d3Sopenharmony_ci 433094332d3Sopenharmony_cistd::shared_ptr<CameraAbility> TestCameraBase::GetCameraAbility() 434094332d3Sopenharmony_ci{ 435094332d3Sopenharmony_ci if (cameraDevice == nullptr) { 436094332d3Sopenharmony_ci OHOS::Camera::RetCode ret = cameraHost->GetCameraIds(cameraIds); 437094332d3Sopenharmony_ci if (ret != HDI::Camera::V1_0::NO_ERROR) { 438094332d3Sopenharmony_ci std::cout << "==========[test log]GetCameraIds failed." << std::endl; 439094332d3Sopenharmony_ci return ability; 440094332d3Sopenharmony_ci } else { 441094332d3Sopenharmony_ci std::cout << "==========[test log]GetCameraIds success." << std::endl; 442094332d3Sopenharmony_ci } 443094332d3Sopenharmony_ci if (cameraIds.size() == 0) { 444094332d3Sopenharmony_ci std::cout << "==========[test log]camera device list is empty." << std::endl; 445094332d3Sopenharmony_ci return ability; 446094332d3Sopenharmony_ci } 447094332d3Sopenharmony_ci if (cameraIds.size() > 0) { 448094332d3Sopenharmony_ci ret = cameraHost->GetCameraAbility(cameraIds.back(), ability_); 449094332d3Sopenharmony_ci if (ret != HDI::Camera::V1_0::NO_ERROR) { 450094332d3Sopenharmony_ci std::cout << "==========[test log]GetCameraAbility failed, rc = " << rc << std::endl; 451094332d3Sopenharmony_ci } 452094332d3Sopenharmony_ci MetadataUtils::ConvertVecToMetadata(ability_, ability); 453094332d3Sopenharmony_ci } 454094332d3Sopenharmony_ci } 455094332d3Sopenharmony_ci return ability; 456094332d3Sopenharmony_ci} 457094332d3Sopenharmony_ci 458094332d3Sopenharmony_cistd::shared_ptr<CameraAbility> TestCameraBase::GetCameraAbilityById(std::string cameraId) 459094332d3Sopenharmony_ci{ 460094332d3Sopenharmony_ci int ret = cameraHost->GetCameraAbility(cameraId, ability_); 461094332d3Sopenharmony_ci if (ret != HDI::Camera::V1_0::NO_ERROR) { 462094332d3Sopenharmony_ci CAMERA_LOGD("==========[test log]GetCameraAbility failed, ret = %{public}d", ret); 463094332d3Sopenharmony_ci } 464094332d3Sopenharmony_ci MetadataUtils::ConvertVecToMetadata(ability_, ability); 465094332d3Sopenharmony_ci return ability; 466094332d3Sopenharmony_ci} 467094332d3Sopenharmony_ci 468094332d3Sopenharmony_civoid TestCameraBase::OpenUsbCamera() 469094332d3Sopenharmony_ci{ 470094332d3Sopenharmony_ci if (cameraDevice == nullptr) { 471094332d3Sopenharmony_ci cameraHost->GetCameraIds(cameraIds); 472094332d3Sopenharmony_ci if (cameraIds.size() > 0) { 473094332d3Sopenharmony_ci cameraHost->GetCameraAbility(cameraIds.back(), ability_); 474094332d3Sopenharmony_ci MetadataUtils::ConvertVecToMetadata(ability_, ability); 475094332d3Sopenharmony_ci const OHOS::sptr<DemoCameraDeviceCallback> callback = new DemoCameraDeviceCallback(); 476094332d3Sopenharmony_ci rc = (CamRetCode)cameraHost->OpenCamera(cameraIds.back(), callback, cameraDevice); 477094332d3Sopenharmony_ci if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) { 478094332d3Sopenharmony_ci std::cout << "OpenCamera failed, rc = " << rc << std::endl; 479094332d3Sopenharmony_ci return; 480094332d3Sopenharmony_ci } 481094332d3Sopenharmony_ci std::cout << "OpenCamera success." << std::endl; 482094332d3Sopenharmony_ci } else { 483094332d3Sopenharmony_ci std::cout << "No usb camera plugged in" << std::endl; 484094332d3Sopenharmony_ci GTEST_SKIP() << "No usb camera plugged in" << std::endl; 485094332d3Sopenharmony_ci } 486094332d3Sopenharmony_ci } 487094332d3Sopenharmony_ci} 488094332d3Sopenharmony_ci 489094332d3Sopenharmony_ciCamRetCode TestCameraBase::SelectOpenCamera(std::string cameraId) 490094332d3Sopenharmony_ci{ 491094332d3Sopenharmony_ci cameraHost->GetCameraAbility(cameraId, ability_); 492094332d3Sopenharmony_ci MetadataUtils::ConvertVecToMetadata(ability_, ability); 493094332d3Sopenharmony_ci const OHOS::sptr<DemoCameraDeviceCallback> callback = new DemoCameraDeviceCallback(); 494094332d3Sopenharmony_ci rc = (CamRetCode)cameraHost->OpenCamera(cameraId, callback, cameraDevice); 495094332d3Sopenharmony_ci if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) { 496094332d3Sopenharmony_ci std::cout << "OpenCamera failed, rc = " << rc << std::endl; 497094332d3Sopenharmony_ci return rc; 498094332d3Sopenharmony_ci } 499094332d3Sopenharmony_ci std::cout << "OpenCamera success." << std::endl; 500094332d3Sopenharmony_ci return rc; 501094332d3Sopenharmony_ci} 502094332d3Sopenharmony_ci 503094332d3Sopenharmony_civoid TestCameraBase::Close() 504094332d3Sopenharmony_ci{ 505094332d3Sopenharmony_ci CAMERA_LOGD("cameraDevice->Close()."); 506094332d3Sopenharmony_ci if (cameraDevice != nullptr) { 507094332d3Sopenharmony_ci cameraDevice->Close(); 508094332d3Sopenharmony_ci cameraDevice = nullptr; 509094332d3Sopenharmony_ci } 510094332d3Sopenharmony_ci} 511094332d3Sopenharmony_ci 512094332d3Sopenharmony_civoid TestCameraBase::OpenCamera() 513094332d3Sopenharmony_ci{ 514094332d3Sopenharmony_ci if (cameraDevice == nullptr) { 515094332d3Sopenharmony_ci cameraHost->GetCameraIds(cameraIds); 516094332d3Sopenharmony_ci const OHOS::sptr<OHOS::Camera::CameraDeviceCallback> callback = new CameraDeviceCallback(); 517094332d3Sopenharmony_ci rc = (CamRetCode)cameraHost->OpenCamera(cameraIds.front(), callback, cameraDevice); 518094332d3Sopenharmony_ci if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) { 519094332d3Sopenharmony_ci std::cout << "==========[test log] OpenCamera failed, rc = " << rc << std::endl; 520094332d3Sopenharmony_ci return; 521094332d3Sopenharmony_ci } 522094332d3Sopenharmony_ci std::cout << "==========[test log] OpenCamera success." << std::endl; 523094332d3Sopenharmony_ci } 524094332d3Sopenharmony_ci} 525094332d3Sopenharmony_ci 526094332d3Sopenharmony_cifloat TestCameraBase::CalTime(struct timeval start, struct timeval end) 527094332d3Sopenharmony_ci{ 528094332d3Sopenharmony_ci float timeUse = 0; 529094332d3Sopenharmony_ci timeUse = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); // 1000000:time 530094332d3Sopenharmony_ci return timeUse; 531094332d3Sopenharmony_ci} 532094332d3Sopenharmony_ci 533094332d3Sopenharmony_civoid TestCameraBase::AchieveStreamOperator() 534094332d3Sopenharmony_ci{ 535094332d3Sopenharmony_ci // Create and get streamOperator information 536094332d3Sopenharmony_ci OHOS::sptr<DemoStreamOperatorCallback> streamOperatorCallback_ = new DemoStreamOperatorCallback(); 537094332d3Sopenharmony_ci rc = (CamRetCode)cameraDevice->GetStreamOperator(streamOperatorCallback_, streamOperator); 538094332d3Sopenharmony_ci EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR); 539094332d3Sopenharmony_ci if (rc == HDI::Camera::V1_0::NO_ERROR) { 540094332d3Sopenharmony_ci CAMERA_LOGI("AchieveStreamOperator success."); 541094332d3Sopenharmony_ci } else { 542094332d3Sopenharmony_ci CAMERA_LOGE("AchieveStreamOperator fail, rc = %{public}d", rc); 543094332d3Sopenharmony_ci } 544094332d3Sopenharmony_ci} 545094332d3Sopenharmony_ci 546094332d3Sopenharmony_civoid TestCameraBase::StartStream(std::vector<StreamIntent> intents) 547094332d3Sopenharmony_ci{ 548094332d3Sopenharmony_ci for (auto& intent : intents) { 549094332d3Sopenharmony_ci if (intent == PREVIEW) { 550094332d3Sopenharmony_ci if (streamCustomerPreview_ == nullptr) { 551094332d3Sopenharmony_ci streamCustomerPreview_ = std::make_shared<StreamCustomer>(); 552094332d3Sopenharmony_ci } 553094332d3Sopenharmony_ci streamInfoPre.streamId_ = STREAM_ID_PREVIEW; 554094332d3Sopenharmony_ci streamInfoPre.width_ = PREVIEW_WIDTH; // 640:picture width 555094332d3Sopenharmony_ci streamInfoPre.height_ = PREVIEW_HEIGHT; // 480:picture height 556094332d3Sopenharmony_ci streamInfoPre.format_ = PIXEL_FMT_RGBA_8888; 557094332d3Sopenharmony_ci streamInfoPre.dataspace_ = 8; // 8:picture dataspace 558094332d3Sopenharmony_ci streamInfoPre.intent_ = intent; 559094332d3Sopenharmony_ci streamInfoPre.tunneledMode_ = 5; // 5:tunnel mode 560094332d3Sopenharmony_ci streamInfoPre.bufferQueue_ = new BufferProducerSequenceable(streamCustomerPreview_->CreateProducer()); 561094332d3Sopenharmony_ci ASSERT_NE(streamInfoPre.bufferQueue_, nullptr); 562094332d3Sopenharmony_ci streamInfoPre.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size 563094332d3Sopenharmony_ci CAMERA_LOGD("preview success."); 564094332d3Sopenharmony_ci std::vector<StreamInfo>().swap(streamInfos); 565094332d3Sopenharmony_ci streamInfos.push_back(streamInfoPre); 566094332d3Sopenharmony_ci } else if (intent == VIDEO) { 567094332d3Sopenharmony_ci if (streamCustomerVideo_ == nullptr) { 568094332d3Sopenharmony_ci streamCustomerVideo_ = std::make_shared<StreamCustomer>(); 569094332d3Sopenharmony_ci } 570094332d3Sopenharmony_ci streamInfoVideo.streamId_ = STREAM_ID_VIDEO; 571094332d3Sopenharmony_ci streamInfoVideo.width_ = VIDEO_WIDTH; // 1280:picture width 572094332d3Sopenharmony_ci streamInfoVideo.height_ = VIDEO_HEIGHT; // 960:picture height 573094332d3Sopenharmony_ci streamInfoVideo.format_ = PIXEL_FMT_YCRCB_420_SP; 574094332d3Sopenharmony_ci streamInfoVideo.dataspace_ = 8; // 8:picture dataspace 575094332d3Sopenharmony_ci streamInfoVideo.intent_ = intent; 576094332d3Sopenharmony_ci streamInfoVideo.encodeType_ = ENCODE_TYPE_H264; 577094332d3Sopenharmony_ci streamInfoVideo.tunneledMode_ = 5; // 5:tunnel mode 578094332d3Sopenharmony_ci streamInfoVideo.bufferQueue_ = new BufferProducerSequenceable(streamCustomerVideo_->CreateProducer()); 579094332d3Sopenharmony_ci ASSERT_NE(streamInfoVideo.bufferQueue_, nullptr); 580094332d3Sopenharmony_ci streamInfoVideo.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size 581094332d3Sopenharmony_ci CAMERA_LOGD("video success."); 582094332d3Sopenharmony_ci std::vector<StreamInfo>().swap(streamInfos); 583094332d3Sopenharmony_ci streamInfos.push_back(streamInfoVideo); 584094332d3Sopenharmony_ci } else if (intent == STILL_CAPTURE) { 585094332d3Sopenharmony_ci if (streamCustomerCapture_ == nullptr) { 586094332d3Sopenharmony_ci streamCustomerCapture_ = std::make_shared<StreamCustomer>(); 587094332d3Sopenharmony_ci } 588094332d3Sopenharmony_ci streamInfoCapture.streamId_ = STREAM_ID_CAPTURE; 589094332d3Sopenharmony_ci streamInfoCapture.width_ = CAPTURE_WIDTH; // 1280:picture width 590094332d3Sopenharmony_ci streamInfoCapture.height_ = CAPTURE_HEIGHT; // 960:picture height 591094332d3Sopenharmony_ci streamInfoCapture.format_ = PIXEL_FMT_RGBA_8888; 592094332d3Sopenharmony_ci streamInfoCapture.dataspace_ = 8; // 8:picture dataspace 593094332d3Sopenharmony_ci streamInfoCapture.intent_ = intent; 594094332d3Sopenharmony_ci streamInfoCapture.encodeType_ = ENCODE_TYPE_JPEG; 595094332d3Sopenharmony_ci streamInfoCapture.tunneledMode_ = 5; // 5:tunnel mode 596094332d3Sopenharmony_ci streamInfoCapture.bufferQueue_ = new BufferProducerSequenceable(streamCustomerCapture_->CreateProducer()); 597094332d3Sopenharmony_ci ASSERT_NE(streamInfoCapture.bufferQueue_, nullptr); 598094332d3Sopenharmony_ci streamInfoCapture.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size 599094332d3Sopenharmony_ci CAMERA_LOGD("capture success."); 600094332d3Sopenharmony_ci std::vector<StreamInfo>().swap(streamInfos); 601094332d3Sopenharmony_ci streamInfos.push_back(streamInfoCapture); 602094332d3Sopenharmony_ci } else if (intent == ANALYZE) { 603094332d3Sopenharmony_ci if (streamCustomerAnalyze_ == nullptr) { 604094332d3Sopenharmony_ci streamCustomerAnalyze_ = std::make_shared<StreamCustomer>(); 605094332d3Sopenharmony_ci } 606094332d3Sopenharmony_ci streamInfoAnalyze.streamId_ = STREAM_ID_ANALYZE; 607094332d3Sopenharmony_ci streamInfoAnalyze.width_ = ANALYZE_WIDTH; // 640:picture width 608094332d3Sopenharmony_ci streamInfoAnalyze.height_ = ANALYZE_HEIGHT; // 480:picture height 609094332d3Sopenharmony_ci streamInfoAnalyze.format_ = PIXEL_FMT_RGBA_8888; 610094332d3Sopenharmony_ci streamInfoAnalyze.dataspace_ = 8; // 8:picture dataspace 611094332d3Sopenharmony_ci streamInfoAnalyze.intent_ = intent; 612094332d3Sopenharmony_ci streamInfoAnalyze.tunneledMode_ = 5; // 5:tunnel mode 613094332d3Sopenharmony_ci streamInfoAnalyze.bufferQueue_ = new BufferProducerSequenceable(streamCustomerAnalyze_->CreateProducer()); 614094332d3Sopenharmony_ci ASSERT_NE(streamInfoAnalyze.bufferQueue_, nullptr); 615094332d3Sopenharmony_ci streamInfoAnalyze.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size 616094332d3Sopenharmony_ci CAMERA_LOGD("analyze success."); 617094332d3Sopenharmony_ci std::vector<StreamInfo>().swap(streamInfos); 618094332d3Sopenharmony_ci streamInfos.push_back(streamInfoAnalyze); 619094332d3Sopenharmony_ci } 620094332d3Sopenharmony_ci rc = (CamRetCode)streamOperator->CreateStreams(streamInfos); 621094332d3Sopenharmony_ci EXPECT_EQ(false, rc != HDI::Camera::V1_0::NO_ERROR); 622094332d3Sopenharmony_ci if (rc == HDI::Camera::V1_0::NO_ERROR) { 623094332d3Sopenharmony_ci CAMERA_LOGI("CreateStreams success."); 624094332d3Sopenharmony_ci } else { 625094332d3Sopenharmony_ci CAMERA_LOGE("CreateStreams fail, rc = %{public}d", rc); 626094332d3Sopenharmony_ci } 627094332d3Sopenharmony_ci 628094332d3Sopenharmony_ci rc = (CamRetCode)streamOperator->CommitStreams(NORMAL, ability_); 629094332d3Sopenharmony_ci EXPECT_EQ(false, rc != HDI::Camera::V1_0::NO_ERROR); 630094332d3Sopenharmony_ci if (rc == HDI::Camera::V1_0::NO_ERROR) { 631094332d3Sopenharmony_ci CAMERA_LOGI("CommitStreams success."); 632094332d3Sopenharmony_ci } else { 633094332d3Sopenharmony_ci CAMERA_LOGE("CommitStreams fail, rc = %{public}d", rc); 634094332d3Sopenharmony_ci } 635094332d3Sopenharmony_ci } 636094332d3Sopenharmony_ci} 637094332d3Sopenharmony_ci 638094332d3Sopenharmony_civoid TestCameraBase::StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming) 639094332d3Sopenharmony_ci{ 640094332d3Sopenharmony_ci // Get preview 641094332d3Sopenharmony_ci captureInfo.streamIds_ = {streamId}; 642094332d3Sopenharmony_ci captureInfo.captureSetting_ = ability_; 643094332d3Sopenharmony_ci captureInfo.enableShutterCallback_ = shutterCallback; 644094332d3Sopenharmony_ci rc = (CamRetCode)streamOperator->Capture(captureId, captureInfo, isStreaming); 645094332d3Sopenharmony_ci EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR); 646094332d3Sopenharmony_ci if (rc == HDI::Camera::V1_0::NO_ERROR) { 647094332d3Sopenharmony_ci CAMERA_LOGI("check Capture: Capture success, captureId = %{public}d", captureId); 648094332d3Sopenharmony_ci } else { 649094332d3Sopenharmony_ci CAMERA_LOGE("check Capture: Capture fail, rc = %{public}d, captureId = %{public}d", rc, captureId); 650094332d3Sopenharmony_ci } 651094332d3Sopenharmony_ci if (captureId == CAPTURE_ID_PREVIEW) { 652094332d3Sopenharmony_ci streamCustomerPreview_->ReceiveFrameOn(nullptr); 653094332d3Sopenharmony_ci } else if (captureId == CAPTURE_ID_CAPTURE) { 654094332d3Sopenharmony_ci streamCustomerCapture_->ReceiveFrameOn([this](const unsigned char *addr, const uint32_t size) { 655094332d3Sopenharmony_ci StoreImage(addr, size); 656094332d3Sopenharmony_ci }); 657094332d3Sopenharmony_ci } else if (captureId == CAPTURE_ID_VIDEO) { 658094332d3Sopenharmony_ci streamCustomerVideo_->ReceiveFrameOn([this](const unsigned char *addr, const uint32_t size) { 659094332d3Sopenharmony_ci StoreVideo(addr, size); 660094332d3Sopenharmony_ci }); 661094332d3Sopenharmony_ci } else if (captureId == CAPTURE_ID_ANALYZE) { 662094332d3Sopenharmony_ci streamCustomerAnalyze_->ReceiveFrameOn([this](const unsigned char *addr, const uint32_t size) { 663094332d3Sopenharmony_ci PrintFaceDetectInfo(addr, size); 664094332d3Sopenharmony_ci }); 665094332d3Sopenharmony_ci } 666094332d3Sopenharmony_ci sleep(2); // 2:sleep two second 667094332d3Sopenharmony_ci} 668094332d3Sopenharmony_ci 669094332d3Sopenharmony_civoid TestCameraBase::StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds) 670094332d3Sopenharmony_ci{ 671094332d3Sopenharmony_ci constexpr uint32_t timeForWaitCancelCapture = 2; 672094332d3Sopenharmony_ci sleep(timeForWaitCancelCapture); 673094332d3Sopenharmony_ci if (captureIds.size() > 0) { 674094332d3Sopenharmony_ci for (const auto &captureId : captureIds) { 675094332d3Sopenharmony_ci if (captureId == CAPTURE_ID_PREVIEW) { 676094332d3Sopenharmony_ci streamCustomerPreview_->ReceiveFrameOff(); 677094332d3Sopenharmony_ci } else if (captureId == CAPTURE_ID_CAPTURE) { 678094332d3Sopenharmony_ci streamCustomerCapture_->ReceiveFrameOff(); 679094332d3Sopenharmony_ci } else if (captureId == CAPTURE_ID_VIDEO) { 680094332d3Sopenharmony_ci streamCustomerVideo_->ReceiveFrameOff(); 681094332d3Sopenharmony_ci sleep(1); 682094332d3Sopenharmony_ci CloseFd(); 683094332d3Sopenharmony_ci } else if (captureId == CAPTURE_ID_ANALYZE) { 684094332d3Sopenharmony_ci streamCustomerAnalyze_->ReceiveFrameOff(); 685094332d3Sopenharmony_ci } 686094332d3Sopenharmony_ci } 687094332d3Sopenharmony_ci for (const auto &captureId : captureIds) { 688094332d3Sopenharmony_ci CAMERA_LOGI("check Capture: CancelCapture success, captureId = %{public}d", captureId); 689094332d3Sopenharmony_ci rc = (CamRetCode)streamOperator->CancelCapture(captureId); 690094332d3Sopenharmony_ci sleep(timeForWaitCancelCapture); 691094332d3Sopenharmony_ci EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR); 692094332d3Sopenharmony_ci if (rc == HDI::Camera::V1_0::NO_ERROR) { 693094332d3Sopenharmony_ci CAMERA_LOGI("check Capture: CancelCapture success, captureId = %{public}d", captureId); 694094332d3Sopenharmony_ci } else { 695094332d3Sopenharmony_ci CAMERA_LOGE("check Capture: CancelCapture fail, rc = %{public}d, captureId = %{public}d", 696094332d3Sopenharmony_ci rc, captureId); 697094332d3Sopenharmony_ci } 698094332d3Sopenharmony_ci } 699094332d3Sopenharmony_ci } 700094332d3Sopenharmony_ci sleep(1); 701094332d3Sopenharmony_ci if (streamIds.size() > 0) { 702094332d3Sopenharmony_ci // release stream 703094332d3Sopenharmony_ci rc = (CamRetCode)streamOperator->ReleaseStreams(streamIds); 704094332d3Sopenharmony_ci EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR); 705094332d3Sopenharmony_ci if (rc == HDI::Camera::V1_0::NO_ERROR) { 706094332d3Sopenharmony_ci CAMERA_LOGI("check Capture: ReleaseStreams success."); 707094332d3Sopenharmony_ci } else { 708094332d3Sopenharmony_ci CAMERA_LOGE("check Capture: ReleaseStreams fail, rc = %{public}d, streamIds = %{public}d", 709094332d3Sopenharmony_ci rc, streamIds.front()); 710094332d3Sopenharmony_ci } 711094332d3Sopenharmony_ci } 712094332d3Sopenharmony_ci} 713094332d3Sopenharmony_ci 714094332d3Sopenharmony_civoid DemoCameraDeviceCallback::PrintStabiliInfo(const std::vector<uint8_t>& result) 715094332d3Sopenharmony_ci{ 716094332d3Sopenharmony_ci std::shared_ptr<CameraMetadata> metaData; 717094332d3Sopenharmony_ci MetadataUtils::ConvertVecToMetadata(result, metaData); 718094332d3Sopenharmony_ci 719094332d3Sopenharmony_ci if (metaData == nullptr) { 720094332d3Sopenharmony_ci CAMERA_LOGE("TestCameraBase: result is null"); 721094332d3Sopenharmony_ci return; 722094332d3Sopenharmony_ci } 723094332d3Sopenharmony_ci common_metadata_header_t* data = metaData->get(); 724094332d3Sopenharmony_ci if (data == nullptr) { 725094332d3Sopenharmony_ci CAMERA_LOGE("TestCameraBase: data is null"); 726094332d3Sopenharmony_ci return; 727094332d3Sopenharmony_ci } 728094332d3Sopenharmony_ci uint8_t videoStabiliMode; 729094332d3Sopenharmony_ci camera_metadata_item_t entry; 730094332d3Sopenharmony_ci int ret = FindCameraMetadataItem(data, OHOS_CONTROL_VIDEO_STABILIZATION_MODE, &entry); 731094332d3Sopenharmony_ci if (ret != 0) { 732094332d3Sopenharmony_ci CAMERA_LOGE("demo test: get OHOS_CONTROL_EXPOSURE_MODE error\n"); 733094332d3Sopenharmony_ci return; 734094332d3Sopenharmony_ci } 735094332d3Sopenharmony_ci videoStabiliMode = *(entry.data.u8); 736094332d3Sopenharmony_ci CAMERA_LOGI("videoStabiliMode: %{public}d", static_cast<int>(videoStabiliMode)); 737094332d3Sopenharmony_ci} 738094332d3Sopenharmony_ci 739094332d3Sopenharmony_civoid DemoCameraDeviceCallback::PrintFpsInfo(const std::vector<uint8_t>& result) 740094332d3Sopenharmony_ci{ 741094332d3Sopenharmony_ci std::shared_ptr<CameraMetadata> metaData; 742094332d3Sopenharmony_ci MetadataUtils::ConvertVecToMetadata(result, metaData); 743094332d3Sopenharmony_ci 744094332d3Sopenharmony_ci if (metaData == nullptr) { 745094332d3Sopenharmony_ci CAMERA_LOGE("TestCameraBase: result is null"); 746094332d3Sopenharmony_ci return; 747094332d3Sopenharmony_ci } 748094332d3Sopenharmony_ci common_metadata_header_t* data = metaData->get(); 749094332d3Sopenharmony_ci if (data == nullptr) { 750094332d3Sopenharmony_ci CAMERA_LOGE("TestCameraBase: data is null"); 751094332d3Sopenharmony_ci return; 752094332d3Sopenharmony_ci } 753094332d3Sopenharmony_ci std::vector<int32_t> fpsRange; 754094332d3Sopenharmony_ci camera_metadata_item_t entry; 755094332d3Sopenharmony_ci int ret = FindCameraMetadataItem(data, OHOS_CONTROL_FPS_RANGES, &entry); 756094332d3Sopenharmony_ci if (ret != 0) { 757094332d3Sopenharmony_ci CAMERA_LOGE("demo test: get OHOS_CONTROL_EXPOSURE_MODE error\n"); 758094332d3Sopenharmony_ci return; 759094332d3Sopenharmony_ci } 760094332d3Sopenharmony_ci 761094332d3Sopenharmony_ci for (int i = 0; i < entry.count; i++) { 762094332d3Sopenharmony_ci fpsRange.push_back(*(entry.data.i32 + i)); 763094332d3Sopenharmony_ci } 764094332d3Sopenharmony_ci CAMERA_LOGI("PrintFpsInfo fpsRange: [%{public}d, %{public}d]", fpsRange[0], fpsRange[1]); 765094332d3Sopenharmony_ci} 766094332d3Sopenharmony_ci 767094332d3Sopenharmony_ci#ifndef CAMERA_BUILT_ON_OHOS_LITE 768094332d3Sopenharmony_ciint32_t DemoCameraDeviceCallback::OnError(ErrorType type, int32_t errorCode) 769094332d3Sopenharmony_ci{ 770094332d3Sopenharmony_ci CAMERA_LOGI("demo test: OnError type : %{public}d, errorMsg : %{public}d", type, errorCode); 771094332d3Sopenharmony_ci} 772094332d3Sopenharmony_ci 773094332d3Sopenharmony_ciint32_t DemoCameraDeviceCallback::OnResult(uint64_t timestamp, const std::vector<uint8_t>& result) 774094332d3Sopenharmony_ci{ 775094332d3Sopenharmony_ci CAMERA_LOGI("%{public}s, enter.", __func__); 776094332d3Sopenharmony_ci PrintStabiliInfo(result); 777094332d3Sopenharmony_ci PrintFpsInfo(result); 778094332d3Sopenharmony_ci DealCameraMetadata(result); 779094332d3Sopenharmony_ci return RC_OK; 780094332d3Sopenharmony_ci} 781094332d3Sopenharmony_ci 782094332d3Sopenharmony_ciint32_t DemoCameraHostCallback::OnCameraStatus(const std::string& cameraId, CameraStatus status) 783094332d3Sopenharmony_ci{ 784094332d3Sopenharmony_ci CAMERA_LOGI("%{public}s, enter.", __func__); 785094332d3Sopenharmony_ci std::cout << "OnCameraStatus, enter, cameraId = " << cameraId << ", status = " << status << std::endl; 786094332d3Sopenharmony_ci return RC_OK; 787094332d3Sopenharmony_ci} 788094332d3Sopenharmony_ci 789094332d3Sopenharmony_ciint32_t DemoCameraHostCallback::OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) 790094332d3Sopenharmony_ci{ 791094332d3Sopenharmony_ci CAMERA_LOGI("%{public}s, enter. cameraId = %s, status = %d", 792094332d3Sopenharmony_ci __func__, cameraId.c_str(), static_cast<int>(status)); 793094332d3Sopenharmony_ci return RC_OK; 794094332d3Sopenharmony_ci} 795094332d3Sopenharmony_ci 796094332d3Sopenharmony_ciint32_t DemoCameraHostCallback::OnCameraEvent(const std::string& cameraId, CameraEvent event) 797094332d3Sopenharmony_ci{ 798094332d3Sopenharmony_ci CAMERA_LOGI("%{public}s, enter. cameraId = %s, event = %d", 799094332d3Sopenharmony_ci __func__, cameraId.c_str(), static_cast<int>(event)); 800094332d3Sopenharmony_ci std::cout << "OnCameraEvent, enter, cameraId = " << cameraId << ", event = " << event<< std::endl; 801094332d3Sopenharmony_ci return RC_OK; 802094332d3Sopenharmony_ci} 803094332d3Sopenharmony_ci 804094332d3Sopenharmony_ciint32_t DemoStreamOperatorCallback::OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds) 805094332d3Sopenharmony_ci{ 806094332d3Sopenharmony_ci CAMERA_LOGI("%{public}s, enter.", __func__); 807094332d3Sopenharmony_ci return RC_OK; 808094332d3Sopenharmony_ci} 809094332d3Sopenharmony_ci 810094332d3Sopenharmony_ciint32_t DemoStreamOperatorCallback::OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos) 811094332d3Sopenharmony_ci{ 812094332d3Sopenharmony_ci CAMERA_LOGI("%{public}s, enter.", __func__); 813094332d3Sopenharmony_ci return RC_OK; 814094332d3Sopenharmony_ci} 815094332d3Sopenharmony_ci 816094332d3Sopenharmony_civoid DemoCameraDeviceCallback::DealCameraMetadata(const std::vector<uint8_t> &settings) 817094332d3Sopenharmony_ci{ 818094332d3Sopenharmony_ci std::shared_ptr<CameraMetadata> result; 819094332d3Sopenharmony_ci MetadataUtils::ConvertVecToMetadata(settings, result); 820094332d3Sopenharmony_ci if (result == nullptr) { 821094332d3Sopenharmony_ci CAMERA_LOGE("TestCameraBase: result is null"); 822094332d3Sopenharmony_ci return; 823094332d3Sopenharmony_ci } 824094332d3Sopenharmony_ci common_metadata_header_t *data = result->get(); 825094332d3Sopenharmony_ci if (data == nullptr) { 826094332d3Sopenharmony_ci CAMERA_LOGE("data is null"); 827094332d3Sopenharmony_ci return; 828094332d3Sopenharmony_ci } 829094332d3Sopenharmony_ci for (auto it = DATA_BASE.cbegin(); it != DATA_BASE.cend(); it++) { 830094332d3Sopenharmony_ci std::string st = {}; 831094332d3Sopenharmony_ci st = MetadataItemDump(data, *it); 832094332d3Sopenharmony_ci CAMERA_LOGI("%{publid}s", st.c_str()); 833094332d3Sopenharmony_ci } 834094332d3Sopenharmony_ci} 835094332d3Sopenharmony_ci 836094332d3Sopenharmony_ciint32_t DemoStreamOperatorCallback::OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos) 837094332d3Sopenharmony_ci{ 838094332d3Sopenharmony_ci CAMERA_LOGI("%{public}s, enter.", __func__); 839094332d3Sopenharmony_ci return RC_OK; 840094332d3Sopenharmony_ci} 841094332d3Sopenharmony_ci 842094332d3Sopenharmony_ciint32_t DemoStreamOperatorCallback::OnFrameShutter(int32_t captureId, 843094332d3Sopenharmony_ci const std::vector<int32_t>& streamIds, uint64_t timestamp) 844094332d3Sopenharmony_ci{ 845094332d3Sopenharmony_ci CAMERA_LOGI("%{public}s, enter.", __func__); 846094332d3Sopenharmony_ci return RC_OK; 847094332d3Sopenharmony_ci} 848094332d3Sopenharmony_ci 849094332d3Sopenharmony_ci#endif 850