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 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 <cstdio> 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 <cassert> 49 #include <cerrno> 50 #include <getopt.h> 51 #include <linux/fb.h> 52 #include <linux/videodev2.h> 53 #include <mutex> 54 #include <pthread.h> 55 #include <cstdlib> 56 #include <cstring> 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 constexpr int DEFAULT_STREAM_ID = 1001; 82 constexpr int INVALID_VALUE_TEST = 2147483647; 83 84 using namespace OHOS::HDI::Camera::V1_0; 85 using namespace OHOS::Camera; 86 class TestDisplay { 87 public: 88 // This should get the size setting according to the bottom layer 89 unsigned int bufSize_ = 614400; // 614400:bufSize 90 unsigned char* displayBuf_ = nullptr; 91 unsigned int camframeV4l2Exit_ = 1; 92 unsigned int numOfReadyFrames_ = 0; 93 unsigned int bufCont_ = BUFFERSCOUNT; 94 pthread_cond_t subThreadReadyCond = PTHREAD_COND_INITIALIZER; 95 pthread_mutex_t subThreadReadyMutex = PTHREAD_MUTEX_INITIALIZER; 96 pthread_t previewThreadId_; 97 std::mutex readyFrameLock_; 98 99 int fbFd_ = 0; 100 int readIndex_ = 0; 101 struct fb_var_screeninfo vinfo_; 102 struct fb_fix_screeninfo finfo_; 103 104 std::shared_ptr<StreamCustomer> streamCustomerPreview_ = nullptr; 105 std::shared_ptr<StreamCustomer> streamCustomerCapture_ = nullptr; 106 std::shared_ptr<StreamCustomer> streamCustomerVideo_ = nullptr; 107 std::shared_ptr<StreamCustomer> streamCustomerAnalyze_ = nullptr; 108 OHOS::sptr<IStreamOperator> streamOperator = nullptr; 109 std::shared_ptr<IStreamOperatorCallback> streamOperatorCallback = nullptr; 110 CaptureInfo captureInfo = {}; 111 std::vector<StreamInfo> streamInfos = {}; 112 StreamInfo streamInfo = {}; 113 StreamInfo streamInfoPre = {}; 114 StreamInfo streamInfoVideo = {}; 115 StreamInfo streamInfoCapture = {}; 116 StreamInfo streamInfoAnalyze = {}; 117 OHOS::sptr<ICameraHost> cameraHost = nullptr; 118 OHOS::sptr<ICameraDevice> cameraDevice = nullptr; 119 std::shared_ptr<CameraAbility> ability = nullptr; 120 std::vector<uint8_t> ability_ = {}; 121 std::vector<int> captureIds; 122 std::vector<std::string> cameraIds; 123 std::vector<int> streamIds; 124 std::vector<StreamIntent> intents; 125 enum { 126 STREAM_ID_PREVIEW = 1000, // 1000:preview streamID 127 STREAM_ID_CAPTURE, 128 STREAM_ID_VIDEO, 129 STREAM_ID_ANALYZE, 130 CAPTURE_ID_PREVIEW = 2000, // 2000:preview captureId 131 CAPTURE_ID_CAPTURE, 132 CAPTURE_ID_VIDEO, 133 CAPTURE_ID_ANALYZE 134 }; 135 enum { 136 PREVIEW_MODE = 0, 137 CAPTURE_MODE, 138 VIDEO_MODE, 139 ANALYZE_MODE, 140 }; 141 CamRetCode rc; 142 int initFlag = 0; 143 bool status = false; 144 145 public: 146 TestDisplay(); 147 sptr<ICameraHost> CameraHostImplGetInstance(void); 148 uint64_t GetCurrentLocalTimeStamp(); 149 int32_t SaveYUV(char* type, unsigned char* buffer, int32_t size); 150 int DoFbMunmap(unsigned char* addr); 151 unsigned char* DoFbMmap(int* pmemfd); 152 void FBLog(); 153 OHOS::Camera::RetCode FBInit(); 154 void ProcessImage(unsigned char* p, unsigned char* fbp); 155 void LcdDrawScreen(unsigned char* displayBuf, unsigned char* addr); 156 void BufferCallback(unsigned char* addr, int choice); 157 void Init(); 158 void UsbInit(); 159 void Close(); 160 void OpenCamera(); 161 void AchieveStreamOperator(); 162 void StartStream(std::vector<StreamIntent> intents); 163 void StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds); 164 void StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming); 165 float CalTime(struct timeval start, struct timeval end); 166 void StoreImage(const unsigned char *bufStart, const uint32_t size) const; 167 void StoreVideo(const unsigned char *bufStart, const uint32_t size) const; 168 void OpenVideoFile(); 169 void PrintFaceDetectInfo(const unsigned char *bufStart, const uint32_t size) const; 170 void CloseFd(); 171 int videoFd_ = -1; 172 }; 173 174 #ifndef CAMERA_BUILT_ON_OHOS_LITE 175 class DemoCameraDeviceCallback : public ICameraDeviceCallback { 176 public: 177 DemoCameraDeviceCallback() = default; 178 virtual ~DemoCameraDeviceCallback() = default; 179 int32_t OnError(ErrorType type, int32_t errorCode) override; 180 int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t>& result) override; 181 182 void PrintStabiliInfo(const std::shared_ptr<CameraMetadata>& result); 183 void PrintFpsInfo(const std::shared_ptr<CameraMetadata>& result); 184 }; 185 186 class DemoCameraHostCallback : public ICameraHostCallback { 187 public: 188 DemoCameraHostCallback() = default; 189 virtual ~DemoCameraHostCallback() = default; 190 191 public: 192 int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override; 193 194 int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override; 195 196 int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override; 197 }; 198 199 class DemoStreamOperatorCallback : public IStreamOperatorCallback { 200 public: 201 DemoStreamOperatorCallback() = default; 202 virtual ~DemoStreamOperatorCallback() = default; 203 204 public: 205 int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds) override; 206 int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos) override; 207 int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos) override; 208 int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp) override; 209 }; 210 211 #endif 212 #endif 213