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