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