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