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