• 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 
16 #ifndef TEST_DISPLAY_H
17 #define TEST_DISPLAY_H
18 #include <gtest/gtest.h>
19 #include "camera.h"
20 #include "stream_operator.h"
21 #include "utils.h"
22 #include <thread>
23 #include <map>
24 #include <stdio.h>
25 #include <climits>
26 #include "v1_0/types.h"
27 #include "camera_device_impl.h"
28 #include "camera_host_impl.h"
29 #include "v1_0/stream_operator_proxy.h"
30 #include "idevice_manager.h"
31 #include "camera_metadata_info.h"
32 #include "metadata_utils.h"
33 #include <display_type.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <sys/ioctl.h>
37 #include <sys/wait.h>
38 #include "buffer_manager.h"
39 #include "stream_customer.h"
40 #include "camera_host_callback.h"
41 #include "camera_device_callback.h"
42 #include "stream_operator_callback.h"
43 #include "v1_0/istream_operator_callback.h"
44 #include "v1_0/icamera_host.h"
45 #include "v1_0/camera_host_proxy.h"
46 #include "ibuffer.h"
47 #include <algorithm>
48 #include <assert.h>
49 #include <errno.h>
50 #include <getopt.h>
51 #include <linux/fb.h>
52 #include <linux/videodev2.h>
53 #include <mutex>
54 #include <pthread.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <sys/mman.h>
58 #include <sys/stat.h>
59 #include <sys/time.h>
60 #include <vector>
61 #include <iostream>
62 #include <cstring>
63 #include <cerrno>
64 #include <securec.h>
65 #include <surface_buffer.h>
66 #include <ibuffer_producer.h>
67 #include <fstream>
68 #define PATH_MAX 128
69 #define BUFFERSCOUNT 8
70 #define CAMERA_BUFFER_QUEUE_IPC 654320
71 #define RANGE_LIMIT(x) ((x) > 255 ? 255 : ((x) < 0 ? 0 : (x)))
72 #define PREVIEW_WIDTH 640
73 #define PREVIEW_HEIGHT 480
74 #define CAPTURE_WIDTH 1280
75 #define CAPTURE_HEIGHT 960
76 #define VIDEO_WIDTH 1280
77 #define VIDEO_HEIGHT 960
78 #define ANALYZE_WIDTH 640
79 #define ANALYZE_HEIGHT 480
80 
81 using namespace OHOS::HDI::Camera::V1_0;
82 using namespace OHOS::Camera;
83 class TestDisplay {
84 public:
85     // This should get the size setting according to the bottom layer
86     unsigned int bufSize_ = 614400; // 614400:bufSize
87     unsigned char* displayBuf_;
88     unsigned int camframeV4l2Exit_ = 1;
89     unsigned int numOfReadyFrames_ = 0;
90     unsigned int bufCont_ = BUFFERSCOUNT;
91     pthread_cond_t sub_thread_ready_cond = PTHREAD_COND_INITIALIZER;
92     pthread_mutex_t sub_thread_ready_mutex = PTHREAD_MUTEX_INITIALIZER;
93     pthread_t previewThreadId_;
94     std::mutex readyFrameLock_;
95 
96     int fbFd_, readIndex_;
97     struct fb_var_screeninfo vinfo_;
98     struct fb_fix_screeninfo finfo_;
99 
100     std::shared_ptr<StreamCustomer> streamCustomerPreview_ = nullptr;
101     std::shared_ptr<StreamCustomer> streamCustomerCapture_ = nullptr;
102     std::shared_ptr<StreamCustomer> streamCustomerVideo_ = nullptr;
103     std::shared_ptr<StreamCustomer> streamCustomerAnalyze_ = nullptr;
104     OHOS::sptr<IStreamOperator> streamOperator = nullptr;
105     std::shared_ptr<IStreamOperatorCallback> streamOperatorCallback = nullptr;
106     CaptureInfo captureInfo = {};
107     std::vector<StreamInfo> streamInfos = {};
108     StreamInfo streamInfo = {};
109     StreamInfo streamInfoPre = {};
110     StreamInfo streamInfoVideo = {};
111     StreamInfo streamInfoCapture = {};
112     StreamInfo streamInfoAnalyze = {};
113     OHOS::sptr<ICameraHost> cameraHost = nullptr;
114     OHOS::sptr<ICameraDevice> cameraDevice = nullptr;
115     std::shared_ptr<CameraAbility> ability = nullptr;
116     std::vector<uint8_t> ability_ = {};
117     std::vector<int> captureIds;
118     std::vector<std::string> cameraIds;
119     std::vector<int> streamIds;
120     std::vector<StreamIntent> intents;
121     enum {
122         streamId_preview = 1000, // 1000:preview streamID
123         streamId_capture,
124         streamId_video,
125         streamId_analyze,
126         captureId_preview = 2000, // 2000:preview captureId
127         captureId_capture,
128         captureId_video,
129         captureId_analyze
130     };
131     enum {
132         preview_mode = 0,
133         capture_mode,
134         video_mode,
135         analyze_mode,
136     };
137     CamRetCode rc;
138     int init_flag = 0;
139     bool status;
140 
141 public:
142     TestDisplay();
143     sptr<ICameraHost> CameraHostImplGetInstance(void);
144     uint64_t GetCurrentLocalTimeStamp();
145     int32_t SaveYUV(char* type, unsigned char* buffer, int32_t size);
146     int DoFbMunmap(unsigned char* addr);
147     unsigned char* DoFbMmap(int* pmemfd);
148     void FBLog();
149     OHOS::Camera::RetCode FBInit();
150     void ProcessImage(const unsigned char* p, unsigned char* fbp);
151     void LcdDrawScreen(unsigned char* displayBuf_, unsigned char* addr);
152     void BufferCallback(void* addr, int choice);
153     void Init();
154     void UsbInit();
155     void Close();
156     void OpenCamera();
157     void AchieveStreamOperator();
158     void StartStream(std::vector<StreamIntent> intents);
159     void StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds);
160     void StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming);
161     float calTime(struct timeval start, struct timeval end);
162     void StoreImage(const void *bufStart, const uint32_t size) const;
163     void StoreVideo(const void *bufStart, const uint32_t size) const;
164     void OpenVideoFile();
165     void PrintFaceDetectInfo(const void *bufStart, const uint32_t size) const;
166     void CloseFd();
167     int videoFd_ = -1;
168 };
169 
170 #ifndef CAMERA_BUILT_ON_OHOS_LITE
171 class DemoCameraDeviceCallback : public ICameraDeviceCallback {
172 public:
173     DemoCameraDeviceCallback() = default;
174     virtual ~DemoCameraDeviceCallback() = default;
175     int32_t OnError(ErrorType type, int32_t errorCode) override;
176     int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t>& result) override;
177 
178     void PrintStabiliInfo(const std::shared_ptr<CameraMetadata>& result);
179     void PrintFpsInfo(const std::shared_ptr<CameraMetadata>& result);
180 };
181 
182 class DemoCameraHostCallback : public ICameraHostCallback {
183 public:
184     DemoCameraHostCallback() = default;
185     virtual ~DemoCameraHostCallback() = default;
186 
187 public:
188     int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override;
189 
190     int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override;
191 
192     int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override;
193 };
194 
195 class DemoStreamOperatorCallback : public IStreamOperatorCallback {
196 public:
197     DemoStreamOperatorCallback() = default;
198     virtual ~DemoStreamOperatorCallback() = default;
199 
200 public:
201     int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds) override;
202     int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos) override;
203     int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos) override;
204     int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp) override;
205 };
206 
207 #endif
208 #endif
209