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_1/types.h"
30 #include "metadata_utils.h"
31 #include "v1_1/icamera_host.h"
32 #include "v1_1/icamera_device.h"
33 #include "v1_1/istream_operator.h"
34 #include "v1_1/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,
43     UT_TUNNEL_MODE = 5,
44     UT_DATA_SIZE = 8,
45     UT_PREVIEW_SIZE = 3112960,
46 };
47 
48 enum ImageDataSaveSwitch {
49     SWITCH_OFF,
50     SWITCH_ON,
51 };
52 enum CameraIds {
53     DEVICE_0, // rear camera
54     DEVICE_1, // front camera
55     DEVICE_2,
56     DEVICE_3,
57     DEVICE_4,
58     DEVICE_5,
59     DEVICE_6,
60 };
61 using namespace OHOS::HDI::Camera::V1_0;
62 class Test {
63 public:
64     void Init();
65     void Open(int cameraId);
66     void Close();
67     void GetCameraMetadata(int cameraId);
68     void DefaultPreview(
69         std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos);
70     void DefaultCapture(
71         std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos);
72     void DefaultInfosPreview(
73         std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos);
74     void DefaultInfosCapture(
75         std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos);
76     void DefaultInfosAnalyze(
77         std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos);
78     void DefaultInfosVideo(
79         std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos);
80     void StartStream(std::vector<StreamIntent> intents);
81     void StartStream(std::vector<StreamIntent> intents,
82         OHOS::HDI::Camera::V1_1::OperationMode_V1_1 mode);
83     uint64_t GetCurrentLocalTimeStamp();
84     int32_t DumpImageFile(int streamId, std::string suffix, const void* buffer, int32_t size);
85     void StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming);
86     void StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds);
87     OHOS::sptr<OHOS::Camera::ICameraHost> service = nullptr;
88     OHOS::sptr<OHOS::HDI::Camera::V1_1::ICameraHost> serviceV1_1 = nullptr;
89     OHOS::sptr<ICameraDevice> cameraDevice = nullptr;
90     OHOS::sptr<OHOS::HDI::Camera::V1_1::ICameraDevice> cameraDeviceV1_1 = nullptr;
91     OHOS::sptr<IStreamOperatorCallback> streamOperatorCallback = nullptr;
92     OHOS::sptr<ICameraHostCallback> hostCallback = nullptr;
93     OHOS::sptr<IStreamOperator> streamOperator = nullptr;
94     OHOS::sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperator_V1_1 = nullptr;
95     OHOS::sptr<ICameraDeviceCallback> deviceCallback = nullptr;
96     std::vector<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfos;
97     std::vector<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfosV1_1;
98     std::shared_ptr<OHOS::HDI::Camera::V1_1::PrelaunchConfig> prelaunchConfig = nullptr;
99     std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoV1_1 = nullptr;
100     std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfo = nullptr;
101     std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoSnapshot = nullptr;
102     std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoCapture = nullptr;
103     std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoAnalyze = nullptr;
104     std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoPre = nullptr;
105     std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoVideo = nullptr;
106     std::shared_ptr<CaptureInfo> captureInfo = nullptr;
107     int previewFormat = PIXEL_FMT_YCRCB_420_SP;
108     int videoFormat = PIXEL_FMT_YCRCB_420_SP;
109     int snapshotFormat = PIXEL_FMT_YCRCB_420_SP;
110     int analyzeFormat = PIXEL_FMT_YCRCB_420_SP;
111     int streamIdPreview = 100;
112     int streamIdCapture = 101;
113     int captureWidth = 1280;
114     int captureHeight = 960;
115     int captureIdPreview = 2000;
116     int previewWidth = 1920;
117     int previewHeight = 1080;
118     int captureIdCapture = 2010;
119     int captureIdVideo = 2020;
120     int streamIdVideo = 102;
121     int videoHeight = 1080;
122     int videoWidth = 1920;
123     int analyzeWidth = 1920;
124     int analyzeHeight = 1080;
125     int snapshotWidth = 4160;
126     int snapshotHeight = 3120;
127     int streamIdAnalyze = 103;
128     std::vector<int> captureIds;
129     std::vector<int> streamIds;
130     int32_t imageDataSaveSwitch = SWITCH_OFF;
131 
132     int32_t rc;
133     bool status;
134     std::vector<std::string> cameraIds;
135     std::vector<uint8_t> abilityVec = {};
136     std::shared_ptr<CameraMetadata> ability = nullptr;
137     std::vector<StreamIntent> intents;
138     class StreamConsumer;
139     std::map<OHOS::Camera::StreamIntent, std::shared_ptr<StreamConsumer>> consumerMap_ = {};
140     class TestBufferConsumerListener : public OHOS::IBufferConsumerListener {
141     public:
TestBufferConsumerListener()142         TestBufferConsumerListener() {}
~TestBufferConsumerListener()143         ~TestBufferConsumerListener() {}
OnBufferAvailable()144         void OnBufferAvailable()
145         {
146             hasAvailablebuffer = true;
147         }
checkBufferAvailable()148         bool checkBufferAvailable()
149         {
150             if (hasAvailablebuffer) {
151                 hasAvailablebuffer = false;
152                 return true;
153             }
154             return false;
155         }
156     private:
157         bool hasAvailablebuffer = false;
158     };
159 
160     class StreamConsumer {
161     public:
162         void CalculateFps(int64_t timestamp, int32_t streamId);
163         void GetTimeStamp(int64_t *g_timestamp, uint32_t lenght, int64_t timestamp, int32_t gotSize);
164         OHOS::sptr<OHOS::IBufferProducer> CreateProducer(std::function<void(void*, uint32_t)> callback);
165         OHOS::sptr<BufferProducerSequenceable> CreateProducerSeq(std::function<void(void*, uint32_t)> callback);
TakeSnapshoe()166         void TakeSnapshoe()
167         {
168             shotCount_++;
169         }
WaitSnapshotEnd()170         void WaitSnapshotEnd()
171         {
172             std::cout << "ready to wait" << std::endl;
173             std::unique_lock<std::mutex> l(l_);
174             cv_.wait(l, [this]() {return shotCount_ == 0; });
175         }
~StreamConsumer()176         ~StreamConsumer()
177         {
178             running_ = false;
179             if (consumerThread_ != nullptr) {
180                 consumerThread_->join();
181                 delete consumerThread_;
182             }
183         }
184     public:
185         std::atomic<uint64_t> shotCount_ = 0;
186         std::mutex l_;
187         std::condition_variable cv_;
188         bool running_ = true;
189         OHOS::sptr<OHOS::IConsumerSurface> consumer_ = nullptr;
190         std::thread* consumerThread_ = nullptr;
191         std::function<void(void*, uint32_t)> callback_ = nullptr;
192         bool isFirstCalculateFps_ = false;
193         int timestampCount_ = 0;
194         static int64_t g_timestamp[2];
195         int64_t intervalTimestamp_ = 0;
196         const int64_t ONESECOND_OF_MICROSECOND_UNIT = 1000000000;
197         int64_t interval_ = ONESECOND_OF_MICROSECOND_UNIT;
198     };
199 
200     class DemoCameraDeviceCallback : public ICameraDeviceCallback {
201     public:
202         DemoCameraDeviceCallback() = default;
203         virtual ~DemoCameraDeviceCallback() = default;
204 
205         int32_t OnError(ErrorType type, int32_t errorMsg) override;
206         int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t> &result) override;
207     };
208 
209     using ResultCallback = std::function<void (uint64_t, const std::shared_ptr<CameraMetadata>)>;
210     static ResultCallback resultCallback_;
211 
212     class TestStreamOperatorCallback : public IStreamOperatorCallback {
213     public:
214         TestStreamOperatorCallback() = default;
215         virtual ~TestStreamOperatorCallback() = default;
216         int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamId) override;
217         int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo> &infos) override;
218         int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo> &infos) override;
219         int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t> &streamIds, uint64_t timestamp) override;
220     };
221 
222     class TestCameraHostCallback : public ICameraHostCallback {
223     public:
224         TestCameraHostCallback() = default;
225         virtual ~TestCameraHostCallback() = default;
226 
227         int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override;
228         int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override;
229         int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override;
230     };
231 };
232 }
233 #endif
234