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