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