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_1/types.h" 30 #include "metadata_utils.h" 31 #include "v1_1/icamera_host.h" 32 #include "v1_1/icamera_device.h" 33 #include "v1_1/istream_operator.h" 34 #include "v1_1/camera_host_proxy.h" 35 #include "v1_0/ioffline_stream_operator.h" 36 #include "v1_0/display_composer_type.h" 37 #include "iconsumer_surface.h" 38 39 namespace OHOS::Camera { 40 enum CameraUtConstants { 41 UT_SLEEP_TIME = 2, 42 UT_SECOND_TIMES, 43 UT_TUNNEL_MODE = 5, 44 UT_DATA_SIZE = 8, 45 }; 46 47 enum ImageDataSaveSwitch { 48 SWITCH_OFF, 49 SWITCH_ON, 50 }; 51 using namespace OHOS::HDI::Camera::V1_0; 52 using namespace OHOS::HDI::Display::Composer::V1_0; 53 class Test { 54 public: 55 void Init(); 56 void Open(); 57 void Close(); 58 void GetCameraMetadata(); 59 void DefaultPreview( 60 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 61 void DefaultCapture( 62 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 63 void DefaultInfosPreview( 64 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 65 void DefaultInfosCapture( 66 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 67 void DefaultInfosAnalyze( 68 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 69 void DefaultInfosVideo( 70 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 71 void StartStream(std::vector<StreamIntent> intents); 72 uint64_t GetCurrentLocalTimeStamp(); 73 int32_t DumpImageFile(int streamId, std::string suffix, const void* buffer, int32_t size); 74 void StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming); 75 void StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds); 76 OHOS::sptr<OHOS::Camera::ICameraHost> service = nullptr; 77 OHOS::sptr<OHOS::HDI::Camera::V1_1::ICameraHost> serviceV1_1 = nullptr; 78 OHOS::sptr<ICameraDevice> cameraDevice = nullptr; 79 OHOS::sptr<OHOS::HDI::Camera::V1_1::ICameraDevice> cameraDeviceV1_1 = nullptr; 80 OHOS::sptr<IStreamOperatorCallback> streamOperatorCallback = nullptr; 81 OHOS::sptr<ICameraHostCallback> hostCallback = nullptr; 82 OHOS::sptr<IStreamOperator> streamOperator = nullptr; 83 OHOS::sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperator_V1_1 = nullptr; 84 OHOS::sptr<ICameraDeviceCallback> deviceCallback = nullptr; 85 std::vector<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfos; 86 std::vector<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfosV1_1; 87 std::shared_ptr<OHOS::HDI::Camera::V1_1::PrelaunchConfig> prelaunchConfig = nullptr; 88 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoV1_1 = nullptr; 89 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfo = nullptr; 90 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoSnapshot = nullptr; 91 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoCapture = nullptr; 92 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoAnalyze = nullptr; 93 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoPre = nullptr; 94 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoVideo = nullptr; 95 std::shared_ptr<CaptureInfo> captureInfo = nullptr; 96 int previewFormat = PIXEL_FMT_YCRCB_420_SP; 97 int videoFormat = PIXEL_FMT_YCRCB_420_SP; 98 int snapshotFormat = PIXEL_FMT_YCRCB_420_SP; 99 int analyzeFormat = PIXEL_FMT_YCRCB_420_SP; 100 int streamIdPreview = 100; 101 int streamIdCapture = 101; 102 int captureWidth = 1280; 103 int captureHeight = 960; 104 int captureIdPreview = 2000; 105 int previewWidth = 1920; 106 int previewHeight = 1080; 107 int captureIdCapture = 2010; 108 int captureIdVideo = 2020; 109 int streamIdVideo = 102; 110 int videoHeight = 1080; 111 int videoWidth = 1920; 112 int analyzeWidth = 1920; 113 int analyzeHeight = 1080; 114 int snapshotWidth = 4160; 115 int snapshotHeight = 3120; 116 int streamIdAnalyze = 103; 117 std::vector<int> captureIds; 118 std::vector<int> streamIds; 119 int32_t imageDataSaveSwitch = SWITCH_OFF; 120 121 int32_t rc; 122 bool status; 123 std::vector<std::string> cameraIds; 124 std::vector<uint8_t> abilityVec = {}; 125 std::shared_ptr<CameraMetadata> ability = nullptr; 126 std::vector<StreamIntent> intents; 127 class StreamConsumer; 128 std::map<OHOS::Camera::StreamIntent, std::shared_ptr<StreamConsumer>> consumerMap_ = {}; 129 class TestBufferConsumerListener : public OHOS::IBufferConsumerListener { 130 public: TestBufferConsumerListener()131 TestBufferConsumerListener() {} ~TestBufferConsumerListener()132 ~TestBufferConsumerListener() {} OnBufferAvailable()133 void OnBufferAvailable() 134 { 135 hasAvailablebuffer = true; 136 } checkBufferAvailable()137 bool checkBufferAvailable() 138 { 139 if (hasAvailablebuffer) { 140 hasAvailablebuffer = false; 141 return true; 142 } 143 return false; 144 } 145 private: 146 bool hasAvailablebuffer = false; 147 }; 148 149 class StreamConsumer { 150 public: 151 void CalculateFps(int64_t timestamp, int32_t streamId); 152 OHOS::sptr<OHOS::IBufferProducer> CreateProducer(std::function<void(void*, uint32_t)> callback); 153 OHOS::sptr<BufferProducerSequenceable> CreateProducerSeq(std::function<void(void*, uint32_t)> callback); TakeSnapshoe()154 void TakeSnapshoe() 155 { 156 shotCount_++; 157 } WaitSnapshotEnd()158 void WaitSnapshotEnd() 159 { 160 std::cout << "ready to wait" << std::endl; 161 std::unique_lock<std::mutex> l(l_); 162 cv_.wait(l, [this]() {return shotCount_ == 0; }); 163 } ~StreamConsumer()164 ~StreamConsumer() 165 { 166 running_ = false; 167 if (consumerThread_ != nullptr) { 168 consumerThread_->join(); 169 delete consumerThread_; 170 } 171 } 172 public: 173 std::atomic<uint64_t> shotCount_ = 0; 174 std::mutex l_; 175 std::condition_variable cv_; 176 bool running_ = true; 177 OHOS::sptr<OHOS::IConsumerSurface> consumer_ = nullptr; 178 std::thread* consumerThread_ = nullptr; 179 std::function<void(void*, uint32_t)> callback_ = nullptr; 180 bool isFirstCalculateFps_ = false; 181 int timestampCount_ = 0; 182 int64_t intervalTimestamp_ = 0; 183 const int64_t ONESECOND_OF_MICROSECOND_UNIT = 1000000000; 184 int64_t interval_ = ONESECOND_OF_MICROSECOND_UNIT; 185 }; 186 187 class DemoCameraDeviceCallback : public ICameraDeviceCallback { 188 public: 189 DemoCameraDeviceCallback() = default; 190 virtual ~DemoCameraDeviceCallback() = default; 191 192 int32_t OnError(ErrorType type, int32_t errorMsg) override; 193 int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t> &result) override; 194 }; 195 196 using ResultCallback = std::function<void (uint64_t, const std::shared_ptr<CameraMetadata>)>; 197 static ResultCallback resultCallback_; 198 199 class TestStreamOperatorCallback : public IStreamOperatorCallback { 200 public: 201 TestStreamOperatorCallback() = default; 202 virtual ~TestStreamOperatorCallback() = default; 203 int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamId) override; 204 int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo> &infos) override; 205 int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo> &infos) override; 206 int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t> &streamIds, uint64_t timestamp) override; 207 }; 208 209 class TestCameraHostCallback : public ICameraHostCallback { 210 public: 211 TestCameraHostCallback() = default; 212 virtual ~TestCameraHostCallback() = default; 213 214 int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override; 215 int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override; 216 int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override; 217 }; 218 }; 219 } 220 #endif 221