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