1 /* 2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file expected 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 UT_COMMON_H 17 #define UT_COMMON_H 18 19 #include <stdlib.h> 20 #include <thread> 21 #include <iostream> 22 #include <unistd.h> 23 #include <gtest/gtest.h> 24 #include <sys/time.h> 25 #include <map> 26 #include <vector> 27 #include <fcntl.h> 28 #include "camera.h" 29 #include "v1_2/types.h" 30 #include "metadata_utils.h" 31 #include "v1_2/icamera_host.h" 32 #include "v1_2/icamera_device.h" 33 #include "v1_2/istream_operator.h" 34 #include "v1_2/camera_host_proxy.h" 35 #include "v1_2/image_process_service_proxy.h" 36 #include "v1_0/ioffline_stream_operator.h" 37 #include "display_format.h" 38 #include "iconsumer_surface.h" 39 40 namespace OHOS::Camera { 41 enum CameraUtConstants { 42 UT_SLEEP_TIME = 2, 43 UT_SECOND_TIMES = 3, 44 UT_SECOND_TIMES_MAX = 100, 45 UT_TUNNEL_MODE = 5, 46 UT_DATA_SIZE = 8, 47 UT_PREVIEW_SIZE = 3112960, 48 UT_MICROSECOND_TIMES = 500000, 49 CAMERA_ID_NUM = 2, 50 }; 51 52 enum Numbers { 53 ZERO, 54 ONE, 55 TWO, 56 THREE, 57 FOUR, 58 FIVE, 59 SIX, 60 SEVEN, 61 EIGHT, 62 NINE, 63 TEN, 64 SIXTEEN = 16, 65 }; 66 67 enum ImageDataSaveSwitch { 68 SWITCH_OFF, 69 SWITCH_ON, 70 }; 71 72 enum CameraIds { 73 DEVICE_0, // rear camera 74 DEVICE_1, // front camera 75 DEVICE_2, 76 DEVICE_3, 77 DEVICE_4, 78 DEVICE_5, 79 DEVICE_6, 80 }; 81 82 using namespace OHOS::HDI::Camera::V1_0; 83 class Test { 84 public: 85 void Init(); 86 int32_t DefferredImageTestInit(); 87 void Open(int cameraId); 88 void OpenCameraV1_2(int cameraId); 89 void Close(); 90 void GetCameraMetadata(int cameraId); 91 void DefaultPreview( 92 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 93 void DefaultCapture( 94 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 95 void DefaultSketch( 96 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 97 void DefaultInfosPreview( 98 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 99 void DefaultInfosPreviewV1_2( 100 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 101 void DefaultInfosCapture( 102 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 103 void DefaultInfosAnalyze( 104 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 105 void DefaultInfosVideo( 106 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 107 void DefaultInfosSketch( 108 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 109 void StartStream(std::vector<StreamIntent> intents); 110 uint64_t GetCurrentLocalTimeStamp(); 111 int32_t DumpImageFile(int streamId, std::string suffix, const void* buffer, int32_t size); 112 void StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming); 113 void StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds); 114 bool IsTagValueExistsU8(std::shared_ptr<CameraMetadata> ability, uint32_t tag, uint8_t value); 115 void TakePhotoWithTags(std::shared_ptr<OHOS::Camera::CameraSetting> meta); 116 OHOS::sptr<OHOS::Camera::ICameraHost> service = nullptr; 117 OHOS::sptr<OHOS::HDI::Camera::V1_1::ICameraHost> serviceV1_1 = nullptr; 118 OHOS::sptr<OHOS::HDI::Camera::V1_2::ICameraHost> serviceV1_2 = nullptr; 119 OHOS::sptr<ICameraDevice> cameraDevice = nullptr; 120 OHOS::sptr<OHOS::HDI::Camera::V1_1::ICameraDevice> cameraDeviceV1_1 = nullptr; 121 OHOS::sptr<OHOS::HDI::Camera::V1_2::ICameraDevice> cameraDeviceV1_2 = nullptr; 122 OHOS::sptr<IStreamOperatorCallback> streamOperatorCallback = nullptr; 123 OHOS::sptr<OHOS::HDI::Camera::V1_2::IStreamOperatorCallback> streamOperatorCallbackV1_2 = nullptr; 124 OHOS::sptr<ICameraHostCallback> hostCallback = nullptr; 125 OHOS::sptr<OHOS::HDI::Camera::V1_2::ICameraHostCallback> hostCallbackV1_2 = nullptr; 126 OHOS::sptr<IStreamOperator> streamOperator = nullptr; 127 OHOS::sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperator_V1_1 = nullptr; 128 OHOS::sptr<OHOS::HDI::Camera::V1_2::IStreamOperator> streamOperator_V1_2 = nullptr; 129 class DemoCameraDeviceCallback; 130 OHOS::sptr<DemoCameraDeviceCallback> deviceCallback = nullptr; 131 std::vector<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfos; 132 std::vector<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfosV1_1; 133 std::shared_ptr<OHOS::HDI::Camera::V1_1::PrelaunchConfig> prelaunchConfig = nullptr; 134 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoV1_1 = nullptr; 135 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfo = nullptr; 136 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoSnapshot = nullptr; 137 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoCapture = nullptr; 138 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoAnalyze = nullptr; 139 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoPre = nullptr; 140 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoVideo = nullptr; 141 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoSketch = nullptr; 142 std::shared_ptr<CaptureInfo> captureInfo = nullptr; 143 sptr<OHOS::HDI::Camera::V1_2::IImageProcessSession> imageProcessSession_ = nullptr; 144 sptr<OHOS::HDI::Camera::V1_2::IImageProcessService> imageProcessService_ = nullptr; 145 class TestImageProcessCallback; 146 sptr<TestImageProcessCallback> imageProcessCallback_ = nullptr; 147 std::vector<std::string> pendingImageIds_; 148 int previewFormat = PIXEL_FMT_YCRCB_420_SP; 149 int videoFormat = PIXEL_FMT_YCRCB_420_SP; 150 int snapshotFormat = PIXEL_FMT_YCRCB_420_SP; 151 int analyzeFormat = PIXEL_FMT_YCRCB_420_SP; 152 int streamIdPreview = 100; 153 int streamIdCapture = 101; 154 int streamIdSketch = 105; 155 int captureWidth = 1280; 156 int captureHeight = 960; 157 int sketchWidth = 640; 158 int sketchHeight = 480; 159 int captureIdPreview = 2000; 160 int captureIdSketch = 2050; 161 int previewWidth = 1920; 162 int previewHeight = 1080; 163 int captureIdCapture = 2010; 164 int captureIdVideo = 2020; 165 int streamIdVideo = 102; 166 int videoHeight = 1080; 167 int videoWidth = 1920; 168 int analyzeWidth = 1920; 169 int analyzeHeight = 1080; 170 int snapshotWidth = 4160; 171 int snapshotHeight = 3120; 172 int streamIdAnalyze = 103; 173 std::vector<int> captureIds; 174 std::vector<int> streamIds; 175 int32_t imageDataSaveSwitch = SWITCH_OFF; 176 uint32_t itemCapacity = 100; 177 uint32_t dataCapacity = 2000; 178 uint32_t dataCount = 1; 179 180 int32_t rc; 181 bool status; 182 float statusV1_2; 183 static FlashlightStatus statusCallback; 184 std::vector<std::string> cameraIds; 185 std::vector<uint8_t> abilityVec = {}; 186 std::shared_ptr<CameraMetadata> ability = nullptr; 187 std::vector<StreamIntent> intents; 188 std::vector<int32_t> previewColorSpaces_ = {2360580, 2360324}; 189 class StreamConsumer; 190 std::map<OHOS::Camera::StreamIntent, std::shared_ptr<StreamConsumer>> consumerMap_ = {}; 191 class TestBufferConsumerListener : public OHOS::IBufferConsumerListener { 192 public: TestBufferConsumerListener()193 TestBufferConsumerListener() {} ~TestBufferConsumerListener()194 ~TestBufferConsumerListener() {} OnBufferAvailable()195 void OnBufferAvailable() 196 { 197 hasAvailablebuffer = true; 198 } checkBufferAvailable()199 bool checkBufferAvailable() 200 { 201 if (hasAvailablebuffer) { 202 hasAvailablebuffer = false; 203 return true; 204 } 205 return false; 206 } 207 private: 208 bool hasAvailablebuffer = false; 209 }; 210 211 class StreamConsumer { 212 public: 213 void CalculateFps(int64_t timestamp, int32_t streamId); 214 void GetTimeStamp(int64_t *g_timestamp, uint32_t lenght, int64_t timestamp, int32_t gotSize); 215 OHOS::sptr<OHOS::IBufferProducer> CreateProducer(std::function<void(void*, uint32_t)> callback); 216 OHOS::sptr<BufferProducerSequenceable> CreateProducerSeq(std::function<void(void*, uint32_t)> callback); TakeSnapshoe()217 void TakeSnapshoe() 218 { 219 shotCount_++; 220 } WaitSnapshotEnd()221 void WaitSnapshotEnd() 222 { 223 std::cout << "ready to wait" << std::endl; 224 std::unique_lock<std::mutex> l(l_); 225 cv_.wait(l, [this]() {return shotCount_ == 0; }); 226 } ~StreamConsumer()227 ~StreamConsumer() 228 { 229 running_ = false; 230 if (consumerThread_ != nullptr) { 231 consumerThread_->join(); 232 delete consumerThread_; 233 } 234 } 235 public: 236 std::atomic<uint64_t> shotCount_ = 0; 237 std::mutex l_; 238 std::condition_variable cv_; 239 bool running_ = true; 240 OHOS::sptr<OHOS::IConsumerSurface> consumer_ = nullptr; 241 std::thread* consumerThread_ = nullptr; 242 std::function<void(void*, uint32_t)> callback_ = nullptr; 243 bool isFirstCalculateFps_ = false; 244 int timestampCount_ = 0; 245 static int64_t g_timestamp[2]; 246 int64_t intervalTimestamp_ = 0; 247 const int64_t ONESECOND_OF_MICROSECOND_UNIT = 1000000000; 248 int64_t interval_ = ONESECOND_OF_MICROSECOND_UNIT; 249 }; 250 251 class DemoCameraDeviceCallback : public ICameraDeviceCallback { 252 public: 253 std::shared_ptr<CameraMetadata> resultMeta = nullptr; 254 DemoCameraDeviceCallback() = default; 255 virtual ~DemoCameraDeviceCallback() = default; 256 257 int32_t OnError(ErrorType type, int32_t errorMsg) override; 258 int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t> &result) override; 259 }; 260 261 using ResultCallback = std::function<void (uint64_t, const std::shared_ptr<CameraMetadata>)>; 262 static ResultCallback resultCallback_; 263 264 class TestStreamOperatorCallback : public IStreamOperatorCallback { 265 public: 266 TestStreamOperatorCallback() = default; 267 virtual ~TestStreamOperatorCallback() = default; 268 int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamId) override; 269 int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo> &infos) override; 270 int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo> &infos) override; 271 int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t> &streamIds, uint64_t timestamp) override; 272 }; 273 274 class TestStreamOperatorCallbackV1_2 : public OHOS::HDI::Camera::V1_2::IStreamOperatorCallback { 275 public: 276 TestStreamOperatorCallbackV1_2() = default; 277 virtual ~TestStreamOperatorCallbackV1_2() = default; 278 int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamId) override; 279 int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo> &infos) override; 280 int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo> &infos) override; 281 int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t> &streamIds, uint64_t timestamp) override; 282 int32_t OnCaptureStarted_V1_2(int32_t captureId, 283 const std::vector<HDI::Camera::V1_2::CaptureStartedInfo> &infos) override; 284 }; 285 286 class TestCameraHostCallback : public ICameraHostCallback { 287 public: 288 TestCameraHostCallback() = default; 289 virtual ~TestCameraHostCallback() = default; 290 291 int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override; 292 int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override; 293 int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override; 294 }; 295 296 class TestCameraHostCallbackV1_2 : public OHOS::HDI::Camera::V1_2::ICameraHostCallback { 297 public: 298 TestCameraHostCallbackV1_2() = default; 299 virtual ~TestCameraHostCallbackV1_2() = default; 300 301 int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override; 302 int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override; 303 int32_t OnFlashlightStatus_V1_2(FlashlightStatus status) override; 304 int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override; 305 }; 306 307 class TestImageProcessCallback : public OHOS::HDI::Camera::V1_2::IImageProcessCallback { 308 public: 309 int32_t coutProcessDone_ = 0; 310 int32_t coutStatusChanged_ = 0; 311 int32_t countError_ = 0; 312 std::string curImageId_; 313 int32_t curErrorCode_ = 0; 314 bool isDone = false; 315 OHOS::HDI::Camera::V1_2::ImageBufferInfo curImageBufferInfo_; 316 OHOS::HDI::Camera::V1_2::SessionStatus curStatus_; 317 TestImageProcessCallback() = default; 318 virtual ~TestImageProcessCallback() = default; 319 int32_t OnProcessDone(const std::string& imageId, 320 const OHOS::HDI::Camera::V1_2::ImageBufferInfo& buffer) override; 321 int32_t OnStatusChanged(OHOS::HDI::Camera::V1_2::SessionStatus status) override; 322 int32_t OnError(const std::string& imageId, OHOS::HDI::Camera::V1_2::ErrorCode errorCode) override; 323 }; 324 }; 325 } 326 #endif