1/* 2 * Copyright (c) 2021 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 CAMERA_TEST_COMMON_H 16#define CAMERA_TEST_COMMON_H 17 18#include <stdlib.h> 19#include <limits.h> 20#include <gtest/gtest.h> 21#include <iostream> 22#include <climits> 23#include <sys/stat.h> 24#include <fcntl.h> 25#include <unistd.h> 26#include <sys/ioctl.h> 27#include <sys/wait.h> 28#include <thread> 29#include <stdio.h> 30#include <sys/time.h> 31#include <vector> 32#include <map> 33#include "utils.h" 34#include "camera.h" 35#include "camera_host.h" 36#include "types.h" 37#include <surface.h> 38#include "idevice_manager.h" 39#include "camera_metadata_info.h" 40#include "ibuffer.h" 41#include "display_format.h" 42#include <hdf_log.h> 43#include <osal_mem.h> 44#include "securec.h" 45#include "icamera_host.h" 46#include "icamera_device.h" 47#include "istream_operator.h" 48#include "ioffline_stream_operator.h" 49#include "camera_host_callback.h" 50#include "camera_device_callback.h" 51#include "stream_operator_callback.h" 52#include "video_key_info.h" 53#include "type_common.h" 54 55namespace OHOS::Camera { 56class Test { 57public: 58 void Init(); 59 void Open(); 60 void Close(); 61 void GetCameraAbility(); 62 uint64_t GetCurrentLocalTimeStamp(); 63 int32_t SaveYUV(const char* type, const void* buffer, int32_t size); 64 int32_t SaveVideoFile(const char* type, const void* buffer, int32_t size, int32_t operationMode); 65 void StartStream(std::vector<Camera::StreamIntent> intents); 66 void StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming); 67 void StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds); 68 void StopOfflineStream(int captureId); 69 void GetCameraMetadata(); 70 void StopConsumer(std::vector<Camera::StreamIntent> intents); 71 void CreateStreamOperatorCallback(); 72 void CreateDeviceCallback(); 73 void CreateOfflineStreamOperatorCallback(); 74 void StreamInfoFormat(); 75 76 OHOS::sptr<StreamOperatorCallback> streamOperatorCallback = nullptr; 77 OHOS::sptr<CameraHostCallback> hostCallback = nullptr; 78 OHOS::sptr<CameraDeviceCallback> deviceCallback = nullptr; 79 OHOS::sptr<IStreamOperator> streamOperator = nullptr; 80 OHOS::sptr<Camera::IOfflineStreamOperator> offlineStreamOperator = nullptr; 81 OHOS::sptr<IStreamOperatorCallback> offlineStreamOperatorCallback = nullptr; 82 83 std::shared_ptr<OHOS::Camera::CaptureInfo> captureInfo = nullptr; 84 std::vector<std::shared_ptr<OHOS::Camera::StreamInfo>> streamInfos; 85 std::shared_ptr<OHOS::Camera::StreamInfo> streamInfo = nullptr; 86 std::shared_ptr<OHOS::Camera::StreamInfo> streamInfo2 = nullptr; 87 std::shared_ptr<OHOS::Camera::StreamInfo> streamInfo_pre = nullptr; 88 std::shared_ptr<OHOS::Camera::StreamInfo> streamInfo_video = nullptr; 89 std::shared_ptr<OHOS::Camera::StreamInfo> streamInfo_capture = nullptr; 90 std::vector<std::string> cameraIds; 91 int streamId_preview = 1000; 92 int streamId_preview_double = 1001; 93 int streamId_capture = 1010; 94 int streamId_video = 1020; 95 int captureId_preview = 2000; 96 int captureId_preview_double = 2001; 97 int captureId_capture = 3000; 98 int captureId_video = 4000; 99 std::vector<int> captureIds; 100 std::vector<int> streamIds; 101 std::vector<Camera::StreamIntent> intents; 102 OHOS::Camera::CamRetCode rc; 103 104#ifdef CAMERA_BUILT_ON_OHOS_LITE 105 std::shared_ptr<OHOS::Camera::CameraHost> service = nullptr; 106 std::shared_ptr<ICameraDevice> cameraDevice = nullptr; 107#else 108 OHOS::sptr<OHOS::Camera::ICameraHost> service = nullptr; 109 OHOS::sptr<ICameraDevice> cameraDevice = nullptr; 110#endif 111 std::shared_ptr<CameraAbility> ability = nullptr; 112 113 bool status; 114 bool onCameraStatusFlag; 115 bool onFlashlightStatusFlag; 116 bool onErrorFlag; 117 bool onResultFlag; 118 bool captureStartFlag; 119 bool captureEndFlag; 120 bool captureErrorFlag; 121 bool frameShutterFlag; 122 int previewBufCnt = 0; 123 int32_t videoFd = -1; 124 class StreamConsumer; 125 std::map<OHOS::Camera::StreamIntent, std::shared_ptr<StreamConsumer>> consumerMap_ = {}; 126 127 class TestBufferConsumerListener : public IBufferConsumerListener { 128 public: 129 TestBufferConsumerListener() {} 130 ~TestBufferConsumerListener() {} 131 void OnBufferAvailable() {} 132 }; 133 134 class StreamConsumer { 135 public: 136 void StopConsumer(); 137#ifdef CAMERA_BUILT_ON_OHOS_LITE 138 std::shared_ptr<OHOS::Surface> CreateProducer(std::function<void(OHOS::SurfaceBuffer* buffer)> callback); 139#else 140 OHOS::sptr<OHOS::IBufferProducer> CreateProducer(std::function<void(void*, uint32_t)> callback); 141#endif 142 void TakeSnapshot() 143 { 144 shotCount_++; 145 } 146 void WaitSnapshotEnd() 147 { 148 std::cout << "ready to wait" << std::endl; 149 std::unique_lock<std::mutex> l(l_); 150 cv_.wait(l, [this]() { return shotCount_ == 0; }); 151 } 152 ~StreamConsumer() 153 { 154 running_ = false; 155 if (consumerThread_ != nullptr) { 156 consumerThread_->join(); 157 delete consumerThread_; 158 } 159 } 160 public: 161 std::atomic<uint64_t> shotCount_ = 0; 162 std::mutex l_; 163 std::condition_variable cv_; 164 bool running_ = true; 165#ifdef CAMERA_BUILT_ON_OHOS_LITE 166 std::shared_ptr<OHOS::Surface> consumer_ = nullptr; 167 std::function<void(OHOS::SurfaceBuffer*)> callback_ = nullptr; 168#else 169 OHOS::sptr<OHOS::Surface> consumer_ = nullptr; 170 std::function<void(void*, uint32_t)> callback_ = nullptr; 171#endif 172 std::thread* consumerThread_ = nullptr; 173 }; 174}; 175 176class HdiHostCallback : public CameraHostCallback { 177public: 178 Test *test_; 179 virtual ~HdiHostCallback() = default; 180 HdiHostCallback(Test *test) 181 { 182 test_ = test; 183 } 184 void OnCameraStatus(const std::string &cameraId, CameraStatus status) override 185 { 186 test_->onCameraStatusFlag = true; 187 } 188 void OnFlashlightStatus(const std::string &cameraId, FlashlightStatus status) override 189 { 190 test_->onFlashlightStatusFlag = true; 191 } 192}; 193class HdiDeviceCallback : public CameraDeviceCallback { 194public: 195 Test *test_; 196 virtual ~HdiDeviceCallback() = default; 197 HdiDeviceCallback(Test *test) 198 { 199 test_ = test; 200 } 201 void OnError(ErrorType type, int32_t errorMsg) override 202 { 203 test_->onErrorFlag = true; 204 } 205 void OnResult(uint64_t timestamp, const std::shared_ptr<CameraMetadata> &result) override 206 { 207 test_->onResultFlag = true; 208 } 209}; 210class HdiOperatorCallback : public StreamOperatorCallback { 211public: 212 Test *test_; 213 virtual ~HdiOperatorCallback() = default; 214 HdiOperatorCallback(Test *test) 215 { 216 test_ = test; 217 } 218 void OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamId) override 219 { 220 test_->captureStartFlag = true; 221 } 222 void OnCaptureEnded(int32_t captureId, 223 const std::vector<std::shared_ptr<CaptureEndedInfo>> &info) override 224 { 225 test_->captureEndFlag = true; 226 } 227 void OnCaptureError(int32_t captureId, 228 const std::vector<std::shared_ptr<CaptureErrorInfo>> &info) override 229 { 230 test_->captureErrorFlag = true; 231 } 232 void OnFrameShutter(int32_t captureId, 233 const std::vector<int32_t> &streamId, uint64_t timestamp) override 234 { 235 test_->frameShutterFlag = true; 236 } 237}; 238} 239#endif 240