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