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