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