1 /* 2 * Copyright (c) 2021-2025 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 DISTRIBUTED_CAMERA_HOST_H 17 #define DISTRIBUTED_CAMERA_HOST_H 18 19 #include "dcamera_base.h" 20 #include "dcamera_device.h" 21 22 #include "v1_0/icamera_host.h" 23 #include "v1_3/icamera_host.h" 24 #include "v1_0/icamera_device_callback.h" 25 #include "v1_0/icamera_host_callback.h" 26 #include "v1_2/icamera_host_callback.h" 27 #include "v1_1/dcamera_types.h" 28 #include "v1_1/id_camera_provider.h" 29 #include "v1_0/types.h" 30 #include "v1_2/types.h" 31 #include "constants.h" 32 #include "iremote_object.h" 33 34 namespace OHOS { 35 namespace DistributedHardware { 36 using namespace OHOS::HDI::DistributedCamera::V1_1; 37 using HDI::Camera::V1_3::ICameraDevice; 38 using HDI::Camera::V1_0::ICameraDeviceCallback; 39 using HDI::Camera::V1_0::FlashlightStatus; 40 using HDI::Camera::V1_0::CameraStatus; 41 using HDI::Camera::V1_3::ICameraHost; 42 using HDI::Camera::V1_1::PrelaunchConfig; 43 class DCameraHost : public OHOS::HDI::Camera::V1_3::ICameraHost { 44 const uint32_t ABILITYINFO_MAX_LENGTH = 50 * 1024 * 1024; 45 const uint32_t ID_MAX_SIZE = 2 * DEVID_MAX_LENGTH; 46 const size_t MAX_DCAMERAS_NUMBER = 32; 47 public: 48 DCameraHost() = default; 49 ~DCameraHost() override = default; 50 DCameraHost(const DCameraHost &other) = delete; 51 DCameraHost(DCameraHost &&other) = delete; 52 DCameraHost& operator=(const DCameraHost &other) = delete; 53 DCameraHost& operator=(DCameraHost &&other) = delete; 54 55 public: 56 static OHOS::sptr<DCameraHost> GetInstance(); 57 58 int32_t OpenCamera_V1_3(const std::string &cameraId, const sptr<ICameraDeviceCallback> &callbackObj, 59 sptr<ICameraDevice> &device) override; 60 int32_t OpenSecureCamera(const std::string &cameraId, const sptr<ICameraDeviceCallback> &callbackObj, 61 sptr<ICameraDevice> &device) override; 62 int32_t GetResourceCost(const std::string &cameraId, 63 OHOS::HDI::Camera::V1_3::CameraDeviceResourceCost &resourceCost) override; 64 65 int32_t OpenCamera_V1_2(const std::string &cameraId, const sptr<ICameraDeviceCallback> &callbackObj, 66 sptr<HDI::Camera::V1_2::ICameraDevice> &device) override; 67 int32_t NotifyDeviceStateChangeInfo(int notifyType, int deviceState) override; 68 int32_t SetCallback_V1_2(const sptr<HDI::Camera::V1_2::ICameraHostCallback> &callbackObj) override; 69 int32_t SetFlashlight_V1_2(float level) override; 70 int32_t PreCameraSwitch(const std::string &cameraId) override; 71 int32_t PrelaunchWithOpMode(const PrelaunchConfig &config, int32_t operationMode) override; 72 73 int32_t OpenCamera_V1_1(const std::string &cameraId, const sptr<ICameraDeviceCallback> &callbackObj, 74 sptr<HDI::Camera::V1_1::ICameraDevice> &device) override; 75 int32_t Prelaunch(const PrelaunchConfig &config) override; 76 77 int32_t SetCallback(const sptr<HDI::Camera::V1_0::ICameraHostCallback> &callbackObj) override; 78 int32_t GetCameraIds(std::vector<std::string> &cameraIds) override; 79 int32_t GetCameraAbility(const std::string &cameraId, std::vector<uint8_t> &cameraAbility) override; 80 int32_t OpenCamera(const std::string &cameraId, const sptr<ICameraDeviceCallback> &callbackObj, 81 sptr<HDI::Camera::V1_0::ICameraDevice> &device) override; 82 int32_t SetFlashlight(const std::string &cameraId, bool isEnable) override; 83 int32_t GetCameraAbilityFromDev(const std::string &cameraId, 84 std::shared_ptr<CameraAbility> &cameraAbility); 85 DCamRetCode AddDCameraDevice(const DHBase &dhBase, const std::string &sinkAbilityInfo, 86 const std::string &sourceCodecInfo, const sptr<IDCameraProviderCallback> &callback); 87 DCamRetCode AddDeviceParamCheck(const DHBase &dhBase, const std::string &sinkAbilityInfo, 88 const std::string &sourceCodecInfo, const sptr<IDCameraProviderCallback> &callback); 89 DCamRetCode RemoveDCameraDevice(const DHBase &dhBase); 90 OHOS::sptr<DCameraDevice> GetDCameraDeviceByDHBase(const DHBase &dhBase); 91 void NotifyDCameraStatus(const DHBase &dhBase, int32_t result); 92 DCamRetCode RegisterCameraHdfListener(const std::string &serviceName, 93 const sptr<IDCameraHdfCallback> &callbackObj); 94 DCamRetCode UnRegisterCameraHdfListener(const std::string &serviceName); 95 96 private: 97 bool IsCameraIdInvalid(const std::string &cameraId); 98 std::string GetCameraIdByDHBase(const DHBase &dhBase); 99 size_t GetCamDevNum(); 100 101 template<typename Callback, typename Device> 102 int32_t OpenCameraImpl(const std::string &cameraId, const Callback &callbackObj, Device &device); 103 int32_t AddClearRegisterRecipient(sptr<IRemoteObject> &remote, const DHBase &dhBase); 104 int32_t RemoveClearRegisterRecipient(sptr<IRemoteObject> &remote, const DHBase &dhBase); 105 106 private: 107 class AutoRelease { 108 public: AutoRelease()109 AutoRelease() {} ~AutoRelease()110 ~AutoRelease() 111 { 112 if (DCameraHost::instance_ != nullptr) { 113 DCameraHost::instance_ = nullptr; 114 } 115 } 116 }; 117 class ClearRegisterRecipient : public IRemoteObject::DeathRecipient { 118 public: ClearRegisterRecipient(const DHBase & dhBase)119 explicit ClearRegisterRecipient(const DHBase &dhBase) 120 : dhBase_(dhBase) 121 { 122 } IsNeedErase()123 bool IsNeedErase() 124 { 125 return needErase_; 126 } IsMatch(const DHBase & dhBase)127 bool IsMatch(const DHBase &dhBase) 128 { 129 return (dhBase.deviceId_ == dhBase_.deviceId_) && (dhBase.dhId_ == dhBase_.dhId_); 130 } 131 protected: 132 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 133 private: 134 DHBase dhBase_; 135 bool needErase_ = false; 136 }; 137 std::mutex clearRegisterRecipientsMtx_; 138 std::vector<sptr<ClearRegisterRecipient>> clearRegisterRecipients_; 139 class DCameraHostRecipient : public IRemoteObject::DeathRecipient { 140 public: 141 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 142 }; 143 sptr<DCameraHostRecipient> dCameraHostRecipient_ = sptr<DCameraHostRecipient>(new DCameraHostRecipient()); 144 static AutoRelease autoRelease_; 145 static OHOS::sptr<DCameraHost> instance_; 146 147 OHOS::sptr<HDI::Camera::V1_0::ICameraHostCallback> dCameraHostCallback_; 148 OHOS::sptr<HDI::Camera::V1_2::ICameraHostCallback> dCameraHostCallback_V1_2_; 149 std::map<std::string, OHOS::sptr<DCameraDevice>> dCameraDeviceMap_; 150 std::mutex deviceMapLock_; 151 std::map<std::string, sptr<IDCameraHdfCallback>> mapCameraHdfCallback_; 152 std::mutex hdfCallbackMapMtx_; 153 }; 154 } // namespace DistributedHardware 155 } // namespace OHOS 156 #endif // DISTRIBUTED_CAMERA_HOST_H