• 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_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     uint64_t GetCurrentLocalTimeStamp();
82     int32_t DumpImageFile(int streamId, std::string suffix, const void* buffer, int32_t size);
83     void StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming);
84     void StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds);
85     OHOS::sptr<OHOS::Camera::ICameraHost> service = nullptr;
86     OHOS::sptr<OHOS::HDI::Camera::V1_1::ICameraHost> serviceV1_1 = nullptr;
87     OHOS::sptr<ICameraDevice> cameraDevice = nullptr;
88     OHOS::sptr<OHOS::HDI::Camera::V1_1::ICameraDevice> cameraDeviceV1_1 = nullptr;
89     OHOS::sptr<IStreamOperatorCallback> streamOperatorCallback = nullptr;
90     OHOS::sptr<ICameraHostCallback> hostCallback = nullptr;
91     OHOS::sptr<IStreamOperator> streamOperator = nullptr;
92     OHOS::sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperator_V1_1 = nullptr;
93     OHOS::sptr<ICameraDeviceCallback> deviceCallback = nullptr;
94     std::vector<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfos;
95     std::vector<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfosV1_1;
96     std::shared_ptr<OHOS::HDI::Camera::V1_1::PrelaunchConfig> prelaunchConfig = nullptr;
97     std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoV1_1 = nullptr;
98     std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfo = nullptr;
99     std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoSnapshot = nullptr;
100     std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoCapture = nullptr;
101     std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoAnalyze = nullptr;
102     std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoPre = nullptr;
103     std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoVideo = nullptr;
104     std::shared_ptr<CaptureInfo> captureInfo = nullptr;
105     int previewFormat = PIXEL_FMT_YCRCB_420_SP;
106     int videoFormat = PIXEL_FMT_YCRCB_420_SP;
107     int snapshotFormat = PIXEL_FMT_YCRCB_420_SP;
108     int analyzeFormat = PIXEL_FMT_YCRCB_420_SP;
109     int streamIdPreview = 100;
110     int streamIdCapture = 101;
111     int captureWidth = 1280;
112     int captureHeight = 960;
113     int captureIdPreview = 2000;
114     int previewWidth = 1920;
115     int previewHeight = 1080;
116     int captureIdCapture = 2010;
117     int captureIdVideo = 2020;
118     int streamIdVideo = 102;
119     int videoHeight = 1080;
120     int videoWidth = 1920;
121     int analyzeWidth = 1920;
122     int analyzeHeight = 1080;
123     int snapshotWidth = 4160;
124     int snapshotHeight = 3120;
125     int streamIdAnalyze = 103;
126     std::vector<int> captureIds;
127     std::vector<int> streamIds;
128     int32_t imageDataSaveSwitch = SWITCH_OFF;
129 
130     int32_t rc;
131     bool status;
132     std::vector<std::string> cameraIds;
133     std::vector<uint8_t> abilityVec = {};
134     std::shared_ptr<CameraMetadata> ability = nullptr;
135     std::vector<StreamIntent> intents;
136     class StreamConsumer;
137     std::map<OHOS::Camera::StreamIntent, std::shared_ptr<StreamConsumer>> consumerMap_ = {};
138     class TestBufferConsumerListener : public OHOS::IBufferConsumerListener {
139     public:
TestBufferConsumerListener()140         TestBufferConsumerListener() {}
~TestBufferConsumerListener()141         ~TestBufferConsumerListener() {}
OnBufferAvailable()142         void OnBufferAvailable()
143         {
144             hasAvailablebuffer = true;
145         }
checkBufferAvailable()146         bool checkBufferAvailable()
147         {
148             if (hasAvailablebuffer) {
149                 hasAvailablebuffer = false;
150                 return true;
151             }
152             return false;
153         }
154     private:
155         bool hasAvailablebuffer = false;
156     };
157 
158     class StreamConsumer {
159     public:
160         void CalculateFps(int64_t timestamp, int32_t streamId);
161         void GetTimeStamp(int64_t *g_timestamp, uint32_t lenght, int64_t timestamp, int32_t gotSize);
162         OHOS::sptr<OHOS::IBufferProducer> CreateProducer(std::function<void(void*, uint32_t)> callback);
163         OHOS::sptr<BufferProducerSequenceable> CreateProducerSeq(std::function<void(void*, uint32_t)> callback);
TakeSnapshoe()164         void TakeSnapshoe()
165         {
166             shotCount_++;
167         }
WaitSnapshotEnd()168         void WaitSnapshotEnd()
169         {
170             std::cout << "ready to wait" << std::endl;
171             std::unique_lock<std::mutex> l(l_);
172             cv_.wait(l, [this]() {return shotCount_ == 0; });
173         }
~StreamConsumer()174         ~StreamConsumer()
175         {
176             running_ = false;
177             if (consumerThread_ != nullptr) {
178                 consumerThread_->join();
179                 delete consumerThread_;
180             }
181         }
182     public:
183         std::atomic<uint64_t> shotCount_ = 0;
184         std::mutex l_;
185         std::condition_variable cv_;
186         bool running_ = true;
187         OHOS::sptr<OHOS::IConsumerSurface> consumer_ = nullptr;
188         std::thread* consumerThread_ = nullptr;
189         std::function<void(void*, uint32_t)> callback_ = nullptr;
190         bool isFirstCalculateFps_ = false;
191         int timestampCount_ = 0;
192         static int64_t g_timestamp[2];
193         int64_t intervalTimestamp_ = 0;
194         const int64_t ONESECOND_OF_MICROSECOND_UNIT = 1000000000;
195         int64_t interval_ = ONESECOND_OF_MICROSECOND_UNIT;
196     };
197 
198     class DemoCameraDeviceCallback : public ICameraDeviceCallback {
199     public:
200         DemoCameraDeviceCallback() = default;
201         virtual ~DemoCameraDeviceCallback() = default;
202 
203         int32_t OnError(ErrorType type, int32_t errorMsg) override;
204         int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t> &result) override;
205     };
206 
207     using ResultCallback = std::function<void (uint64_t, const std::shared_ptr<CameraMetadata>)>;
208     static ResultCallback resultCallback_;
209 
210     class TestStreamOperatorCallback : public IStreamOperatorCallback {
211     public:
212         TestStreamOperatorCallback() = default;
213         virtual ~TestStreamOperatorCallback() = default;
214         int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamId) override;
215         int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo> &infos) override;
216         int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo> &infos) override;
217         int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t> &streamIds, uint64_t timestamp) override;
218     };
219 
220     class TestCameraHostCallback : public ICameraHostCallback {
221     public:
222         TestCameraHostCallback() = default;
223         virtual ~TestCameraHostCallback() = default;
224 
225         int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override;
226         int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override;
227         int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override;
228     };
229 };
230 }
231 #endif
232