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