1 /* 2 * Copyright (c) 2021-2023 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 16 #ifndef COMMON_H 17 #define COMMON_H 18 19 #include <stdlib.h> 20 #include <thread> 21 #include <iostream> 22 #include <unistd.h> 23 #include <sys/time.h> 24 #include <map> 25 #include <vector> 26 #include <fcntl.h> 27 #include "camera.h" 28 #include "v1_2/types.h" 29 #include "v1_3/types.h" 30 #include "metadata_utils.h" 31 #include "v1_2/icamera_host.h" 32 #include "v1_2/icamera_device.h" 33 #include "v1_2/istream_operator.h" 34 #include "v1_2/camera_host_proxy.h" 35 #include "v1_2/image_process_service_proxy.h" 36 #include "v1_3/icamera_host.h" 37 #include "v1_3/icamera_device.h" 38 #include "v1_0/ioffline_stream_operator.h" 39 #include "display_format.h" 40 #include "video_key_info.h" 41 #include "iconsumer_surface.h" 42 43 namespace OHOS::Camera { 44 enum CameraUtConstants { 45 UT_SLEEP_TIME = 2, 46 UT_SECOND_TIMES = 3, 47 UT_SECOND_TIMES_MAX = 100, 48 UT_TUNNEL_MODE = 5, 49 UT_DATA_SIZE = 8, 50 UT_PREVIEW_SIZE = 3112960, 51 }; 52 53 enum Numbers { 54 ZERO, 55 ONE, 56 TWO, 57 THREE, 58 FOUR, 59 FIVE, 60 SIX, 61 SEVEN, 62 EIGHT, 63 NINE, 64 TEN, 65 }; 66 67 enum ImageDataSaveSwitch { 68 SWITCH_OFF, 69 SWITCH_ON, 70 }; 71 72 enum CameraIds { 73 DEVICE_0, // rear camera 74 DEVICE_1, // front camera 75 DEVICE_2, 76 DEVICE_3, 77 DEVICE_4, 78 DEVICE_5, 79 DEVICE_6, 80 }; 81 82 using namespace OHOS::HDI::Camera::V1_0; 83 class CameraManager { 84 public: 85 void Init(); 86 void InitV1_2(); 87 void InitV1_3(); 88 void Open(); 89 void OpenV1_2(); 90 void OpenV1_3(); 91 void OpenCameraV1_2(); 92 void Close(); 93 void GetCameraMetadata(); 94 void DefaultPreview( 95 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 96 void DefaultCapture( 97 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 98 void DefaultSketch( 99 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 100 void DefaultInfosPreview( 101 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 102 void DefaultInfosPreviewV1_2( 103 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 104 void DefaultInfosCapture( 105 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 106 void DefaultInfosAnalyze( 107 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 108 void DefaultInfosVideo( 109 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 110 void DefaultInfosSketch( 111 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> &infos); 112 void StartStream(std::vector<StreamIntent> intents); 113 uint64_t GetCurrentLocalTimeStamp(); 114 int32_t DumpImageFile(int streamId, std::string suffix, const void* buffer, int32_t size); 115 void StartCapture(int streamId, int captureId, bool shutterCallback, bool isStreaming); 116 void StopStream(std::vector<int>& captureIds, std::vector<int>& streamIds); 117 OHOS::sptr<OHOS::Camera::ICameraHost> service = nullptr; 118 OHOS::sptr<OHOS::HDI::Camera::V1_1::ICameraHost> serviceV1_1 = nullptr; 119 OHOS::sptr<OHOS::HDI::Camera::V1_2::ICameraHost> serviceV1_2 = nullptr; 120 OHOS::sptr<OHOS::HDI::Camera::V1_3::ICameraHost> serviceV1_3 = nullptr; 121 OHOS::sptr<ICameraDevice> cameraDevice = nullptr; 122 OHOS::sptr<OHOS::HDI::Camera::V1_1::ICameraDevice> cameraDeviceV1_1 = nullptr; 123 OHOS::sptr<OHOS::HDI::Camera::V1_2::ICameraDevice> cameraDeviceV1_2 = nullptr; 124 OHOS::sptr<OHOS::HDI::Camera::V1_3::ICameraDevice> cameraDeviceV1_3 = nullptr; 125 OHOS::sptr<IStreamOperatorCallback> streamOperatorCallback = nullptr; 126 OHOS::sptr<OHOS::HDI::Camera::V1_2::IStreamOperatorCallback> streamOperatorCallbackV1_2 = nullptr; 127 OHOS::sptr<ICameraHostCallback> hostCallback = nullptr; 128 OHOS::sptr<OHOS::HDI::Camera::V1_2::ICameraHostCallback> hostCallbackV1_2 = nullptr; 129 OHOS::sptr<IStreamOperator> streamOperator = nullptr; 130 OHOS::sptr<IOfflineStreamOperator> offlineStreamOperator = nullptr; 131 OHOS::sptr<OHOS::HDI::Camera::V1_1::IStreamOperator> streamOperator_V1_1 = nullptr; 132 OHOS::sptr<OHOS::HDI::Camera::V1_2::IStreamOperator> streamOperator_V1_2 = nullptr; 133 class DemoCameraDeviceCallback; 134 OHOS::sptr<DemoCameraDeviceCallback> deviceCallback = nullptr; 135 std::vector<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfos; 136 std::vector<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfosV1_1; 137 std::shared_ptr<OHOS::HDI::Camera::V1_1::PrelaunchConfig> prelaunchConfig = nullptr; 138 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoV1_1 = nullptr; 139 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfo = nullptr; 140 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoSnapshot = nullptr; 141 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoCapture = nullptr; 142 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoAnalyze = nullptr; 143 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoPre = nullptr; 144 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoVideo = nullptr; 145 std::shared_ptr<OHOS::HDI::Camera::V1_1::StreamInfo_V1_1> streamInfoSketch = nullptr; 146 std::shared_ptr<CaptureInfo> captureInfo = nullptr; 147 sptr<OHOS::HDI::Camera::V1_2::IImageProcessSession> imageProcessSession_ = nullptr; 148 sptr<OHOS::HDI::Camera::V1_2::IImageProcessService> imageProcessService_ = nullptr; 149 class TestImageProcessCallback; 150 sptr<TestImageProcessCallback> imageProcessCallback_ = nullptr; 151 std::vector<std::string> pendingImageIds_; 152 int previewFormat = PIXEL_FMT_YCRCB_420_SP; 153 int videoFormat = PIXEL_FMT_YCRCB_420_SP; 154 int snapshotFormat = PIXEL_FMT_YCRCB_420_SP; 155 int analyzeFormat = PIXEL_FMT_YCRCB_420_SP; 156 int streamIdPreview = 100; 157 int streamIdCapture = 101; 158 int streamIdSketch = 105; 159 int captureWidth = 1280; 160 int captureHeight = 960; 161 int sketchWidth = 640; 162 int sketchHeight = 480; 163 int captureIdPreview = 2000; 164 int captureIdSketch = 2050; 165 int previewWidth = 1920; 166 int previewHeight = 1080; 167 int captureIdCapture = 2010; 168 int captureIdVideo = 2020; 169 int streamIdVideo = 102; 170 int videoHeight = 1080; 171 int videoWidth = 1920; 172 int analyzeWidth = 1920; 173 int analyzeHeight = 1080; 174 int snapshotWidth = 4160; 175 int snapshotHeight = 3120; 176 int streamIdAnalyze = 103; 177 std::vector<int> captureIds; 178 std::vector<int> streamIds; 179 int32_t imageDataSaveSwitch = SWITCH_OFF; 180 uint32_t itemCapacity = 100; 181 uint32_t dataCapacity = 2000; 182 uint32_t dataCount = 1; 183 184 int32_t rc; 185 bool status; 186 float statusV1_2; 187 static FlashlightStatus statusCallback; 188 std::vector<std::string> cameraIds; 189 std::vector<uint8_t> abilityVec = {}; 190 std::shared_ptr<CameraMetadata> ability = nullptr; 191 std::vector<StreamIntent> intents; 192 class StreamConsumer; 193 std::map<OHOS::Camera::StreamIntent, std::shared_ptr<StreamConsumer>> consumerMap_ = {}; 194 class TestBufferConsumerListener : public OHOS::IBufferConsumerListener { 195 public: TestBufferConsumerListener()196 TestBufferConsumerListener() {} ~TestBufferConsumerListener()197 ~TestBufferConsumerListener() {} OnBufferAvailable()198 void OnBufferAvailable() 199 { 200 hasAvailablebuffer = true; 201 } checkBufferAvailable()202 bool checkBufferAvailable() 203 { 204 if (hasAvailablebuffer) { 205 hasAvailablebuffer = false; 206 return true; 207 } 208 return false; 209 } 210 private: 211 bool hasAvailablebuffer = false; 212 }; 213 214 class StreamConsumer { 215 public: 216 void CalculateFps(int64_t timestamp, int32_t streamId); 217 void CreateProducerForBuffer(OHOS::sptr<OHOS::SurfaceBuffer> buffer, int64_t timestamp); 218 OHOS::sptr<OHOS::IBufferProducer> CreateProducer(std::function<void(void*, uint32_t)> callback); 219 OHOS::sptr<BufferProducerSequenceable> CreateProducerSeq(std::function<void(void*, uint32_t)> callback); TakeSnapshoe()220 void TakeSnapshoe() 221 { 222 shotCount_++; 223 } WaitSnapshotEnd()224 void WaitSnapshotEnd() 225 { 226 std::cout << "ready to wait" << std::endl; 227 std::unique_lock<std::mutex> l(l_); 228 cv_.wait(l, [this]() {return shotCount_ == 0; }); 229 } ~StreamConsumer()230 ~StreamConsumer() 231 { 232 running_ = false; 233 if (consumerThread_ != nullptr) { 234 consumerThread_->join(); 235 delete consumerThread_; 236 } 237 } 238 public: 239 std::atomic<uint64_t> shotCount_ = 0; 240 std::mutex l_; 241 std::condition_variable cv_; 242 bool running_ = true; 243 OHOS::sptr<OHOS::IConsumerSurface> consumer_ = nullptr; 244 std::thread* consumerThread_ = nullptr; 245 std::function<void(void*, uint32_t)> callback_ = nullptr; 246 bool isFirstCalculateFps_ = false; 247 int timestampCount_ = 0; 248 static int64_t g_timestamp[2]; 249 int64_t intervalTimestamp_ = 0; 250 const int64_t ONESECOND_OF_MICROSECOND_UNIT = 1000000000; 251 int64_t interval_ = ONESECOND_OF_MICROSECOND_UNIT; 252 }; 253 254 using ResultCallback = std::function<void (uint64_t, const std::shared_ptr<CameraMetadata>)>; 255 static ResultCallback resultCallback_; 256 257 class TestStreamOperatorCallback : public IStreamOperatorCallback { 258 public: 259 TestStreamOperatorCallback() = default; 260 virtual ~TestStreamOperatorCallback() = default; 261 int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamId) override; 262 int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo> &infos) override; 263 int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo> &infos) override; 264 int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t> &streamIds, uint64_t timestamp) override; 265 }; 266 267 class TestStreamOperatorCallbackV1_2 : public OHOS::HDI::Camera::V1_2::IStreamOperatorCallback { 268 public: 269 TestStreamOperatorCallbackV1_2() = default; 270 virtual ~TestStreamOperatorCallbackV1_2() = default; 271 int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamId) override; 272 int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo> &infos) override; 273 int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo> &infos) override; 274 int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t> &streamIds, uint64_t timestamp) override; 275 int32_t OnCaptureStarted_V1_2(int32_t captureId, 276 const std::vector<HDI::Camera::V1_2::CaptureStartedInfo> &infos) override; 277 }; 278 279 class DemoCameraDeviceCallback : public ICameraDeviceCallback { 280 public: 281 DemoCameraDeviceCallback() = default; 282 virtual ~DemoCameraDeviceCallback() = default; 283 284 int32_t OnError(ErrorType type, int32_t errorMsg) override; 285 int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t> &result) override; 286 }; 287 288 class TestCameraHostCallback : public ICameraHostCallback { 289 public: 290 TestCameraHostCallback() = default; 291 virtual ~TestCameraHostCallback() = default; 292 293 int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override; 294 int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override; 295 int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override; 296 }; 297 298 class TestCameraHostCallbackV1_2 : public OHOS::HDI::Camera::V1_2::ICameraHostCallback { 299 public: 300 TestCameraHostCallbackV1_2() = default; 301 virtual ~TestCameraHostCallbackV1_2() = default; 302 303 int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override; 304 int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override; 305 int32_t OnFlashlightStatus_V1_2(FlashlightStatus status) override; 306 int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override; 307 }; 308 309 class TestImageProcessCallback : public OHOS::HDI::Camera::V1_2::IImageProcessCallback { 310 public: 311 int32_t coutProcessDone_ = 0; 312 int32_t coutStatusChanged_ = 0; 313 int32_t countError_ = 0; 314 std::string curImageId_; 315 int32_t curErrorCode_ = 0; 316 OHOS::HDI::Camera::V1_2::ImageBufferInfo curImageBufferInfo_; 317 OHOS::HDI::Camera::V1_2::SessionStatus curStatus_; 318 TestImageProcessCallback() = default; 319 virtual ~TestImageProcessCallback() = default; 320 int32_t OnProcessDone(const std::string& imageId, 321 const OHOS::HDI::Camera::V1_2::ImageBufferInfo& buffer) override; 322 int32_t OnStatusChanged(OHOS::HDI::Camera::V1_2::SessionStatus status) override; 323 int32_t OnError(const std::string& imageId, OHOS::HDI::Camera::V1_2::ErrorCode errorCode) override; 324 }; 325 }; 326 } 327 #endif