1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except 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 #ifndef DISTRIBUTED_CAMERA_TEST_COMMON_H 16 #define DISTRIBUTED_CAMERA_TEST_COMMON_H 17 18 #include <climits> 19 #include <cstdio> 20 #include <cstdlib> 21 #include <fcntl.h> 22 #include <iostream> 23 #include <map> 24 #include <sys/ioctl.h> 25 #include <sys/stat.h> 26 #include <sys/time.h> 27 #include <sys/wait.h> 28 #include <thread> 29 #include <unistd.h> 30 #include <vector> 31 32 #include "camera_metadata_info.h" 33 #include "distributed_hardware_log.h" 34 #include "idistributed_hardware_source.h" 35 #include "iservice_registry.h" 36 #include "securec.h" 37 #include "surface.h" 38 39 #include "constants.h" 40 #include "dcamera.h" 41 #include "dcamera_host.h" 42 #include "v1_0/dcamera_types.h" 43 #include "v1_0/icamera_device.h" 44 #include "v1_0/icamera_host.h" 45 #include "v1_0/ioffline_stream_operator.h" 46 #include "v1_0/istream_operator.h" 47 #include "v1_0/types.h" 48 49 namespace OHOS { 50 namespace DistributedHardware { 51 using namespace OHOS::HDI::DistributedCamera::V1_0; 52 using namespace OHOS::HDI::Camera::V1_0; 53 class Test { 54 public: 55 void Init(); 56 void Open(); 57 void Close(); 58 std::shared_ptr<CameraAbility> GetCameraAbility(); 59 uint64_t GetCurrentLocalTimeStamp(); 60 int32_t SaveYUV(const char* type, const void* buffer, int32_t size); 61 int32_t SaveVideoFile(const char* type, const void* buffer, int32_t size, int32_t operationMode); 62 void StartStream(std::vector<StreamIntent> intents); 63 void StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming); 64 void StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds); 65 void StopOfflineStream(int captureId); 66 void GetCameraMetadata(); 67 68 void SetPreviewStreamInfo(); 69 void SetVideoStreamInfo(); 70 void SetPhotoStreamInfo(); 71 72 OHOS::sptr<DStreamOperatorCallback> streamOperatorCallback = nullptr; 73 OHOS::sptr<DCameraHostCallback> hostCallback = nullptr; 74 OHOS::sptr<DCameraDeviceCallback> deviceCallback = nullptr; 75 OHOS::sptr<IStreamOperator> streamOperator = nullptr; 76 OHOS::sptr<IOfflineStreamOperator> offlineStreamOperator = nullptr; 77 OHOS::sptr<IStreamOperatorCallback> offlineStreamOperatorCallback = nullptr; 78 CaptureInfo captureInfo; 79 std::vector<StreamInfo> streamInfos; 80 StreamInfo streamInfo_pre; 81 StreamInfo streamInfo_video; 82 StreamInfo streamInfo_capture; 83 std::vector<std::string> cameraIds; 84 int streamId_preview = 1000; 85 int streamId_preview_double = 1001; 86 int streamId_capture = 1010; 87 int streamId_video = 1020; 88 int captureId_preview = 2000; 89 int captureId_preview_double = 2001; 90 int captureId_capture = 2010; 91 int captureId_video = 2020; 92 int preview_format = PIXEL_FMT_YCRCB_420_SP; 93 int video_format = PIXEL_FMT_YCRCB_420_SP; 94 int snapshot_format = PIXEL_FMT_YCRCB_420_SP; 95 int preview_width = 1920; 96 int preview_height = 1080; 97 int snapshot_width = 4160; 98 int snapshot_height = 3120; 99 int video_width = 1920; 100 int video_height = 1080; 101 std::vector<int> captureIds; 102 std::vector<int> streamIds; 103 std::vector<StreamIntent> intents; 104 OHOS::HDI::Camera::V1_0::CamRetCode rc; 105 OHOS::sptr<OHOS::HDI::Camera::V1_0::ICameraHost> service = nullptr; 106 std::vector<uint8_t> ability; 107 OHOS::sptr<ICameraDevice> cameraDevice = nullptr; 108 bool status; 109 int previewBufCnt = 0; 110 int32_t videoFd = -1; 111 class StreamConsumer; 112 std::map<OHOS::HDI::Camera::V1_0::StreamIntent, std::shared_ptr<StreamConsumer>> consumerMap_ = {}; 113 114 class TestBufferConsumerListener : public IBufferConsumerListener { 115 public: TestBufferConsumerListener()116 TestBufferConsumerListener() {} ~TestBufferConsumerListener()117 ~TestBufferConsumerListener() {} OnBufferAvailable()118 void OnBufferAvailable() {} 119 }; 120 121 class StreamConsumer { 122 public: 123 OHOS::sptr<OHOS::IBufferProducer> CreateProducer(std::function<void(void*, uint32_t)> callback); TakeSnapshot()124 void TakeSnapshot() 125 { 126 shotCount_++; 127 } WaitSnapshotEnd()128 void WaitSnapshotEnd() 129 { 130 std::cout << "ready to wait" << std::endl; 131 std::unique_lock<std::mutex> l(l_); 132 cv_.wait(l, [this]() { return shotCount_ == 0; }); 133 } ~StreamConsumer()134 ~StreamConsumer() 135 { 136 running_ = false; 137 if (consumerThread_ != nullptr) { 138 consumerThread_->join(); 139 delete consumerThread_; 140 } 141 } 142 143 public: 144 std::atomic<uint64_t> shotCount_ = 0; 145 std::mutex l_; 146 std::condition_variable cv_; 147 bool running_ = true; 148 OHOS::sptr<OHOS::Surface> consumer_ = nullptr; 149 std::thread* consumerThread_ = nullptr; 150 std::function<void(void*, uint32_t)> callback_ = nullptr; 151 }; 152 }; 153 154 class DCameraMockRegisterCallback : public RegisterCallback { 155 public: 156 virtual ~DCameraMockRegisterCallback() = default; OnRegisterResult(const std::string & devId,const std::string & dhId,int32_t status,const std::string & data)157 int32_t OnRegisterResult(const std::string &devId, const std::string &dhId, int32_t status, 158 const std::string &data) 159 { 160 cout << "Register devId: " << devId << " dhId: " << dhId << " status:" << status << endl; 161 return 0; 162 } 163 }; 164 165 class DCameraMockUnRegisterCallback : public UnregisterCallback { 166 public: 167 virtual ~DCameraMockUnRegisterCallback() = default; OnUnregisterResult(const std::string & devId,const std::string & dhId,int32_t status,const std::string & data)168 int32_t OnUnregisterResult(const std::string &devId, const std::string &dhId, int32_t status, 169 const std::string &data) 170 { 171 cout << "UnRegister devId: " << devId << " dhId: " << dhId << " status:" << status << endl; 172 return 0; 173 } 174 }; 175 } // namespace DistributedHardware 176 } // namespace OHOS 177 #endif // DISTRIBUTED_CAMERA_TEST_COMMON_H 178