1 /* 2 * Copyright (c) 2021-2024 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 <thread> 21 #include <map> 22 #include <cstdio> 23 #include <climits> 24 #include "v1_0/types.h" 25 #include "v1_0/icamera_host.h" 26 #include "v1_0/icamera_device.h" 27 #include "v1_0/istream_operator.h" 28 #include "v1_0/stream_operator_proxy.h" 29 #include "idevice_manager.h" 30 #include "camera_metadata_info.h" 31 #include "metadata_utils.h" 32 #include <v1_0/display_composer_type.h> 33 #include <fcntl.h> 34 #include <unistd.h> 35 #include <sys/ioctl.h> 36 #include <sys/wait.h> 37 #include "buffer_manager.h" 38 #include "stream_customer.h" 39 #include "camera_host_callback.h" 40 #include "camera_device_callback.h" 41 #include "stream_operator_callback.h" 42 #include "v1_0/istream_operator_callback.h" 43 #include "v1_0/icamera_host.h" 44 #include "v1_0/camera_host_proxy.h" 45 #include "ibuffer.h" 46 #include <algorithm> 47 #include <cassert> 48 #include <cerrno> 49 #include <getopt.h> 50 #include <linux/fb.h> 51 #include <linux/videodev2.h> 52 #include <mutex> 53 #include <pthread.h> 54 #include <cstdlib> 55 #include <cstring> 56 #include <sys/mman.h> 57 #include <sys/stat.h> 58 #include <sys/time.h> 59 #include <vector> 60 #include <iostream> 61 #include <cstring> 62 #include <cerrno> 63 #include <securec.h> 64 #include <surface_buffer.h> 65 #include <ibuffer_producer.h> 66 #include <fstream> 67 #define PATH_MAX 128 68 #define BUFFERSCOUNT 8 69 #define CAMERA_BUFFER_QUEUE_IPC 654320 70 #define RANGE_LIMIT(x) ((x) > 255 ? 255 : ((x) < 0 ? 0 : (x))) 71 #define PREVIEW_WIDTH 640 72 #define PREVIEW_HEIGHT 480 73 #define CAPTURE_WIDTH 1280 74 #define CAPTURE_HEIGHT 960 75 #define VIDEO_WIDTH 1280 76 #define VIDEO_HEIGHT 960 77 #define ANALYZE_WIDTH 640 78 #define ANALYZE_HEIGHT 480 79 80 constexpr int DEFAULT_STREAM_ID = 1001; 81 constexpr int INVALID_VALUE_TEST = 2147483647; 82 83 using namespace OHOS::HDI::Camera::V1_0; 84 using namespace OHOS::HDI::Display::Composer::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 std::shared_ptr<CameraAbility> GetCameraAbility(); 160 void OpenUsbCamera(); 161 CamRetCode SelectOpenCamera(std::string cameraId); 162 void Close(); 163 void OpenCamera(); 164 void AchieveStreamOperator(); 165 void StartStream(std::vector<StreamIntent> intents); 166 void StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds); 167 void StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming); 168 float CalTime(struct timeval start, struct timeval end); 169 void StoreImage(const unsigned char *bufStart, const uint32_t size) const; 170 void StoreVideo(const unsigned char *bufStart, const uint32_t size) const; 171 void OpenVideoFile(); 172 void PrintFaceDetectInfo(const unsigned char *bufStart, const uint32_t size) const; 173 void StartStreamUpdate(int wide, int hight); 174 void CloseFd(); 175 int videoFd_ = -1; 176 }; 177 178 #ifndef CAMERA_BUILT_ON_OHOS_LITE 179 class DemoCameraDeviceCallback : public ICameraDeviceCallback { 180 public: 181 DemoCameraDeviceCallback() = default; 182 virtual ~DemoCameraDeviceCallback() = default; 183 int32_t OnError(ErrorType type, int32_t errorCode) override; 184 int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t>& result) override; 185 186 void PrintStabiliInfo(const std::vector<uint8_t>& result); 187 void PrintFpsInfo(const std::vector<uint8_t>& result); 188 void DealCameraMetadata(const std::vector<uint8_t> &settings); 189 }; 190 191 class DemoCameraHostCallback : public ICameraHostCallback { 192 public: 193 DemoCameraHostCallback() = default; 194 virtual ~DemoCameraHostCallback() = default; 195 196 public: 197 int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override; 198 199 int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override; 200 201 int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override; 202 }; 203 204 class DemoStreamOperatorCallback : public IStreamOperatorCallback { 205 public: 206 DemoStreamOperatorCallback() = default; 207 virtual ~DemoStreamOperatorCallback() = default; 208 209 public: 210 int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds) override; 211 int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos) override; 212 int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos) override; 213 int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp) override; 214 }; 215 216 #endif 217 #endif 218