• 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 #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 streamIdPreview = 100;
79     int streamIdCapture = 101;
80     int captureIdPreview = 2000;
81     int previewWidth = 1920;
82     int previewHeight = 1080;
83     int captureIdCapture = 2010;
84     int captureIdVideo = 2020;
85     int streamIdVideo = 102;
86     int videoHeight = 1080;
87     int videoWidth = 1920;
88     int analyzeWidth = 1920;
89     int analyzeHeight = 1080;
90     int snapshotWidth = 4160;
91     int snapshotHeight = 3120;
92     int streamIdAnalyze = 103;
93     std::vector<int> captureIds;
94     std::vector<int> streamIds;
95     int32_t imageDataSaveSwitch = SWITCH_OFF;
96 
97     int32_t rc;
98     bool status;
99     std::vector<std::string> cameraIds;
100     std::vector<uint8_t> abilityVec = {};
101     std::shared_ptr<CameraMetadata> ability = nullptr;
102     std::vector<StreamIntent> intents;
103     class StreamConsumer;
104     std::map<OHOS::Camera::StreamIntent, std::shared_ptr<StreamConsumer>> consumerMap_ = {};
105 
106     class TestBufferConsumerListener : public OHOS::IBufferConsumerListener {
107     public:
TestBufferConsumerListener()108         TestBufferConsumerListener() {}
~TestBufferConsumerListener()109         ~TestBufferConsumerListener() {}
OnBufferAvailable()110         void OnBufferAvailable()
111         {
112             hasAvailablebuffer = true;
113         }
checkBufferAvailable()114         bool checkBufferAvailable()
115         {
116             if (hasAvailablebuffer) {
117                 hasAvailablebuffer = false;
118                 return true;
119             }
120             return false;
121         }
122     private:
123         bool hasAvailablebuffer = false;
124     };
125 
126     class StreamConsumer {
127     public:
128         void CalculateFps(int64_t timestamp, int32_t streamId);
129         OHOS::sptr<OHOS::IBufferProducer> CreateProducer(std::function<void(void*, uint32_t)> callback);
130         OHOS::sptr<BufferProducerSequenceable> CreateProducerSeq(std::function<void(void*, uint32_t)> callback);
TakeSnapshoe()131         void TakeSnapshoe()
132         {
133             shotCount_++;
134         }
WaitSnapshotEnd()135         void WaitSnapshotEnd()
136         {
137             std::cout << "ready to wait" << std::endl;
138             std::unique_lock<std::mutex> l(l_);
139             cv_.wait(l, [this]() {return shotCount_ == 0; });
140         }
~StreamConsumer()141         ~StreamConsumer()
142         {
143             running_ = false;
144             if (consumerThread_ != nullptr) {
145                 consumerThread_->join();
146                 delete consumerThread_;
147             }
148         }
149     public:
150         std::atomic<uint64_t> shotCount_ = 0;
151         std::mutex l_;
152         std::condition_variable cv_;
153         bool running_ = true;
154         OHOS::sptr<OHOS::IConsumerSurface> consumer_ = nullptr;
155         std::thread* consumerThread_ = nullptr;
156         std::function<void(void*, uint32_t)> callback_ = nullptr;
157         bool isFirstCalculateFps_ = false;
158         int timestampCount_ = 0;
159         int64_t intervalTimestamp_ = 0;
160         const int64_t ONESECOND_OF_MICROSECOND_UNIT = 1000000000;
161         int64_t interval_ = ONESECOND_OF_MICROSECOND_UNIT;
162     };
163 
164     class DemoCameraDeviceCallback : public ICameraDeviceCallback {
165     public:
166         DemoCameraDeviceCallback() = default;
167         virtual ~DemoCameraDeviceCallback() = default;
168 
169         int32_t OnError(ErrorType type, int32_t errorMsg) override;
170         int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t> &result) override;
171     };
172 
173     using ResultCallback = std::function<void (uint64_t, const std::shared_ptr<CameraMetadata>)>;
174     static ResultCallback resultCallback_;
175 
176     class TestStreamOperatorCallback : public IStreamOperatorCallback {
177     public:
178         TestStreamOperatorCallback() = default;
179         virtual ~TestStreamOperatorCallback() = default;
180         int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamId) override;
181         int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo> &infos) override;
182         int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo> &infos) override;
183         int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t> &streamIds, uint64_t timestamp) override;
184     };
185 
186     class TestCameraHostCallback : public ICameraHostCallback {
187     public:
188         TestCameraHostCallback() = default;
189         virtual ~TestCameraHostCallback() = default;
190 
191         int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override;
192         int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override;
193         int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override;
194     };
195 };
196 }