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