1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file expected in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#ifndef HDI_COMMON_H
17094332d3Sopenharmony_ci#define HDI_COMMON_H
18094332d3Sopenharmony_ci
19094332d3Sopenharmony_ci#include <stdlib.h>
20094332d3Sopenharmony_ci#include <thread>
21094332d3Sopenharmony_ci#include <iostream>
22094332d3Sopenharmony_ci#include <unistd.h>
23094332d3Sopenharmony_ci#ifdef CAMERA_UT_TEST
24094332d3Sopenharmony_ci    #include <gtest/gtest.h>
25094332d3Sopenharmony_ci#endif
26094332d3Sopenharmony_ci#include <sys/time.h>
27094332d3Sopenharmony_ci#include <map>
28094332d3Sopenharmony_ci#include <string>
29094332d3Sopenharmony_ci#include <vector>
30094332d3Sopenharmony_ci#include <fcntl.h>
31094332d3Sopenharmony_ci#include "camera.h"
32094332d3Sopenharmony_ci#include "v1_0/types.h"
33094332d3Sopenharmony_ci#include "metadata_utils.h"
34094332d3Sopenharmony_ci#include "v1_0/icamera_host.h"
35094332d3Sopenharmony_ci#include "v1_0/icamera_device.h"
36094332d3Sopenharmony_ci#include "v1_0/istream_operator.h"
37094332d3Sopenharmony_ci#include "v1_0/camera_host_proxy.h"
38094332d3Sopenharmony_ci#include "v1_0/ioffline_stream_operator.h"
39094332d3Sopenharmony_ci#include "display_format.h"
40094332d3Sopenharmony_ci#include "iconsumer_surface.h"
41094332d3Sopenharmony_ci
42094332d3Sopenharmony_cinamespace OHOS::Camera {
43094332d3Sopenharmony_cienum CameraUtConstants {
44094332d3Sopenharmony_ci    UT_SLEEP_TIME = 2,
45094332d3Sopenharmony_ci    UT_SECOND_TIMES = 3,
46094332d3Sopenharmony_ci    UT_SECOND_TIMES_MAX = 100,
47094332d3Sopenharmony_ci    UT_TUNNEL_MODE = 5,
48094332d3Sopenharmony_ci    UT_DATA_SIZE = 8,
49094332d3Sopenharmony_ci    UT_PREVIEW_SIZE = 3112960,
50094332d3Sopenharmony_ci    UT_MICROSECOND_TIMES = 500000,
51094332d3Sopenharmony_ci};
52094332d3Sopenharmony_ci
53094332d3Sopenharmony_cienum ImageDataSaveSwitch {
54094332d3Sopenharmony_ci    SWITCH_OFF,
55094332d3Sopenharmony_ci    SWITCH_ON,
56094332d3Sopenharmony_ci};
57094332d3Sopenharmony_ciusing namespace OHOS::HDI::Camera::V1_0;
58094332d3Sopenharmony_ciclass HdiCommon {
59094332d3Sopenharmony_cipublic:
60094332d3Sopenharmony_ci    void Init();
61094332d3Sopenharmony_ci    void Open();
62094332d3Sopenharmony_ci    void Close();
63094332d3Sopenharmony_ci    void GetCameraMetadata();
64094332d3Sopenharmony_ci    void StartStream(std::vector<StreamIntent> intents);
65094332d3Sopenharmony_ci    void DefaultPreview(std::shared_ptr<StreamInfo> &infos);
66094332d3Sopenharmony_ci    void DefaultCapture(std::shared_ptr<StreamInfo> &infos);
67094332d3Sopenharmony_ci    void DefaultInfosPreview(std::shared_ptr<StreamInfo> &infos);
68094332d3Sopenharmony_ci    void DefaultInfosCapture(std::shared_ptr<StreamInfo> &infos);
69094332d3Sopenharmony_ci    void DefaultInfosVideo(std::shared_ptr<StreamInfo> &infos);
70094332d3Sopenharmony_ci    void DefaultInfosAnalyze(std::shared_ptr<StreamInfo> &infos);
71094332d3Sopenharmony_ci    uint64_t GetCurrentLocalTimeStamp();
72094332d3Sopenharmony_ci    int32_t DumpImageFile(int streamId, std::string suffix, const void* buffer, int32_t size);
73094332d3Sopenharmony_ci    void StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming);
74094332d3Sopenharmony_ci    void StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds);
75094332d3Sopenharmony_ci    OHOS::sptr<OHOS::Camera::ICameraHost> service = nullptr;
76094332d3Sopenharmony_ci    OHOS::sptr<ICameraDevice> cameraDevice = nullptr;
77094332d3Sopenharmony_ci    OHOS::sptr<IStreamOperatorCallback> streamOperatorCallback = nullptr;
78094332d3Sopenharmony_ci    OHOS::sptr<ICameraHostCallback> hostCallback = nullptr;
79094332d3Sopenharmony_ci    OHOS::sptr<IStreamOperator> streamOperator = nullptr;
80094332d3Sopenharmony_ci    class DemoCameraDeviceCallback;
81094332d3Sopenharmony_ci    OHOS::sptr<DemoCameraDeviceCallback> deviceCallback = nullptr;
82094332d3Sopenharmony_ci    std::vector<StreamInfo> streamInfos;
83094332d3Sopenharmony_ci    std::shared_ptr<StreamInfo> streamInfo = nullptr;
84094332d3Sopenharmony_ci    std::shared_ptr<StreamInfo> streamInfoSnapshot = nullptr;
85094332d3Sopenharmony_ci    std::shared_ptr<StreamInfo> streamInfoCapture = nullptr;
86094332d3Sopenharmony_ci    std::shared_ptr<StreamInfo> streamInfoAnalyze = nullptr;
87094332d3Sopenharmony_ci    std::shared_ptr<StreamInfo> streamInfoPre = nullptr;
88094332d3Sopenharmony_ci    std::shared_ptr<StreamInfo> streamInfoVideo = nullptr;
89094332d3Sopenharmony_ci    std::shared_ptr<CaptureInfo> captureInfo = nullptr;
90094332d3Sopenharmony_ci    int previewFormat = PIXEL_FMT_YCRCB_420_SP;
91094332d3Sopenharmony_ci    int videoFormat = PIXEL_FMT_YCRCB_420_SP;
92094332d3Sopenharmony_ci    int snapshotFormat = PIXEL_FMT_YCRCB_420_SP;
93094332d3Sopenharmony_ci    int analyzeFormat = PIXEL_FMT_YCRCB_420_SP;
94094332d3Sopenharmony_ci    int videoEncodeType = ENCODE_TYPE_H265;
95094332d3Sopenharmony_ci    int streamIdPreview = 100;
96094332d3Sopenharmony_ci    int streamIdCapture = 101;
97094332d3Sopenharmony_ci    int captureWidth = 1280;
98094332d3Sopenharmony_ci    int captureHeight = 960;
99094332d3Sopenharmony_ci    int captureIdPreview = 2000;
100094332d3Sopenharmony_ci    int previewWidth = 1920;
101094332d3Sopenharmony_ci    int previewHeight = 1080;
102094332d3Sopenharmony_ci    int captureIdCapture = 2010;
103094332d3Sopenharmony_ci    int captureIdVideo = 2020;
104094332d3Sopenharmony_ci    int streamIdVideo = 102;
105094332d3Sopenharmony_ci    int videoHeight = 1080;
106094332d3Sopenharmony_ci    int videoWidth = 1920;
107094332d3Sopenharmony_ci    int analyzeWidth = 1920;
108094332d3Sopenharmony_ci    int analyzeHeight = 1080;
109094332d3Sopenharmony_ci    int snapshotWidth = 4160;
110094332d3Sopenharmony_ci    int snapshotHeight = 3120;
111094332d3Sopenharmony_ci    int streamIdAnalyze = 103;
112094332d3Sopenharmony_ci    int usbCamera_previewWidth = 640;
113094332d3Sopenharmony_ci    int usbCamera_previewHeight = 480;
114094332d3Sopenharmony_ci    int usbCamera_videoWidth = 1280;
115094332d3Sopenharmony_ci    int usbCamera_videoHeight = 960;
116094332d3Sopenharmony_ci    int usbCamera_captureWidth = 1280;
117094332d3Sopenharmony_ci    int usbCamera_captureHeight = 960;
118094332d3Sopenharmony_ci    int usbCamera_analyzeWidth = 640;
119094332d3Sopenharmony_ci    int usbCamera_analyzeHeight = 480;
120094332d3Sopenharmony_ci    int usbCamera_previewFormat = PIXEL_FMT_RGBA_8888;
121094332d3Sopenharmony_ci    int usbCamera_videoFormat = PIXEL_FMT_YCRCB_420_SP;
122094332d3Sopenharmony_ci    int usbCamera_snapshotFormat = PIXEL_FMT_RGBA_8888;
123094332d3Sopenharmony_ci    int usbCamera_analyzeFormat = PIXEL_FMT_RGBA_8888;
124094332d3Sopenharmony_ci    int usbCamera_videoEncodeType = ENCODE_TYPE_H264;
125094332d3Sopenharmony_ci    std::vector<int> captureIds;
126094332d3Sopenharmony_ci    std::vector<int> streamIds;
127094332d3Sopenharmony_ci    int32_t imageDataSaveSwitch = SWITCH_OFF;
128094332d3Sopenharmony_ci    uint32_t itemCapacity = 100;
129094332d3Sopenharmony_ci    uint32_t dataCapacity = 2000;
130094332d3Sopenharmony_ci    uint32_t dataCount = 1;
131094332d3Sopenharmony_ci
132094332d3Sopenharmony_ci    int32_t rc;
133094332d3Sopenharmony_ci    bool status;
134094332d3Sopenharmony_ci    std::vector<std::string> cameraIds;
135094332d3Sopenharmony_ci    std::vector<uint8_t> abilityVec = {};
136094332d3Sopenharmony_ci    std::shared_ptr<CameraMetadata> ability = nullptr;
137094332d3Sopenharmony_ci    std::vector<StreamIntent> intents;
138094332d3Sopenharmony_ci    class StreamConsumer;
139094332d3Sopenharmony_ci    std::map<OHOS::Camera::StreamIntent, std::shared_ptr<StreamConsumer>> consumerMap_ = {};
140094332d3Sopenharmony_ci
141094332d3Sopenharmony_ci    class TestBufferConsumerListener : public OHOS::IBufferConsumerListener {
142094332d3Sopenharmony_ci    public:
143094332d3Sopenharmony_ci        TestBufferConsumerListener() {}
144094332d3Sopenharmony_ci        ~TestBufferConsumerListener() {}
145094332d3Sopenharmony_ci        void OnBufferAvailable()
146094332d3Sopenharmony_ci        {
147094332d3Sopenharmony_ci            hasAvailablebuffer = true;
148094332d3Sopenharmony_ci        }
149094332d3Sopenharmony_ci        bool checkBufferAvailable()
150094332d3Sopenharmony_ci        {
151094332d3Sopenharmony_ci            if (hasAvailablebuffer) {
152094332d3Sopenharmony_ci                hasAvailablebuffer = false;
153094332d3Sopenharmony_ci                return true;
154094332d3Sopenharmony_ci            }
155094332d3Sopenharmony_ci            return false;
156094332d3Sopenharmony_ci        }
157094332d3Sopenharmony_ci    private:
158094332d3Sopenharmony_ci        bool hasAvailablebuffer = false;
159094332d3Sopenharmony_ci    };
160094332d3Sopenharmony_ci
161094332d3Sopenharmony_ci    class StreamConsumer {
162094332d3Sopenharmony_ci    public:
163094332d3Sopenharmony_ci        void CalculateFps(int64_t timestamp, int32_t streamId);
164094332d3Sopenharmony_ci        OHOS::sptr<OHOS::IBufferProducer> CreateProducer(std::function<void(void*, uint32_t)> callback);
165094332d3Sopenharmony_ci        OHOS::sptr<BufferProducerSequenceable> CreateProducerSeq(std::function<void(void*, uint32_t)> callback);
166094332d3Sopenharmony_ci        void TakeSnapshoe()
167094332d3Sopenharmony_ci        {
168094332d3Sopenharmony_ci            shotCount_++;
169094332d3Sopenharmony_ci        }
170094332d3Sopenharmony_ci        void WaitSnapshotEnd()
171094332d3Sopenharmony_ci        {
172094332d3Sopenharmony_ci            std::cout << "ready to wait" << std::endl;
173094332d3Sopenharmony_ci            std::unique_lock<std::mutex> l(l_);
174094332d3Sopenharmony_ci            cv_.wait(l, [this]() {return shotCount_ == 0; });
175094332d3Sopenharmony_ci        }
176094332d3Sopenharmony_ci        ~StreamConsumer()
177094332d3Sopenharmony_ci        {
178094332d3Sopenharmony_ci            running_ = false;
179094332d3Sopenharmony_ci            if (consumerThread_ != nullptr) {
180094332d3Sopenharmony_ci                consumerThread_->join();
181094332d3Sopenharmony_ci                delete consumerThread_;
182094332d3Sopenharmony_ci            }
183094332d3Sopenharmony_ci        }
184094332d3Sopenharmony_ci    public:
185094332d3Sopenharmony_ci        std::atomic<uint64_t> shotCount_ = 0;
186094332d3Sopenharmony_ci        std::mutex l_;
187094332d3Sopenharmony_ci        std::condition_variable cv_;
188094332d3Sopenharmony_ci        bool running_ = true;
189094332d3Sopenharmony_ci        OHOS::sptr<OHOS::IConsumerSurface> consumer_ = nullptr;
190094332d3Sopenharmony_ci        std::thread* consumerThread_ = nullptr;
191094332d3Sopenharmony_ci        std::function<void(void*, uint32_t)> callback_ = nullptr;
192094332d3Sopenharmony_ci        bool isFirstCalculateFps_ = false;
193094332d3Sopenharmony_ci        int timestampCount_ = 0;
194094332d3Sopenharmony_ci        int64_t intervalTimestamp_ = 0;
195094332d3Sopenharmony_ci        const int64_t ONESECOND_OF_MICROSECOND_UNIT = 1000000000;
196094332d3Sopenharmony_ci        int64_t interval_ = ONESECOND_OF_MICROSECOND_UNIT;
197094332d3Sopenharmony_ci    };
198094332d3Sopenharmony_ci
199094332d3Sopenharmony_ci    class DemoCameraDeviceCallback : public ICameraDeviceCallback {
200094332d3Sopenharmony_ci    public:
201094332d3Sopenharmony_ci        std::shared_ptr<CameraMetadata> resultMeta = nullptr;
202094332d3Sopenharmony_ci        DemoCameraDeviceCallback() = default;
203094332d3Sopenharmony_ci        virtual ~DemoCameraDeviceCallback() = default;
204094332d3Sopenharmony_ci
205094332d3Sopenharmony_ci        int32_t OnError(ErrorType type, int32_t errorMsg) override;
206094332d3Sopenharmony_ci        int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t> &result) override;
207094332d3Sopenharmony_ci    };
208094332d3Sopenharmony_ci
209094332d3Sopenharmony_ci    using ResultCallback = std::function<void (uint64_t, const std::shared_ptr<CameraMetadata>)>;
210094332d3Sopenharmony_ci    static ResultCallback resultCallback_;
211094332d3Sopenharmony_ci
212094332d3Sopenharmony_ci    class TestStreamOperatorCallback : public IStreamOperatorCallback {
213094332d3Sopenharmony_ci    public:
214094332d3Sopenharmony_ci        TestStreamOperatorCallback() = default;
215094332d3Sopenharmony_ci        virtual ~TestStreamOperatorCallback() = default;
216094332d3Sopenharmony_ci        int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamId) override;
217094332d3Sopenharmony_ci        int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo> &infos) override;
218094332d3Sopenharmony_ci        int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo> &infos) override;
219094332d3Sopenharmony_ci        int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t> &streamIds, uint64_t timestamp) override;
220094332d3Sopenharmony_ci    };
221094332d3Sopenharmony_ci
222094332d3Sopenharmony_ci    class TestCameraHostCallback : public ICameraHostCallback {
223094332d3Sopenharmony_ci    public:
224094332d3Sopenharmony_ci        TestCameraHostCallback() = default;
225094332d3Sopenharmony_ci        virtual ~TestCameraHostCallback() = default;
226094332d3Sopenharmony_ci
227094332d3Sopenharmony_ci        int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override;
228094332d3Sopenharmony_ci        int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override;
229094332d3Sopenharmony_ci        int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override;
230094332d3Sopenharmony_ci    };
231094332d3Sopenharmony_ci};
232094332d3Sopenharmony_ci}
233094332d3Sopenharmony_ci#endif
234