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