1 /* 2 * Copyright (c) 2022-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 #ifndef DISTRIBUTED_DCAMERA_HDF_DEMO_H 16 #define DISTRIBUTED_DCAMERA_HDF_DEMO_H 17 18 #include <vector> 19 #include <map> 20 #include <iostream> 21 #include <hdf_log.h> 22 #include <surface.h> 23 #include <sys/time.h> 24 #include <ctime> 25 #include <fcntl.h> 26 #include "constants.h" 27 #include "camera_metadata_operator.h" 28 #include "v1_0/display_composer_type.h" 29 #include "v1_0/icamera_device.h" 30 #include "v1_0/icamera_host.h" 31 #include "v1_0/ioffline_stream_operator.h" 32 #include "v1_0/istream_operator.h" 33 #include "v1_0/types.h" 34 #include "v1_0/istream_operator_callback.h" 35 #include "metadata_utils.h" 36 #include "stream_customer.h" 37 #include "securec.h" 38 #include "if_system_ability_manager.h" 39 #include "iservice_registry.h" 40 41 namespace OHOS { 42 namespace DistributedHardware { 43 using namespace OHOS::Camera; 44 using namespace OHOS::HDI::Camera::V1_0; 45 using namespace OHOS::HDI::Display::Composer::V1_0; 46 using CameraAbility = OHOS::Camera::CameraMetadata; 47 using CameraSetting = OHOS::Camera::CameraMetadata; 48 #define CAMERA_CAPTURE_ENCODE_TYPE OHOS::HDI::Camera::V1_0::ENCODE_TYPE_JPEG 49 #define CAMERA_VIDEO_ENCODE_TYPE OHOS::HDI::Camera::V1_0::ENCODE_TYPE_H264 50 51 #ifdef DCAMERA_DRIVER_YUV 52 #define CAMERA_FORMAT PIXEL_FMT_YCRCB_420_SP 53 #else 54 #define CAMERA_FORMAT PIXEL_FMT_RGBA_8888 55 #endif 56 57 typedef enum { 58 STREAM_ID_PREVIEW = 1001, 59 STREAM_ID_CAPTURE, 60 STREAM_ID_VIDEO, 61 CAPTURE_ID_PREVIEW = 2001, 62 CAPTURE_ID_CAPTURE, 63 CAPTURE_ID_VIDEO, 64 } DemoActionID; 65 66 typedef enum { 67 OHOS_CAMERA_AWB_MODE_OFF, 68 OHOS_CAMERA_AWB_MODE_AUTO, 69 OHOS_CAMERA_AWB_MODE_INCANDESCENT, 70 OHOS_CAMERA_AWB_MODE_FLUORESCENT, 71 OHOS_CAMERA_AWB_MODE_WARM_FLUORESCENT, 72 OHOS_CAMERA_AWB_MODE_DAYLIGHT, 73 OHOS_CAMERA_AWB_MODE_CLOUDY_DAYLIGHT, 74 OHOS_CAMERA_AWB_MODE_TWILIGHT, 75 OHOS_CAMERA_AWB_MODE_SHADE, 76 } CameraAwbMode; 77 78 class DemoCameraHostCallback; 79 class DcameraHdfDemo { 80 public: 81 DcameraHdfDemo(); 82 ~DcameraHdfDemo(); 83 84 RetCode InitCameraDevice(); 85 void ReleaseCameraDevice(); 86 RetCode InitSensors(); 87 88 RetCode StartPreviewStream(); 89 RetCode StartCaptureStream(); 90 RetCode StartVideoStream(); 91 RetCode StartDualStreams(const int streamIdSecond); 92 RetCode CreateStream(); 93 RetCode ReleaseAllStream(); 94 95 RetCode CaptureOnDualStreams(const int streamIdSecond); 96 RetCode CaptureON(const int streamId, const int captureId, CaptureMode mode); 97 void CaptureSet(std::vector<uint8_t> &setNum); 98 RetCode CaptureOff(const int captureId, const CaptureMode mode); 99 100 RetCode SetAwbMode(const int mode) const; 101 RetCode SetAeExpo(); 102 RetCode SetMetadata(); 103 RetCode SetMetadataAe(uint8_t mode); 104 RetCode SetMetadataMeter(uint8_t mode); 105 RetCode SetMetadataFlash(uint8_t mode); 106 RetCode SetMetadataMirror(uint8_t mode); 107 RetCode SetMetadataJpeg(uint8_t mode); 108 RetCode SetMetadataAf(uint8_t mode); 109 RetCode SetMetadataFace(uint8_t mode); 110 void SetEnableResult(); 111 void FlashlightOnOff(bool onOff); 112 113 RetCode StreamOffline(const int streamId); 114 115 void QuitDemo(); 116 117 private: 118 void SetStreamInfo(StreamInfo& streamInfo, 119 const std::shared_ptr<StreamCustomer>& streamCustomer, 120 const int streamId, const StreamIntent intent); 121 void GetStreamOpt(); 122 123 RetCode CreateStreams(const int streamIdSecond, StreamIntent intent); 124 125 void StoreImage(const char *bufStart, const uint32_t size) const; 126 void StoreVideo(const char *bufStart, const uint32_t size) const; 127 void OpenVideoFile(); 128 129 RetCode GetFaceDetectMode(std::shared_ptr<CameraAbility> &ability); 130 RetCode GetFocalLength(std::shared_ptr<CameraAbility> &ability); 131 RetCode GetAvailableFocusModes(std::shared_ptr<CameraAbility> &ability); 132 RetCode GetAvailableExposureModes(std::shared_ptr<CameraAbility> &ability); 133 RetCode GetExposureCompensationRange(std::shared_ptr<CameraAbility> &ability); 134 RetCode GetExposureCompensationSteps(std::shared_ptr<CameraAbility> &ability); 135 RetCode GetAvailableMeterModes(std::shared_ptr<CameraAbility> &ability); 136 RetCode GetAvailableFlashModes(std::shared_ptr<CameraAbility> &ability); 137 RetCode GetMirrorSupported(std::shared_ptr<CameraAbility> &ability); 138 RetCode GetStreamBasicConfigurations(std::shared_ptr<CameraAbility> &ability); 139 RetCode GetFpsRange(std::shared_ptr<CameraAbility> &ability); 140 RetCode GetCameraPosition(std::shared_ptr<CameraAbility> &ability); 141 RetCode GetCameraType(std::shared_ptr<CameraAbility> &ability); 142 RetCode GetCameraConnectionType(std::shared_ptr<CameraAbility> &ability); 143 RetCode GetFaceDetectMaxNum(std::shared_ptr<CameraAbility> &ability); 144 RetCode CreateStreamInfo(const int streamId, std::shared_ptr<StreamCustomer> &streamCustomer, 145 StreamIntent intent); 146 147 int aeStatus_ = 1; 148 int videoFd_ = -1; 149 unsigned int isPreviewOn_ = 0; 150 unsigned int isCaptureOn_ = 0; 151 unsigned int isVideoOn_ = 0; 152 153 uint8_t captureQuality_ = 0; 154 int32_t captureOrientation_ = 0; 155 uint8_t mirrorSwitch_ = 0; 156 std::vector<double> gps_; 157 CaptureInfo captureInfo_; 158 159 std::shared_ptr<StreamCustomer> streamCustomerPreview_ = nullptr; 160 std::shared_ptr<StreamCustomer> streamCustomerCapture_ = nullptr; 161 std::shared_ptr<StreamCustomer> streamCustomerVideo_ = nullptr; 162 std::shared_ptr<CameraAbility> ability_ = nullptr; 163 std::shared_ptr<CameraSetting> captureSetting_ = nullptr; 164 std::mutex metaDatalock_; 165 std::vector<uint8_t> cameraAbility_; 166 std::vector<StreamInfo> streamInfos_; 167 std::vector<int> streamIds_; 168 169 OHOS::sptr<DemoCameraHostCallback> hostCallback_ = nullptr; 170 OHOS::sptr<IStreamOperator> streamOperator_ = nullptr; 171 OHOS::sptr<ICameraHost> demoCameraHost_ = nullptr; 172 OHOS::sptr<ICameraDevice> demoCameraDevice_ = nullptr; 173 174 std::vector<std::string> cameraIds_ = {}; 175 friend class StreamCustomer; 176 }; 177 178 class DemoCameraDeviceCallback : public ICameraDeviceCallback { 179 public: 180 DemoCameraDeviceCallback() = default; 181 virtual ~DemoCameraDeviceCallback() = default; 182 int32_t OnError(ErrorType type, int32_t errorCode) override; 183 int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t>& result) override; 184 }; 185 186 class DemoCameraHostCallback : public ICameraHostCallback { 187 public: 188 DemoCameraHostCallback() = default; 189 virtual ~DemoCameraHostCallback() = default; 190 191 public: 192 int32_t OnCameraStatus(const std::string& cameraId, CameraStatus status) override; 193 194 int32_t OnFlashlightStatus(const std::string& cameraId, FlashlightStatus status) override; 195 196 int32_t OnCameraEvent(const std::string& cameraId, CameraEvent event) override; 197 }; 198 199 class DemoStreamOperatorCallback : public IStreamOperatorCallback { 200 public: 201 DemoStreamOperatorCallback() = default; 202 virtual ~DemoStreamOperatorCallback() = default; 203 204 public: 205 int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds) override; 206 int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos) override; 207 int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos) override; 208 int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp) override; 209 }; 210 void PreviewOff(const std::shared_ptr<DcameraHdfDemo>& mainDemo); 211 RetCode PreviewOn(int mode, const std::shared_ptr<DcameraHdfDemo>& mainDemo); 212 RetCode FlashLightTest(const std::shared_ptr<DcameraHdfDemo>& mainDemo); 213 RetCode CaptureTest(const std::shared_ptr<DcameraHdfDemo>& mainDemo); 214 RetCode VideoTest(const std::shared_ptr<DcameraHdfDemo>& mainDemo); 215 RetCode OfflineTest(const std::shared_ptr<DcameraHdfDemo>& mainDemo); 216 } 217 } // namespace OHOS::DistributedHardware 218 #endif // DISTRIBUTED_DCAMERA_HDF_DEMO_H 219