• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
TestDisplay()18 TestDisplay::TestDisplay()
19 {
20 }
21 
CameraHostImplGetInstance(void)22 sptr<ICameraHost> TestDisplay::CameraHostImplGetInstance(void)
23 {
24     using OHOS::Camera::CameraHostImpl;
25     CameraHostImpl *service = new (std::nothrow) CameraHostImpl();
26     if (service == nullptr) {
27         return nullptr;
28     }
29 
30     service->Init();
31     return service;
32 }
33 
GetCurrentLocalTimeStamp()34 uint64_t TestDisplay::GetCurrentLocalTimeStamp()
35 {
36     std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp =
37         std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
38     auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
39     return tmp.count();
40 }
41 
StoreImage(const void * bufStart,const uint32_t size) const42 void TestDisplay::StoreImage(const void *bufStart, const uint32_t size) const
43 {
44     constexpr uint32_t pathLen = 64;
45     char path[pathLen] = {0};
46 #ifdef CAMERA_BUILT_ON_OHOS_LITE
47     char prefix[] = "/userdata/photo/";
48 #else
49     char prefix[] = "/data/";
50 #endif
51 
52     int imgFD = 0;
53     int ret = 0;
54 
55     struct timeval start = {};
56     gettimeofday(&start, nullptr);
57     if (sprintf_s(path, sizeof(path), "%spicture_%ld.jpeg", prefix, start.tv_usec) < 0) {
58         CAMERA_LOGE("sprintf_s error .....\n");
59         return;
60     }
61 
62     imgFD = open(path, O_RDWR | O_CREAT, 00766); // 00766:file operate permission
63     if (imgFD == -1) {
64         CAMERA_LOGE("demo test:open image file error %{public}s.....\n", strerror(errno));
65         return;
66     }
67 
68     CAMERA_LOGI("demo test:StoreImage %{public}s buf_start == %{public}p size == %{public}d\n", path, bufStart, size);
69 
70     ret = write(imgFD, bufStart, size);
71     if (ret == -1) {
72         CAMERA_LOGE("demo test:write image file error %{public}s.....\n", strerror(errno));
73     }
74 
75     close(imgFD);
76 }
77 
StoreVideo(const void * bufStart,const uint32_t size) const78 void TestDisplay::StoreVideo(const void *bufStart, const uint32_t size) const
79 {
80     int ret = 0;
81 
82     ret = write(videoFd_, bufStart, size);
83     if (ret == -1) {
84         CAMERA_LOGE("demo test:write video file error %{public}s.....\n", strerror(errno));
85     }
86     CAMERA_LOGI("demo test:StoreVideo buf_start == %{public}p size == %{public}d\n", bufStart, size);
87 }
88 
OpenVideoFile()89 void TestDisplay::OpenVideoFile()
90 {
91     constexpr uint32_t pathLen = 64;
92     char path[pathLen] = {0};
93 #ifdef CAMERA_BUILT_ON_OHOS_LITE
94     char prefix[] = "/userdata/video/";
95 #else
96     char prefix[] = "/data/";
97 #endif
98     auto seconds = time(nullptr);
99     if (sprintf_s(path, sizeof(path), "%svideo%ld.h264", prefix, seconds) < 0) {
100         CAMERA_LOGE("%{public}s: sprintf  failed", __func__);
101         return;
102     }
103     videoFd_ = open(path, O_RDWR | O_CREAT, 00766); // 00766:file operate permission
104     if (videoFd_ < 0) {
105         CAMERA_LOGE("demo test: StartVideo open %s %{public}s failed", path, strerror(errno));
106     }
107 }
108 
CloseFd()109 void TestDisplay::CloseFd()
110 {
111     close(videoFd_);
112     videoFd_ = -1;
113 }
114 
PrintFaceDetectInfo(const void * bufStart,const uint32_t size) const115 void TestDisplay::PrintFaceDetectInfo(const void *bufStart, const uint32_t size) const
116 {
117     common_metadata_header_t* data = static_cast<common_metadata_header_t*>((const_cast<void*>(bufStart)));
118     camera_metadata_item_t entry;
119     int ret = 0;
120     ret = FindCameraMetadataItem(data, OHOS_STATISTICS_FACE_DETECT_SWITCH, &entry);
121     if (ret != 0) {
122         CAMERA_LOGE("demo test: get OHOS_STATISTICS_FACE_DETECT_SWITCH error\n");
123         return;
124     }
125     uint8_t switchValue = *(entry.data.u8);
126     CAMERA_LOGI("demo test: switchValue=%{public}d", switchValue);
127 
128     ret = FindCameraMetadataItem(data, OHOS_STATISTICS_FACE_RECTANGLES, &entry);
129     if (ret != 0) {
130         CAMERA_LOGE("demo test: get OHOS_STATISTICS_FACE_RECTANGLES error\n");
131         return;
132     }
133     uint32_t rectCount = entry.count;
134     std::cout << "==========[test log] PrintFaceDetectInfo rectCount=" << rectCount << std::endl;
135     CAMERA_LOGI("demo test: rectCount=%{public}d", rectCount);
136     std::vector<std::vector<float>> faceRectangles;
137     std::vector<float> faceRectangle;
138     for (int i = 0; i < rectCount; i++) {
139         faceRectangle.push_back(*(entry.data.f + i));
140     }
141     faceRectangles.push_back(faceRectangle);
142     for (std::vector<std::vector<float>>::iterator it = faceRectangles.begin(); it < faceRectangles.end(); it++) {
143         for (std::vector<float>::iterator innerIt = (*it).begin(); innerIt < (*it).end(); innerIt++) {
144             std::cout << "==========[test log] PrintFaceDetectInfo innerIt : " << *innerIt << std::endl;
145             CAMERA_LOGI("demo test: innerIt : %{public}f \n", *innerIt);
146         }
147     }
148 
149     ret = FindCameraMetadataItem(data, OHOS_STATISTICS_FACE_IDS, &entry);
150     if (ret != 0) {
151         CAMERA_LOGE("demo test: get OHOS_STATISTICS_FACE_IDS error\n");
152         return;
153     }
154     uint32_t idCount = entry.count;
155     std::cout << "==========[test log] PrintFaceDetectInfo idCount=" << idCount << std::endl;
156     CAMERA_LOGI("demo test: idCount=%{public}d", idCount);
157     std::vector<int32_t> faceIds;
158     for (int i = 0; i < idCount; i++) {
159         faceIds.push_back(*(entry.data.i32 + i));
160     }
161     for (auto it = faceIds.begin(); it != faceIds.end(); it++) {
162         std::cout << "==========[test log] PrintFaceDetectInfo faceIds : " << *it << std::endl;
163         CAMERA_LOGI("demo test: faceIds : %{public}d\n", *it);
164     }
165 }
166 
SaveYUV(char * type,unsigned char * buffer,int32_t size)167 int32_t TestDisplay::SaveYUV(char* type, unsigned char* buffer, int32_t size)
168 {
169     int ret;
170     char path[PATH_MAX] = {0};
171     ret = sprintf_s(path, sizeof(path) / sizeof(path[0]), "/mnt/yuv/%s_%lld.yuv", type, GetCurrentLocalTimeStamp());
172     if (ret < 0) {
173         CAMERA_LOGE("%s, sprintf_s failed, errno = %s.", __FUNCTION__, strerror(errno));
174         return -1;
175     }
176     CAMERA_LOGI("%s, save yuv to file %s", __FUNCTION__, path);
177     system("mkdir -p /mnt/yuv");
178     int imgFd = open(path, O_RDWR | O_CREAT, 00766); // 00766: file permissions
179     if (imgFd == -1) {
180         CAMERA_LOGI("%s, open file failed, errno = %s.", __FUNCTION__, strerror(errno));
181         return -1;
182     }
183     ret = write(imgFd, buffer, size);
184     if (ret == -1) {
185         CAMERA_LOGI("%s, write file failed, error = %s", __FUNCTION__, strerror(errno));
186         close(imgFd);
187         return -1;
188     }
189     close(imgFd);
190     return 0;
191 }
192 
DoFbMunmap(unsigned char * addr)193 int TestDisplay::DoFbMunmap(unsigned char* addr)
194 {
195     int rc;
196     unsigned int size = vinfo_.xres * vinfo_.yres * vinfo_.bits_per_pixel / 8; // 8:picture size;
197     CAMERA_LOGI("main test:munmapped size = %d, virt_addr = 0x%p\n", size, addr);
198     rc = (munmap(addr, finfo_.smem_len));
199     return rc;
200 }
201 
DoFbMmap(int * pmemfd)202 unsigned char* TestDisplay::DoFbMmap(int* pmemfd)
203 {
204     unsigned char* ret;
205     int screensize = vinfo_.xres * vinfo_.yres * vinfo_.bits_per_pixel / 8; // 8:picture size
206     ret = (unsigned char*)mmap(NULL, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, *pmemfd, 0);
207     if (ret == MAP_FAILED) {
208         CAMERA_LOGE("main test:do_mmap: pmem mmap() failed: %s (%d)\n", strerror(errno), errno);
209         return NULL;
210     }
211     CAMERA_LOGI("main test:do_mmap: pmem mmap fd %d ptr %p len %u\n", *pmemfd, ret, screensize);
212     return ret;
213 }
214 
FBLog()215 void TestDisplay::FBLog()
216 {
217     CAMERA_LOGI("the fixed information is as follow:\n");
218     CAMERA_LOGI("id=%s\n", finfo_.id);
219     CAMERA_LOGI("sem_start=%lx\n", finfo_.smem_start);
220     CAMERA_LOGI("smem_len=%u\n", finfo_.smem_len);
221     CAMERA_LOGI("type=%u\n", finfo_.type);
222     CAMERA_LOGI("line_length=%u\n", finfo_.line_length);
223     CAMERA_LOGI("mmio_start=%lu\n", finfo_.mmio_start);
224     CAMERA_LOGI("mmio_len=%d\n", finfo_.mmio_len);
225     CAMERA_LOGI("visual=%d\n", finfo_.visual);
226 
227     CAMERA_LOGI("variable information is as follow:\n");
228     CAMERA_LOGI("The xres is :%u\n", vinfo_.xres);
229     CAMERA_LOGI("The yres is :%u\n", vinfo_.yres);
230     CAMERA_LOGI("xres_virtual=%u\n", vinfo_.xres_virtual);
231     CAMERA_LOGI("yres_virtual=%u\n", vinfo_.yres_virtual);
232     CAMERA_LOGI("xoffset=%u\n", vinfo_.xoffset);
233     CAMERA_LOGI("yoffset=%u\n", vinfo_.yoffset);
234     CAMERA_LOGI("bits_per_pixel is :%u\n", vinfo_.bits_per_pixel);
235     CAMERA_LOGI("red.offset=%u\n", vinfo_.red.offset);
236     CAMERA_LOGI("red.length=%u\n", vinfo_.red.length);
237     CAMERA_LOGI("red.msb_right=%u\n", vinfo_.red.msb_right);
238     CAMERA_LOGI("green.offset=%d\n", vinfo_.green.offset);
239     CAMERA_LOGI("green.length=%d\n", vinfo_.green.length);
240     CAMERA_LOGI("green.msb_right=%d\n", vinfo_.green.msb_right);
241     CAMERA_LOGI("blue.offset=%d\n", vinfo_.blue.offset);
242     CAMERA_LOGI("blue.length=%d\n", vinfo_.blue.length);
243     CAMERA_LOGI("blue.msb_right=%d\n", vinfo_.blue.msb_right);
244     CAMERA_LOGI("transp.offset=%d\n", vinfo_.transp.offset);
245     CAMERA_LOGI("transp.length=%d\n", vinfo_.transp.length);
246     CAMERA_LOGI("transp.msb_right=%d\n", vinfo_.transp.msb_right);
247     CAMERA_LOGI("height=%x\n", vinfo_.height);
248 }
249 
FBInit()250 OHOS::Camera::RetCode TestDisplay::FBInit()
251 {
252     fbFd_ = open("/dev/fb0", O_RDWR);
253     if (fbFd_ < 0) {
254         CAMERA_LOGE("main test:cannot open framebuffer %s file node\n", "/dev/fb0");
255         return RC_ERROR;
256     }
257 
258     if (ioctl(fbFd_, FBIOGET_VSCREENINFO, &vinfo_) < 0) {
259         CAMERA_LOGE("main test:cannot retrieve vscreenInfo!\n");
260         close(fbFd_);
261         return RC_ERROR;
262     }
263 
264     if (ioctl(fbFd_, FBIOGET_FSCREENINFO, &finfo_) < 0) {
265         CAMERA_LOGE("main test:can't retrieve fscreenInfo!\n");
266         close(fbFd_);
267         return RC_ERROR;
268     }
269 
270     FBLog();
271 
272     CAMERA_LOGI("main test:allocating display buffer memory\n");
273     displayBuf_ = DoFbMmap(&fbFd_);
274     if (displayBuf_ == NULL) {
275         CAMERA_LOGE("main test:error displayBuf_ mmap error\n");
276         close(fbFd_);
277         return RC_ERROR;
278     }
279     return RC_OK;
280 }
281 
ProcessImage(const unsigned char * p,unsigned char * fbp)282 void TestDisplay::ProcessImage(const unsigned char* p, unsigned char* fbp)
283 {
284     unsigned char* in = (unsigned char*)p;
285     int width = 640; // 640:Displays the size of the width
286     int height = 480; // 480:Displays the size of the height
287     int istride = 1280; // 1280:Initial value of span
288     int x, y, j;
289     int y0, u, v, r, g, b;
290     int32_t location = 0;
291     int xpos = (vinfo_.xres - width) / 2;
292     int ypos = (vinfo_.yres - height) / 2;
293     int y_pos, u_pos, v_pos;
294 
295     y_pos = 0; // 0:Pixel initial value
296     u_pos = 1; // 1:Pixel initial value
297     v_pos = 3; // 3:Pixel initial value
298     constexpr int Y_POS_RANGE = 2;
299     constexpr int POS_RANGE = 4;
300     for (y = ypos; y < (height + ypos); y++) {
301         for (j = 0, x = xpos; j < width; j++, x++) {
302             location = (x + vinfo_.xoffset) * (vinfo_.bits_per_pixel / 8) + // 8: The bytes for each time
303             (y + vinfo_.yoffset) * finfo_.line_length; // add one y number of rows at a time
304 
305             y0 = in[y_pos];
306             u = in[u_pos] - 128; // 128:display size
307             v = in[v_pos] - 128; // 128:display size
308 
309             r = RANGE_LIMIT(y0 + v + ((v * 103) >> 8)); // 103,8:display range
310             g = RANGE_LIMIT(y0 - ((u * 88) >> 8) - ((v * 183) >> 8)); // 88,8,183:display range
311             b = RANGE_LIMIT(y0 + u + ((u * 198) >> 8)); // 198,8:display range
312 
313             fbp[location + 1] = ((r & 0xF8) | (g >> 5)); // 5:display range
314             fbp[location + 0] = (((g & 0x1C) << 3) | (b >> 3)); // 3:display range
315 
316             y_pos += Y_POS_RANGE; // 2:display range
317 
318             if (j & 0x01) {
319                 u_pos += POS_RANGE; // 4:display range
320                 v_pos += POS_RANGE; // 4:display range
321             }
322         }
323 
324         y_pos = 0; // 0:Pixel initial value
325         u_pos = 1; // 1:Pixel initial value
326         v_pos = 3; // 3:Pixel initial value
327         in += istride; // add one y number of rows at a time
328     }
329 }
330 
LcdDrawScreen(unsigned char * displayBuf_,unsigned char * addr)331 void TestDisplay::LcdDrawScreen(unsigned char* displayBuf_, unsigned char* addr)
332 {
333     ProcessImage(addr, displayBuf_);
334 }
335 
BufferCallback(void * addr,int choice)336 void TestDisplay::BufferCallback(void* addr, int choice)
337 {
338     if (choice == preview_mode) {
339         LcdDrawScreen(displayBuf_, (unsigned char*)addr);
340         return;
341     } else {
342         LcdDrawScreen(displayBuf_, (unsigned char*)addr);
343         std::cout << "==========[test log] capture start saveYuv......" << std::endl;
344         SaveYUV("capture", (unsigned char*)addr, bufSize_);
345         std::cout << "==========[test log] capture end saveYuv......" << std::endl;
346         return;
347     }
348 }
349 
Init()350 void TestDisplay::Init()
351 {
352     std::shared_ptr<OHOS::Camera::IDeviceManager> deviceManager = OHOS::Camera::IDeviceManager::GetInstance();
353     if (!init_flag) {
354         deviceManager->Init();
355         init_flag = 1;
356     }
357     constexpr const char *DEMO_SERVICE_NAME = "camera_service";
358     std::cout << "==========[test log] TestDisplay::Init()." << std::endl;
359     if (cameraHost == nullptr) {
360         cameraHost = ICameraHost::Get(DEMO_SERVICE_NAME, false);
361         std::cout << "==========[test log] Camera::CameraHost::CreateCameraHost();" << std::endl;
362         if (cameraHost == nullptr) {
363             std::cout << "==========[test log] CreateCameraHost failed." << std::endl;
364             return;
365         }
366         std::cout << "==========[test log] CreateCameraHost success." << std::endl;
367     }
368 
369     OHOS::sptr<DemoCameraHostCallback> cameraHostCallback = new DemoCameraHostCallback();
370     OHOS::Camera::RetCode ret = cameraHost->SetCallback(cameraHostCallback);
371     if (ret != HDI::Camera::V1_0::NO_ERROR) {
372         std::cout << "==========[test log] SetCallback failed." << std::endl;
373         return;
374     } else {
375         std::cout << "==========[test log] SetCallback success." << std::endl;
376     }
377 
378     if (cameraDevice == nullptr) {
379         cameraHost->GetCameraIds(cameraIds);
380         cameraHost->GetCameraAbility(cameraIds.front(), ability_);
381         MetadataUtils::ConvertVecToMetadata(ability_, ability);
382         const OHOS::sptr<DemoCameraDeviceCallback> callback = new DemoCameraDeviceCallback();
383         rc = (CamRetCode)cameraHost->OpenCamera(cameraIds.front(), callback, cameraDevice);
384         if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) {
385             std::cout << "==========[test log] OpenCamera failed, rc = " << rc << std::endl;
386             return;
387         }
388         std::cout << "==========[test log]  OpenCamera success." << std::endl;
389     }
390 }
391 
UsbInit()392 void TestDisplay::UsbInit()
393 {
394     std::shared_ptr<OHOS::Camera::IDeviceManager> deviceManager = OHOS::Camera::IDeviceManager::GetInstance();
395     if (!init_flag) {
396         deviceManager->Init();
397         init_flag = 1;
398     }
399     constexpr const char *DEMO_SERVICE_NAME = "camera_service";
400     if (cameraHost == nullptr) {
401         cameraHost = ICameraHost::Get(DEMO_SERVICE_NAME, false);
402         if (cameraHost == nullptr) {
403             std::cout << "==========[test log] CreateCameraHost failed." << std::endl;
404             return;
405         }
406         std::cout << "==========[test log] CreateCameraHost success." << std::endl;
407     }
408 
409     OHOS::sptr<DemoCameraHostCallback> cameraHostCallback = new DemoCameraHostCallback();
410     OHOS::Camera::RetCode ret = cameraHost->SetCallback(cameraHostCallback);
411     if (ret != HDI::Camera::V1_0::NO_ERROR) {
412         std::cout << "==========[test log] SetCallback failed." << std::endl;
413         return;
414     } else {
415         std::cout << "==========[test log] SetCallback success." << std::endl;
416     }
417 }
418 
Close()419 void TestDisplay::Close()
420 {
421     std::cout << "==========[test log] cameraDevice->Close()." << std::endl;
422     if (cameraDevice != nullptr) {
423         cameraDevice->Close();
424         cameraDevice = nullptr;
425     }
426 }
427 
OpenCamera()428 void TestDisplay::OpenCamera()
429 {
430     if (cameraDevice == nullptr) {
431         cameraHost->GetCameraIds(cameraIds);
432         const OHOS::sptr<OHOS::Camera::CameraDeviceCallback> callback = new CameraDeviceCallback();
433         rc = (CamRetCode)cameraHost->OpenCamera(cameraIds.front(), callback, cameraDevice);
434         if (rc != HDI::Camera::V1_0::NO_ERROR || cameraDevice == nullptr) {
435             std::cout << "==========[test log] OpenCamera failed, rc = " << rc << std::endl;
436             return;
437         }
438         std::cout << "==========[test log]  OpenCamera success." << std::endl;
439     }
440 }
441 
calTime(struct timeval start,struct timeval end)442 float TestDisplay::calTime(struct timeval start, struct timeval end)
443 {
444     float time_use = 0;
445     time_use = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); // 1000000:time
446     return time_use;
447 }
448 
AchieveStreamOperator()449 void TestDisplay::AchieveStreamOperator()
450 {
451     // Create and get streamOperator information
452     OHOS::sptr<DemoStreamOperatorCallback> streamOperatorCallback = new DemoStreamOperatorCallback();
453     rc = (CamRetCode)cameraDevice->GetStreamOperator(streamOperatorCallback, streamOperator);
454     EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR);
455     if (rc == HDI::Camera::V1_0::NO_ERROR) {
456         std::cout << "==========[test log] AchieveStreamOperator success." << std::endl;
457     } else {
458         std::cout << "==========[test log] AchieveStreamOperator fail, rc = " << rc << std::endl;
459     }
460 }
461 
StartStream(std::vector<StreamIntent> intents)462 void TestDisplay::StartStream(std::vector<StreamIntent> intents)
463 {
464     for (auto& intent : intents) {
465         if (intent == PREVIEW) {
466             if (streamCustomerPreview_ == nullptr) {
467                 streamCustomerPreview_ = std::make_shared<StreamCustomer>();
468             }
469             streamInfoPre.streamId_ = streamId_preview;
470             streamInfoPre.width_ = PREVIEW_WIDTH; // 640:picture width
471             streamInfoPre.height_ = PREVIEW_HEIGHT; // 480:picture height
472             streamInfoPre.format_ = PIXEL_FMT_RGBA_8888;
473             streamInfoPre.dataspace_ = 8; // 8:picture dataspace
474             streamInfoPre.intent_ = intent;
475             streamInfoPre.tunneledMode_ = 5; // 5:tunnel mode
476             streamInfoPre.bufferQueue_ = new BufferProducerSequenceable(streamCustomerPreview_->CreateProducer());
477             streamInfoPre.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size
478             std::cout << "==========[test log]preview success." << std::endl;
479             std::vector<StreamInfo>().swap(streamInfos);
480             streamInfos.push_back(streamInfoPre);
481         } else if (intent == VIDEO) {
482             if (streamCustomerVideo_ == nullptr) {
483                 streamCustomerVideo_ = std::make_shared<StreamCustomer>();
484             }
485             streamInfoVideo.streamId_ = streamId_video;
486             streamInfoVideo.width_ = VIDEO_WIDTH; // 1280:picture width
487             streamInfoVideo.height_ = VIDEO_HEIGHT; // 960:picture height
488             streamInfoVideo.format_ = PIXEL_FMT_RGBA_8888;
489             streamInfoVideo.dataspace_ = 8; // 8:picture dataspace
490             streamInfoVideo.intent_ = intent;
491             streamInfoVideo.encodeType_ = ENCODE_TYPE_H264;
492             streamInfoVideo.tunneledMode_ = 5; // 5:tunnel mode
493             streamInfoVideo.bufferQueue_ = new BufferProducerSequenceable(streamCustomerVideo_->CreateProducer());
494             streamInfoVideo.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size
495             std::cout << "==========[test log]video success." << std::endl;
496             std::vector<StreamInfo>().swap(streamInfos);
497             streamInfos.push_back(streamInfoVideo);
498         } else if (intent == STILL_CAPTURE) {
499             if (streamCustomerCapture_ == nullptr) {
500                 streamCustomerCapture_ = std::make_shared<StreamCustomer>();
501             }
502             streamInfoCapture.streamId_ = streamId_capture;
503             streamInfoCapture.width_ = CAPTURE_WIDTH; // 1280:picture width
504             streamInfoCapture.height_ = CAPTURE_HEIGHT; // 960:picture height
505             streamInfoCapture.format_ = PIXEL_FMT_RGBA_8888;
506             streamInfoCapture.dataspace_ = 8; // 8:picture dataspace
507             streamInfoCapture.intent_ = intent;
508             streamInfoCapture.encodeType_ = ENCODE_TYPE_JPEG;
509             streamInfoCapture.tunneledMode_ = 5; // 5:tunnel mode
510             streamInfoCapture.bufferQueue_ = new BufferProducerSequenceable(streamCustomerCapture_->CreateProducer());
511             streamInfoCapture.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size
512             std::cout << "==========[test log]capture success." << std::endl;
513             std::vector<StreamInfo>().swap(streamInfos);
514             streamInfos.push_back(streamInfoCapture);
515         } else if (intent == ANALYZE) {
516             if (streamCustomerAnalyze_ == nullptr) {
517                 streamCustomerAnalyze_ = std::make_shared<StreamCustomer>();
518             }
519             streamInfoAnalyze.streamId_ = streamId_analyze;
520             streamInfoAnalyze.width_ = ANALYZE_WIDTH; // 640:picture width
521             streamInfoAnalyze.height_ = ANALYZE_HEIGHT; // 480:picture height
522             streamInfoAnalyze.format_ = PIXEL_FMT_RGBA_8888;
523             streamInfoAnalyze.dataspace_ = 8; // 8:picture dataspace
524             streamInfoAnalyze.intent_ = intent;
525             streamInfoAnalyze.tunneledMode_ = 5; // 5:tunnel mode
526             streamInfoAnalyze.bufferQueue_ = new BufferProducerSequenceable(streamCustomerAnalyze_->CreateProducer());
527             streamInfoAnalyze.bufferQueue_->producer_->SetQueueSize(8); // 8:set bufferQueue size
528             std::cout << "==========[test log]analyze success." << std::endl;
529             std::vector<StreamInfo>().swap(streamInfos);
530             streamInfos.push_back(streamInfoAnalyze);
531         }
532         rc = (CamRetCode)streamOperator->CreateStreams(streamInfos);
533         EXPECT_EQ(false, rc != HDI::Camera::V1_0::NO_ERROR);
534         if (rc == HDI::Camera::V1_0::NO_ERROR) {
535             std::cout << "==========[test log]CreateStreams success." << std::endl;
536         } else {
537             std::cout << "==========[test log]CreateStreams fail, rc = " << rc << std::endl;
538         }
539 
540         rc = (CamRetCode)streamOperator->CommitStreams(NORMAL, ability_);
541         EXPECT_EQ(false, rc != HDI::Camera::V1_0::NO_ERROR);
542         if (rc == HDI::Camera::V1_0::NO_ERROR) {
543             std::cout << "==========[test log]CommitStreams success." << std::endl;
544         } else {
545             std::cout << "==========[test log]CommitStreams fail, rc = " << rc << std::endl;
546         }
547     }
548 }
549 
StartCapture(int streamId,int captureId,bool shutterCallback,bool isStreaming)550 void TestDisplay::StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming)
551 {
552     // Get preview
553     captureInfo.streamIds_ = {streamId};
554     captureInfo.captureSetting_ = ability_;
555     captureInfo.enableShutterCallback_ = shutterCallback;
556     rc = (CamRetCode)streamOperator->Capture(captureId, captureInfo, isStreaming);
557     EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR);
558     if (rc == HDI::Camera::V1_0::NO_ERROR) {
559         std::cout << "==========[test log]check Capture: Capture success, " << captureId << std::endl;
560     } else {
561         std::cout << "==========[test log]check Capture: Capture fail, rc = " << rc << captureId << std::endl;
562     }
563     if (captureId == captureId_preview) {
564         streamCustomerPreview_->ReceiveFrameOn(nullptr);
565     } else if (captureId == captureId_capture) {
566         streamCustomerCapture_->ReceiveFrameOn([this](void* addr, const uint32_t size) {
567             StoreImage(addr, size);
568         });
569     } else if (captureId == captureId_video) {
570         OpenVideoFile();
571         streamCustomerVideo_->ReceiveFrameOn([this](void* addr, const uint32_t size) {
572             StoreVideo(addr, size);
573         });
574     } else if (captureId == captureId_analyze) {
575         streamCustomerAnalyze_->ReceiveFrameOn([this](void* addr, const uint32_t size) {
576             PrintFaceDetectInfo(addr, size);
577         });
578     }
579     sleep(2); // 2:sleep two second
580 }
581 
StopStream(std::vector<int> & captureIds,std::vector<int> & streamIds)582 void TestDisplay::StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds)
583 {
584     constexpr uint32_t SLEEP_SECOND_TWO = 2;
585     sleep(SLEEP_SECOND_TWO);
586     if (sizeof(captureIds) > 0) {
587         for (auto &captureId : captureIds) {
588             if (captureId == captureId_preview) {
589                 streamCustomerPreview_->ReceiveFrameOff();
590             } else if (captureId == captureId_capture) {
591                 streamCustomerCapture_->ReceiveFrameOff();
592             } else if (captureId == captureId_video) {
593                 streamCustomerVideo_->ReceiveFrameOff();
594                 sleep(1);
595                 CloseFd();
596             } else if (captureId == captureId_analyze) {
597                 streamCustomerAnalyze_->ReceiveFrameOff();
598             }
599         }
600         for (auto &captureId : captureIds) {
601             std::cout << "==========[test log]check Capture: CancelCapture success," << captureId << std::endl;
602             rc = (CamRetCode)streamOperator->CancelCapture(captureId);
603             sleep(SLEEP_SECOND_TWO);
604             EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR);
605             if (rc == HDI::Camera::V1_0::NO_ERROR) {
606                 std::cout << "==========[test log]check Capture: CancelCapture success," << captureId << std::endl;
607             } else {
608                 std::cout << "==========[test log]check Capture: CancelCapture fail, rc = " << rc;
609                 std::cout << "captureId = " << captureId << std::endl;
610             }
611         }
612     }
613     sleep(1);
614     if (sizeof(streamIds) > 0) {
615         // release stream
616         rc = (CamRetCode)streamOperator->ReleaseStreams(streamIds);
617         EXPECT_EQ(true, rc == HDI::Camera::V1_0::NO_ERROR);
618         if (rc == HDI::Camera::V1_0::NO_ERROR) {
619             std::cout << "==========[test log]check Capture: ReleaseStreams success." << std::endl;
620         } else {
621             std::cout << "==========[test log]check Capture: ReleaseStreams fail, rc = " << rc << std::endl;
622             std::cout << "streamIds = " << streamIds.front() << std::endl;
623         }
624     }
625 }
626 
PrintStabiliInfo(const std::shared_ptr<CameraMetadata> & result)627 void DemoCameraDeviceCallback::PrintStabiliInfo(const std::shared_ptr<CameraMetadata>& result)
628 {
629     if (result == nullptr) {
630         CAMERA_LOGE("TestDisplay: result is null");
631         return;
632     }
633     common_metadata_header_t* data = result->get();
634     if (data == nullptr) {
635         CAMERA_LOGE("TestDisplay: data is null");
636         return;
637     }
638     uint8_t videoStabiliMode;
639     camera_metadata_item_t entry;
640     int ret = FindCameraMetadataItem(data, OHOS_CONTROL_VIDEO_STABILIZATION_MODE, &entry);
641     if (ret != 0) {
642         CAMERA_LOGE("demo test: get OHOS_CONTROL_EXPOSURE_MODE error\n");
643         return;
644     }
645     videoStabiliMode = *(entry.data.u8);
646     CAMERA_LOGI("videoStabiliMode: %{public}d", videoStabiliMode);
647     std::cout << "==========[test log] PrintStabiliInfo videoStabiliMode: " << (int)videoStabiliMode << std::endl;
648 }
649 
PrintFpsInfo(const std::shared_ptr<CameraMetadata> & result)650 void DemoCameraDeviceCallback::PrintFpsInfo(const std::shared_ptr<CameraMetadata>& result)
651 {
652     if (result == nullptr) {
653         CAMERA_LOGE("TestDisplay: result is null");
654         return;
655     }
656     common_metadata_header_t* data = result->get();
657     if (data == nullptr) {
658         CAMERA_LOGE("TestDisplay: data is null");
659         return;
660     }
661     std::vector<int32_t> fpsRange;
662     camera_metadata_item_t entry;
663     int ret = FindCameraMetadataItem(data, OHOS_CONTROL_FPS_RANGES, &entry);
664     if (ret != 0) {
665         CAMERA_LOGE("demo test: get OHOS_CONTROL_EXPOSURE_MODE error\n");
666         return;
667     }
668 
669     for (int i = 0; i < entry.count; i++) {
670         fpsRange.push_back(*(entry.data.i32 + i));
671     }
672     CAMERA_LOGI("Set fpsRange: [%{public}d, %{public}d]", fpsRange[0], fpsRange[1]);
673     std::cout << "==========[test log] PrintFpsInfo fpsRange: [" << fpsRange[0] << ", " <<
674         fpsRange[1] << "]" << std::endl;
675 }
676 
677 #ifndef CAMERA_BUILT_ON_OHOS_LITE
OnError(ErrorType type,int32_t errorCode)678 int32_t DemoCameraDeviceCallback::OnError(ErrorType type, int32_t errorCode)
679 {
680     CAMERA_LOGI("demo test: OnError type : %{public}d, errorMsg : %{public}d", type, errorCode);
681 }
682 
OnResult(uint64_t timestamp,const std::vector<uint8_t> & result)683 int32_t DemoCameraDeviceCallback::OnResult(uint64_t timestamp, const std::vector<uint8_t>& result)
684 {
685     CAMERA_LOGI("%{public}s, enter.", __func__);
686     return RC_OK;
687 }
688 
OnCameraStatus(const std::string & cameraId,CameraStatus status)689 int32_t DemoCameraHostCallback::OnCameraStatus(const std::string& cameraId, CameraStatus status)
690 {
691     CAMERA_LOGI("%{public}s, enter.", __func__);
692     return RC_OK;
693 }
694 
OnFlashlightStatus(const std::string & cameraId,FlashlightStatus status)695 int32_t DemoCameraHostCallback::OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status)
696 {
697     CAMERA_LOGI("%{public}s, enter. cameraId = %s, status = %d",
698         __func__, cameraId.c_str(), static_cast<int>(status));
699     return RC_OK;
700 }
701 
OnCameraEvent(const std::string & cameraId,CameraEvent event)702 int32_t DemoCameraHostCallback::OnCameraEvent(const std::string& cameraId, CameraEvent event)
703 {
704     CAMERA_LOGI("%{public}s, enter. cameraId = %s, event = %d",
705         __func__, cameraId.c_str(), static_cast<int>(event));
706     return RC_OK;
707 }
708 
OnCaptureStarted(int32_t captureId,const std::vector<int32_t> & streamIds)709 int32_t DemoStreamOperatorCallback::OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds)
710 {
711     CAMERA_LOGI("%{public}s, enter.", __func__);
712     return RC_OK;
713 }
714 
OnCaptureEnded(int32_t captureId,const std::vector<CaptureEndedInfo> & infos)715 int32_t DemoStreamOperatorCallback::OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos)
716 {
717     CAMERA_LOGI("%{public}s, enter.", __func__);
718     return RC_OK;
719 }
720 
OnCaptureError(int32_t captureId,const std::vector<CaptureErrorInfo> & infos)721 int32_t DemoStreamOperatorCallback::OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos)
722 {
723     CAMERA_LOGI("%{public}s, enter.", __func__);
724     return RC_OK;
725 }
726 
OnFrameShutter(int32_t captureId,const std::vector<int32_t> & streamIds,uint64_t timestamp)727 int32_t DemoStreamOperatorCallback::OnFrameShutter(int32_t captureId,
728     const std::vector<int32_t>& streamIds, uint64_t timestamp)
729 {
730     CAMERA_LOGI("%{public}s, enter.", __func__);
731     return RC_OK;
732 }
733 
734 #endif
735