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