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_0/types.h" 30 #include "metadata_utils.h" 31 #include "v1_0/icamera_host.h" 32 #include "v1_0/icamera_device.h" 33 #include "v1_0/istream_operator.h" 34 #include "v1_0/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, 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 class Test { 53 public: 54 void Init(); 55 void Open(); 56 void Close(); 57 void GetCameraMetadata(); 58 void StartStream(std::vector<StreamIntent> intents); 59 void DefaultPreview(std::shared_ptr<StreamInfo> &infos); 60 void DefaultCapture(std::shared_ptr<StreamInfo> &infos); 61 void DefaultInfosPreview(std::shared_ptr<StreamInfo> &infos); 62 void DefaultInfosCapture(std::shared_ptr<StreamInfo> &infos); 63 void DefaultInfosVideo(std::shared_ptr<StreamInfo> &infos); 64 void DefaultInfosAnalyze(std::shared_ptr<StreamInfo> &infos); 65 uint64_t GetCurrentLocalTimeStamp(); 66 int32_t DumpImageFile(int streamId, std::string suffix, const void* buffer, int32_t size); 67 void StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming); 68 void StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds); 69 OHOS::sptr<OHOS::Camera::ICameraHost> service = nullptr; 70 OHOS::sptr<ICameraDevice> cameraDevice = nullptr; 71 OHOS::sptr<IStreamOperatorCallback> streamOperatorCallback = nullptr; 72 OHOS::sptr<ICameraHostCallback> hostCallback = nullptr; 73 OHOS::sptr<IStreamOperator> streamOperator = nullptr; 74 OHOS::sptr<ICameraDeviceCallback> deviceCallback = nullptr; 75 std::vector<StreamInfo> streamInfos; 76 std::shared_ptr<StreamInfo> streamInfo = nullptr; 77 std::shared_ptr<StreamInfo> streamInfoSnapshot = nullptr; 78 std::shared_ptr<StreamInfo> streamInfoCapture = nullptr; 79 std::shared_ptr<StreamInfo> streamInfoAnalyze = nullptr; 80 std::shared_ptr<StreamInfo> streamInfoPre = nullptr; 81 std::shared_ptr<StreamInfo> streamInfoVideo = nullptr; 82 std::shared_ptr<CaptureInfo> captureInfo = nullptr; 83 int previewFormat = PIXEL_FMT_YCRCB_420_SP; 84 int videoFormat = PIXEL_FMT_YCRCB_420_SP; 85 int snapshotFormat = PIXEL_FMT_YCRCB_420_SP; 86 int analyzeFormat = PIXEL_FMT_YCRCB_420_SP; 87 int videoEncodeType = ENCODE_TYPE_H265; 88 int streamIdPreview = 100; 89 int streamIdCapture = 101; 90 int captureWidth = 1280; 91 int captureHeight = 960; 92 int captureIdPreview = 2000; 93 int previewWidth = 1920; 94 int previewHeight = 1080; 95 int captureIdCapture = 2010; 96 int captureIdVideo = 2020; 97 int streamIdVideo = 102; 98 int videoHeight = 1080; 99 int videoWidth = 1920; 100 int analyzeWidth = 1920; 101 int analyzeHeight = 1080; 102 int snapshotWidth = 4160; 103 int snapshotHeight = 3120; 104 int streamIdAnalyze = 103; 105 int usbCamera_previewWidth = 640; 106 int usbCamera_previewHeight = 480; 107 int usbCamera_videoWidth = 1280; 108 int usbCamera_videoHeight = 960; 109 int usbCamera_captureWidth = 1280; 110 int usbCamera_captureHeight = 960; 111 int usbCamera_analyzeWidth = 640; 112 int usbCamera_analyzeHeight = 480; 113 int usbCamera_previewFormat = PIXEL_FMT_RGBA_8888; 114 int usbCamera_videoFormat = PIXEL_FMT_YCRCB_420_SP; 115 int usbCamera_snapshotFormat = PIXEL_FMT_RGBA_8888; 116 int usbCamera_analyzeFormat = PIXEL_FMT_RGBA_8888; 117 int usbCamera_videoEncodeType = ENCODE_TYPE_H264; 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 131 class TestBufferConsumerListener : public OHOS::IBufferConsumerListener { 132 public: TestBufferConsumerListener()133 TestBufferConsumerListener() {} ~TestBufferConsumerListener()134 ~TestBufferConsumerListener() {} OnBufferAvailable()135 void OnBufferAvailable() 136 { 137 hasAvailablebuffer = true; 138 } checkBufferAvailable()139 bool checkBufferAvailable() 140 { 141 if (hasAvailablebuffer) { 142 hasAvailablebuffer = false; 143 return true; 144 } 145 return false; 146 } 147 private: 148 bool hasAvailablebuffer = false; 149 }; 150 151 class StreamConsumer { 152 public: 153 void CalculateFps(int64_t timestamp, int32_t streamId); 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 int64_t intervalTimestamp_ = 0; 185 const int64_t ONESECOND_OF_MICROSECOND_UNIT = 1000000000; 186 int64_t interval_ = ONESECOND_OF_MICROSECOND_UNIT; 187 }; 188 189 class DemoCameraDeviceCallback : public ICameraDeviceCallback { 190 public: 191 DemoCameraDeviceCallback() = default; 192 virtual ~DemoCameraDeviceCallback() = default; 193 194 int32_t OnError(ErrorType type, int32_t errorMsg) override; 195 int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t> &result) override; 196 }; 197 198 using ResultCallback = std::function<void (uint64_t, const std::shared_ptr<CameraMetadata>)>; 199 static ResultCallback resultCallback_; 200 201 class TestStreamOperatorCallback : public IStreamOperatorCallback { 202 public: 203 TestStreamOperatorCallback() = default; 204 virtual ~TestStreamOperatorCallback() = default; 205 int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamId) override; 206 int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo> &infos) override; 207 int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo> &infos) override; 208 int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t> &streamIds, uint64_t timestamp) override; 209 }; 210 211 class TestCameraHostCallback : public ICameraHostCallback { 212 public: 213 TestCameraHostCallback() = default; 214 virtual ~TestCameraHostCallback() = default; 215 216 int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override; 217 int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override; 218 int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override; 219 }; 220 }; 221 } 222 #endif 223