1 /*
2 * Copyright (c) 2021-2022 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
Close()488 void TestDisplay::Close()
489 {
490 CAMERA_LOGD("cameraDevice->Close().");
491 if (cameraDevice != nullptr) {
492 cameraDevice->Close();
493 cameraDevice = nullptr;
494 }
495 }
496
OpenCamera()497 void TestDisplay::OpenCamera()
498 {
499 if (cameraDevice == nullptr) {
500 cameraHost->GetCameraIds(cameraIds);
501 const OHOS::sptr<OHOS::Camera::CameraDeviceCallback> callback = new CameraDeviceCallback();
502 rc = (CamRetCode)cameraHost->OpenCamera(cameraIds.front(), callback, cameraDevice);
503 if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) {
504 std::cout << "==========[test log] OpenCamera failed, rc = " << rc << std::endl;
505 return;
506 }
507 std::cout << "==========[test log] OpenCamera success." << std::endl;
508 }
509 }
510
CalTime(struct timeval start,struct timeval end)511 float TestDisplay::CalTime(struct timeval start, struct timeval end)
512 {
513 float timeUse = 0;
514 timeUse = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); // 1000000:time
515 return timeUse;
516 }
517
AchieveStreamOperator()518 void TestDisplay::AchieveStreamOperator()
519 {
520 // Create and get streamOperator information
521 OHOS::sptr<DemoStreamOperatorCallback> streamOperatorCallback_ = new DemoStreamOperatorCallback();
522 rc = (CamRetCode)cameraDevice->GetStreamOperator(streamOperatorCallback_, streamOperator);
523 EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR);
524 if (rc == HDI::Camera::V1_0::NO_ERROR) {
525 CAMERA_LOGI("AchieveStreamOperator success.");
526 } else {
527 CAMERA_LOGE("AchieveStreamOperator fail, rc = %{public}d", rc);
528 }
529 }
530
StartStream(std::vector<StreamIntent> intents)531 void TestDisplay::StartStream(std::vector<StreamIntent> intents)
532 {
533 for (auto& intent : intents) {
534 if (intent == PREVIEW) {
535 if (streamCustomerPreview_ == nullptr) {
536 streamCustomerPreview_ = std::make_shared<StreamCustomer>();
537 }
538 streamInfoPre.streamId_ = STREAM_ID_PREVIEW;
539 streamInfoPre.width_ = PREVIEW_WIDTH; // 640:picture width
540 streamInfoPre.height_ = PREVIEW_HEIGHT; // 480:picture height
541 streamInfoPre.format_ = PIXEL_FMT_RGBA_8888;
542 streamInfoPre.dataspace_ = 8; // 8:picture dataspace
543 streamInfoPre.intent_ = intent;
544 streamInfoPre.tunneledMode_ = 5; // 5:tunnel mode
545 streamInfoPre.bufferQueue_ = new BufferProducerSequenceable(streamCustomerPreview_->CreateProducer());
546 ASSERT_NE(streamInfoPre.bufferQueue_, nullptr);
547 streamInfoPre.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size
548 CAMERA_LOGD("preview success.");
549 std::vector<StreamInfo>().swap(streamInfos);
550 streamInfos.push_back(streamInfoPre);
551 } else if (intent == VIDEO) {
552 if (streamCustomerVideo_ == nullptr) {
553 streamCustomerVideo_ = std::make_shared<StreamCustomer>();
554 }
555 streamInfoVideo.streamId_ = STREAM_ID_VIDEO;
556 streamInfoVideo.width_ = VIDEO_WIDTH; // 1280:picture width
557 streamInfoVideo.height_ = VIDEO_HEIGHT; // 960:picture height
558 streamInfoVideo.format_ = PIXEL_FMT_RGBA_8888;
559 streamInfoVideo.dataspace_ = 8; // 8:picture dataspace
560 streamInfoVideo.intent_ = intent;
561 streamInfoVideo.encodeType_ = ENCODE_TYPE_H264;
562 streamInfoVideo.tunneledMode_ = 5; // 5:tunnel mode
563 streamInfoVideo.bufferQueue_ = new BufferProducerSequenceable(streamCustomerVideo_->CreateProducer());
564 ASSERT_NE(streamInfoVideo.bufferQueue_, nullptr);
565 streamInfoVideo.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size
566 CAMERA_LOGD("video success.");
567 std::vector<StreamInfo>().swap(streamInfos);
568 streamInfos.push_back(streamInfoVideo);
569 } else if (intent == STILL_CAPTURE) {
570 if (streamCustomerCapture_ == nullptr) {
571 streamCustomerCapture_ = std::make_shared<StreamCustomer>();
572 }
573 streamInfoCapture.streamId_ = STREAM_ID_CAPTURE;
574 streamInfoCapture.width_ = CAPTURE_WIDTH; // 1280:picture width
575 streamInfoCapture.height_ = CAPTURE_HEIGHT; // 960:picture height
576 streamInfoCapture.format_ = PIXEL_FMT_RGBA_8888;
577 streamInfoCapture.dataspace_ = 8; // 8:picture dataspace
578 streamInfoCapture.intent_ = intent;
579 streamInfoCapture.encodeType_ = ENCODE_TYPE_JPEG;
580 streamInfoCapture.tunneledMode_ = 5; // 5:tunnel mode
581 streamInfoCapture.bufferQueue_ = new BufferProducerSequenceable(streamCustomerCapture_->CreateProducer());
582 ASSERT_NE(streamInfoCapture.bufferQueue_, nullptr);
583 streamInfoCapture.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size
584 CAMERA_LOGD("capture success.");
585 std::vector<StreamInfo>().swap(streamInfos);
586 streamInfos.push_back(streamInfoCapture);
587 } else if (intent == ANALYZE) {
588 if (streamCustomerAnalyze_ == nullptr) {
589 streamCustomerAnalyze_ = std::make_shared<StreamCustomer>();
590 }
591 streamInfoAnalyze.streamId_ = STREAM_ID_ANALYZE;
592 streamInfoAnalyze.width_ = ANALYZE_WIDTH; // 640:picture width
593 streamInfoAnalyze.height_ = ANALYZE_HEIGHT; // 480:picture height
594 streamInfoAnalyze.format_ = PIXEL_FMT_RGBA_8888;
595 streamInfoAnalyze.dataspace_ = 8; // 8:picture dataspace
596 streamInfoAnalyze.intent_ = intent;
597 streamInfoAnalyze.tunneledMode_ = 5; // 5:tunnel mode
598 streamInfoAnalyze.bufferQueue_ = new BufferProducerSequenceable(streamCustomerAnalyze_->CreateProducer());
599 ASSERT_NE(streamInfoAnalyze.bufferQueue_, nullptr);
600 streamInfoAnalyze.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size
601 CAMERA_LOGD("analyze success.");
602 std::vector<StreamInfo>().swap(streamInfos);
603 streamInfos.push_back(streamInfoAnalyze);
604 }
605 rc = (CamRetCode)streamOperator->CreateStreams(streamInfos);
606 EXPECT_EQ(false, rc != HDI::Camera::V1_0::NO_ERROR);
607 if (rc == HDI::Camera::V1_0::NO_ERROR) {
608 CAMERA_LOGI("CreateStreams success.");
609 } else {
610 CAMERA_LOGE("CreateStreams fail, rc = %{public}d", rc);
611 }
612
613 rc = (CamRetCode)streamOperator->CommitStreams(NORMAL, ability_);
614 EXPECT_EQ(false, rc != HDI::Camera::V1_0::NO_ERROR);
615 if (rc == HDI::Camera::V1_0::NO_ERROR) {
616 CAMERA_LOGI("CommitStreams success.");
617 } else {
618 CAMERA_LOGE("CommitStreams fail, rc = %{public}d", rc);
619 }
620 }
621 }
622
StartCapture(int streamId,int captureId,bool shutterCallback,bool isStreaming)623 void TestDisplay::StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming)
624 {
625 // Get preview
626 captureInfo.streamIds_ = {streamId};
627 captureInfo.captureSetting_ = ability_;
628 captureInfo.enableShutterCallback_ = shutterCallback;
629 rc = (CamRetCode)streamOperator->Capture(captureId, captureInfo, isStreaming);
630 EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR);
631 if (rc == HDI::Camera::V1_0::NO_ERROR) {
632 CAMERA_LOGI("check Capture: Capture success, captureId = %{public}d", captureId);
633 } else {
634 CAMERA_LOGE("check Capture: Capture fail, rc = %{public}d, captureId = %{public}d", rc, captureId);
635 }
636 if (captureId == CAPTURE_ID_PREVIEW) {
637 streamCustomerPreview_->ReceiveFrameOn(nullptr);
638 } else if (captureId == CAPTURE_ID_CAPTURE) {
639 streamCustomerCapture_->ReceiveFrameOn([this](const unsigned char *addr, const uint32_t size) {
640 StoreImage(addr, size);
641 });
642 } else if (captureId == CAPTURE_ID_VIDEO) {
643 OpenVideoFile();
644 streamCustomerVideo_->ReceiveFrameOn([this](const unsigned char *addr, const uint32_t size) {
645 StoreVideo(addr, size);
646 });
647 } else if (captureId == CAPTURE_ID_ANALYZE) {
648 streamCustomerAnalyze_->ReceiveFrameOn([this](const unsigned char *addr, const uint32_t size) {
649 PrintFaceDetectInfo(addr, size);
650 });
651 }
652 sleep(2); // 2:sleep two second
653 }
654
StopStream(std::vector<int> & captureIds,std::vector<int> & streamIds)655 void TestDisplay::StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds)
656 {
657 constexpr uint32_t TIME_FOR_WAIT_CANCEL_CAPTURE = 2;
658 sleep(TIME_FOR_WAIT_CANCEL_CAPTURE);
659 if (sizeof(captureIds) > 0) {
660 for (auto &captureId : captureIds) {
661 if (captureId == CAPTURE_ID_PREVIEW) {
662 streamCustomerPreview_->ReceiveFrameOff();
663 } else if (captureId == CAPTURE_ID_CAPTURE) {
664 streamCustomerCapture_->ReceiveFrameOff();
665 } else if (captureId == CAPTURE_ID_VIDEO) {
666 streamCustomerVideo_->ReceiveFrameOff();
667 sleep(1);
668 CloseFd();
669 } else if (captureId == CAPTURE_ID_ANALYZE) {
670 streamCustomerAnalyze_->ReceiveFrameOff();
671 }
672 }
673 for (const auto &captureId : captureIds) {
674 CAMERA_LOGI("check Capture: CancelCapture success, captureId = %{public}d", captureId);
675 rc = (CamRetCode)streamOperator->CancelCapture(captureId);
676 sleep(TIME_FOR_WAIT_CANCEL_CAPTURE);
677 EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR);
678 if (rc == HDI::Camera::V1_0::NO_ERROR) {
679 CAMERA_LOGI("check Capture: CancelCapture success, captureId = %{public}d", captureId);
680 } else {
681 CAMERA_LOGE("check Capture: CancelCapture fail, rc = %{public}d, captureId = %{public}d",
682 rc, captureId);
683 }
684 }
685 }
686 sleep(1);
687 if (sizeof(streamIds) > 0) {
688 // release stream
689 rc = (CamRetCode)streamOperator->ReleaseStreams(streamIds);
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: ReleaseStreams success.");
693 } else {
694 CAMERA_LOGE("check Capture: ReleaseStreams fail, rc = %{public}d, streamIds = %{public}d",
695 rc, streamIds.front());
696 }
697 }
698 }
699
PrintStabiliInfo(const std::vector<uint8_t> & result)700 void DemoCameraDeviceCallback::PrintStabiliInfo(const std::vector<uint8_t>& result)
701 {
702 std::shared_ptr<CameraMetadata> metaData;
703 MetadataUtils::ConvertVecToMetadata(result, metaData);
704
705 if (metaData == nullptr) {
706 CAMERA_LOGE("TestDisplay: result is null");
707 return;
708 }
709 common_metadata_header_t* data = metaData->get();
710 if (data == nullptr) {
711 CAMERA_LOGE("TestDisplay: data is null");
712 return;
713 }
714 uint8_t videoStabiliMode;
715 camera_metadata_item_t entry;
716 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_VIDEO_STABILIZATION_MODE, &entry);
717 if (ret != 0) {
718 CAMERA_LOGE("demo test: get OHOS_CONTROL_EXPOSURE_MODE error\n");
719 return;
720 }
721 videoStabiliMode = *(entry.data.u8);
722 CAMERA_LOGI("videoStabiliMode: %{public}d", static_cast<int>(videoStabiliMode));
723 }
724
PrintFpsInfo(const std::vector<uint8_t> & result)725 void DemoCameraDeviceCallback::PrintFpsInfo(const std::vector<uint8_t>& result)
726 {
727 std::shared_ptr<CameraMetadata> metaData;
728 MetadataUtils::ConvertVecToMetadata(result, metaData);
729
730 if (metaData == nullptr) {
731 CAMERA_LOGE("TestDisplay: result is null");
732 return;
733 }
734 common_metadata_header_t* data = metaData->get();
735 if (data == nullptr) {
736 CAMERA_LOGE("TestDisplay: data is null");
737 return;
738 }
739 std::vector<int32_t> fpsRange;
740 camera_metadata_item_t entry;
741 int ret = FindCameraMetadataItem(data, OHOS_CONTROL_FPS_RANGES, &entry);
742 if (ret != 0) {
743 CAMERA_LOGE("demo test: get OHOS_CONTROL_EXPOSURE_MODE error\n");
744 return;
745 }
746
747 for (int i = 0; i < entry.count; i++) {
748 fpsRange.push_back(*(entry.data.i32 + i));
749 }
750 CAMERA_LOGI("PrintFpsInfo fpsRange: [%{public}d, %{public}d]", fpsRange[0], fpsRange[1]);
751 }
752
753 #ifndef CAMERA_BUILT_ON_OHOS_LITE
OnError(ErrorType type,int32_t errorCode)754 int32_t DemoCameraDeviceCallback::OnError(ErrorType type, int32_t errorCode)
755 {
756 CAMERA_LOGI("demo test: OnError type : %{public}d, errorMsg : %{public}d", type, errorCode);
757 }
758
OnResult(uint64_t timestamp,const std::vector<uint8_t> & result)759 int32_t DemoCameraDeviceCallback::OnResult(uint64_t timestamp, const std::vector<uint8_t>& result)
760 {
761 CAMERA_LOGI("%{public}s, enter.", __func__);
762 PrintStabiliInfo(result);
763 PrintFpsInfo(result);
764 DealCameraMetadata(result);
765 return RC_OK;
766 }
767
OnCameraStatus(const std::string & cameraId,CameraStatus status)768 int32_t DemoCameraHostCallback::OnCameraStatus(const std::string& cameraId, CameraStatus status)
769 {
770 CAMERA_LOGI("%{public}s, enter. cameraId = %{public}s, status = %{public}d",
771 __func__, cameraId.c_str(), static_cast<int>(status));
772 return RC_OK;
773 }
774
OnFlashlightStatus(const std::string & cameraId,FlashlightStatus status)775 int32_t DemoCameraHostCallback::OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status)
776 {
777 CAMERA_LOGI("%{public}s, enter. cameraId = %{public}s, status = %{public}d",
778 __func__, cameraId.c_str(), static_cast<int>(status));
779 return RC_OK;
780 }
781
OnCameraEvent(const std::string & cameraId,CameraEvent event)782 int32_t DemoCameraHostCallback::OnCameraEvent(const std::string& cameraId, CameraEvent event)
783 {
784 CAMERA_LOGI("%{public}s, enter. cameraId = %{public}s, event = %{public}d",
785 __func__, cameraId.c_str(), static_cast<int>(event));
786 return RC_OK;
787 }
788
OnCaptureStarted(int32_t captureId,const std::vector<int32_t> & streamIds)789 int32_t DemoStreamOperatorCallback::OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds)
790 {
791 CAMERA_LOGI("%{public}s, enter.", __func__);
792 return RC_OK;
793 }
794
OnCaptureEnded(int32_t captureId,const std::vector<CaptureEndedInfo> & infos)795 int32_t DemoStreamOperatorCallback::OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos)
796 {
797 CAMERA_LOGI("%{public}s, enter.", __func__);
798 return RC_OK;
799 }
800
DealCameraMetadata(const std::vector<uint8_t> & settings)801 void DemoCameraDeviceCallback::DealCameraMetadata(const std::vector<uint8_t> &settings)
802 {
803 std::shared_ptr<CameraMetadata> result;
804 MetadataUtils::ConvertVecToMetadata(settings, result);
805 if (result == nullptr) {
806 CAMERA_LOGE("TestDisplay: result is null");
807 return;
808 }
809 common_metadata_header_t *data = result->get();
810 if (data == nullptr) {
811 CAMERA_LOGE("data is null");
812 return;
813 }
814 for (auto it = DATA_BASE.cbegin(); it != DATA_BASE.cend(); it++) {
815 std::string st = {};
816 st = MetadataItemDump(data, *it);
817 CAMERA_LOGI("%{publid}s", st.c_str());
818 }
819 }
820
OnCaptureError(int32_t captureId,const std::vector<CaptureErrorInfo> & infos)821 int32_t DemoStreamOperatorCallback::OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos)
822 {
823 CAMERA_LOGI("%{public}s, enter.", __func__);
824 return RC_OK;
825 }
826
OnFrameShutter(int32_t captureId,const std::vector<int32_t> & streamIds,uint64_t timestamp)827 int32_t DemoStreamOperatorCallback::OnFrameShutter(int32_t captureId,
828 const std::vector<int32_t>& streamIds, uint64_t timestamp)
829 {
830 CAMERA_LOGI("%{public}s, enter.", __func__);
831 return RC_OK;
832 }
833
834 #endif
835