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