• 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 HDI_COMMON_H
17 #define HDI_COMMON_H
18 
19 #include <stdlib.h>
20 #include <thread>
21 #include <iostream>
22 #include <unistd.h>
23 #ifdef CAMERA_UT_TEST
24     #include <gtest/gtest.h>
25 #endif
26 #include <sys/time.h>
27 #include <map>
28 #include <string>
29 #include <vector>
30 #include <fcntl.h>
31 #include "camera.h"
32 #include "v1_0/types.h"
33 #include "metadata_utils.h"
34 #include "v1_0/icamera_host.h"
35 #include "v1_0/icamera_device.h"
36 #include "v1_0/istream_operator.h"
37 #include "v1_0/camera_host_proxy.h"
38 #include "v1_0/ioffline_stream_operator.h"
39 #include "display_format.h"
40 #include "iconsumer_surface.h"
41 
42 namespace OHOS::Camera {
43 enum CameraUtConstants {
44     UT_SLEEP_TIME = 2,
45     UT_SECOND_TIMES = 3,
46     UT_SECOND_TIMES_MAX = 100,
47     UT_TUNNEL_MODE = 5,
48     UT_DATA_SIZE = 8,
49     UT_PREVIEW_SIZE = 3112960,
50     UT_MICROSECOND_TIMES = 500000,
51 };
52 
53 enum ImageDataSaveSwitch {
54     SWITCH_OFF,
55     SWITCH_ON,
56 };
57 using namespace OHOS::HDI::Camera::V1_0;
58 class HdiCommon {
59 public:
60     void Init();
61     void Open();
62     void Close();
63     void GetCameraMetadata();
64     void StartStream(std::vector<StreamIntent> intents);
65     void DefaultPreview(std::shared_ptr<StreamInfo> &infos);
66     void DefaultCapture(std::shared_ptr<StreamInfo> &infos);
67     void DefaultInfosPreview(std::shared_ptr<StreamInfo> &infos);
68     void DefaultInfosCapture(std::shared_ptr<StreamInfo> &infos);
69     void DefaultInfosVideo(std::shared_ptr<StreamInfo> &infos);
70     void DefaultInfosAnalyze(std::shared_ptr<StreamInfo> &infos);
71     uint64_t GetCurrentLocalTimeStamp();
72     int32_t DumpImageFile(int streamId, std::string suffix, const void* buffer, int32_t size);
73     void StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming);
74     void StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds);
75     OHOS::sptr<OHOS::Camera::ICameraHost> service = nullptr;
76     OHOS::sptr<ICameraDevice> cameraDevice = nullptr;
77     OHOS::sptr<IStreamOperatorCallback> streamOperatorCallback = nullptr;
78     OHOS::sptr<ICameraHostCallback> hostCallback = nullptr;
79     OHOS::sptr<IStreamOperator> streamOperator = nullptr;
80     class DemoCameraDeviceCallback;
81     OHOS::sptr<DemoCameraDeviceCallback> deviceCallback = nullptr;
82     std::vector<StreamInfo> streamInfos;
83     std::shared_ptr<StreamInfo> streamInfo = nullptr;
84     std::shared_ptr<StreamInfo> streamInfoSnapshot = nullptr;
85     std::shared_ptr<StreamInfo> streamInfoCapture = nullptr;
86     std::shared_ptr<StreamInfo> streamInfoAnalyze = nullptr;
87     std::shared_ptr<StreamInfo> streamInfoPre = nullptr;
88     std::shared_ptr<StreamInfo> streamInfoVideo = nullptr;
89     std::shared_ptr<CaptureInfo> captureInfo = nullptr;
90     int previewFormat = PIXEL_FMT_YCRCB_420_SP;
91     int videoFormat = PIXEL_FMT_YCRCB_420_SP;
92     int snapshotFormat = PIXEL_FMT_YCRCB_420_SP;
93     int analyzeFormat = PIXEL_FMT_YCRCB_420_SP;
94     int videoEncodeType = ENCODE_TYPE_H265;
95     int streamIdPreview = 100;
96     int streamIdCapture = 101;
97     int captureWidth = 1280;
98     int captureHeight = 960;
99     int captureIdPreview = 2000;
100     int previewWidth = 1920;
101     int previewHeight = 1080;
102     int captureIdCapture = 2010;
103     int captureIdVideo = 2020;
104     int streamIdVideo = 102;
105     int videoHeight = 1080;
106     int videoWidth = 1920;
107     int analyzeWidth = 1920;
108     int analyzeHeight = 1080;
109     int snapshotWidth = 4160;
110     int snapshotHeight = 3120;
111     int streamIdAnalyze = 103;
112     int usbCamera_previewWidth = 640;
113     int usbCamera_previewHeight = 480;
114     int usbCamera_videoWidth = 1280;
115     int usbCamera_videoHeight = 960;
116     int usbCamera_captureWidth = 1280;
117     int usbCamera_captureHeight = 960;
118     int usbCamera_analyzeWidth = 640;
119     int usbCamera_analyzeHeight = 480;
120     int usbCamera_previewFormat = PIXEL_FMT_RGBA_8888;
121     int usbCamera_videoFormat = PIXEL_FMT_YCRCB_420_SP;
122     int usbCamera_snapshotFormat = PIXEL_FMT_RGBA_8888;
123     int usbCamera_analyzeFormat = PIXEL_FMT_RGBA_8888;
124     int usbCamera_videoEncodeType = ENCODE_TYPE_H264;
125     std::vector<int> captureIds;
126     std::vector<int> streamIds;
127     int32_t imageDataSaveSwitch = SWITCH_OFF;
128     uint32_t itemCapacity = 100;
129     uint32_t dataCapacity = 2000;
130     uint32_t dataCount = 1;
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 
141     class TestBufferConsumerListener : public OHOS::IBufferConsumerListener {
142     public:
TestBufferConsumerListener()143         TestBufferConsumerListener() {}
~TestBufferConsumerListener()144         ~TestBufferConsumerListener() {}
OnBufferAvailable()145         void OnBufferAvailable()
146         {
147             hasAvailablebuffer = true;
148         }
checkBufferAvailable()149         bool checkBufferAvailable()
150         {
151             if (hasAvailablebuffer) {
152                 hasAvailablebuffer = false;
153                 return true;
154             }
155             return false;
156         }
157     private:
158         bool hasAvailablebuffer = false;
159     };
160 
161     class StreamConsumer {
162     public:
163         void CalculateFps(int64_t timestamp, int32_t streamId);
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         int64_t intervalTimestamp_ = 0;
195         const int64_t ONESECOND_OF_MICROSECOND_UNIT = 1000000000;
196         int64_t interval_ = ONESECOND_OF_MICROSECOND_UNIT;
197     };
198 
199     class DemoCameraDeviceCallback : public ICameraDeviceCallback {
200     public:
201         std::shared_ptr<CameraMetadata> resultMeta = nullptr;
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