1 /* 2 * Copyright (c) 2021-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 16 #ifndef OHOS_CAMERA_H_CAMERA_DEVICE_H 17 #define OHOS_CAMERA_H_CAMERA_DEVICE_H 18 #include <vector> 19 #include "refbase.h" 20 #define EXPORT_API __attribute__((visibility("default"))) 21 22 #include <cstdint> 23 #include <functional> 24 #include <iostream> 25 #include <atomic> 26 #include <mutex> 27 #include <set> 28 29 #include "camera_privacy.h" 30 #include "camera_sensor_plugin.h" 31 #include "v1_0/icamera_device_callback.h" 32 #include "camera_metadata_info.h" 33 #include "camera_util.h" 34 #include "hcamera_device_stub.h" 35 #include "hcamera_host_manager.h" 36 #include "v1_0/icamera_device.h" 37 #include "v1_1/icamera_device.h" 38 #include "v1_2/icamera_device.h" 39 #include "v1_3/icamera_device.h" 40 #include "v1_0/icamera_host.h" 41 #include "dfx/camera_report_uitls.h" 42 43 namespace OHOS { 44 namespace CameraStandard { 45 constexpr int32_t HDI_STREAM_ID_INIT = 1; 46 using OHOS::HDI::Camera::V1_0::CaptureEndedInfo; 47 using OHOS::HDI::Camera::V1_0::CaptureErrorInfo; 48 using OHOS::HDI::Camera::V1_0::ICameraDeviceCallback; 49 using OHOS::HDI::Camera::V1_3::IStreamOperatorCallback; 50 class IHCameraCloseListener : public virtual RefBase { 51 public: 52 virtual void BeforeDeviceClose() = 0; 53 }; 54 55 class EXPORT_API HCameraDevice 56 : public HCameraDeviceStub, public ICameraDeviceCallback { 57 public: 58 explicit HCameraDevice( 59 sptr<HCameraHostManager>& cameraHostManager, std::string cameraID, const uint32_t callingTokenId); 60 ~HCameraDevice(); 61 62 int32_t Open() override; 63 int32_t OpenSecureCamera(uint64_t* secureSeqId) override; 64 int32_t Open(int32_t concurrentType) override; 65 int32_t Close() override; 66 int32_t closeDelayed() override; 67 int32_t Release() override; 68 int32_t UpdateSetting(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings) override; 69 int32_t SetUsedAsPosition(uint8_t value) override; 70 int32_t UpdateSettingOnce(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings); 71 int32_t GetStatus(std::shared_ptr<OHOS::Camera::CameraMetadata> &metaIn, 72 std::shared_ptr<OHOS::Camera::CameraMetadata> &metaOut) override; 73 int32_t GetEnabledResults(std::vector<int32_t>& results) override; 74 int32_t EnableResult(std::vector<int32_t>& results) override; 75 int32_t DisableResult(std::vector<int32_t>& results) override; 76 int32_t SetDeviceRetryTime() override; 77 int32_t ReleaseStreams(std::vector<int32_t>& releaseStreamIds); 78 int32_t SetCallback(sptr<ICameraDeviceServiceCallback>& callback) override; 79 int32_t UnSetCallback() override; 80 int32_t OnError(OHOS::HDI::Camera::V1_0::ErrorType type, int32_t errorCode) override; 81 int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t>& result) override; 82 // int32_t OnResult(int32_t streamId, const std::vector<uint8_t>& result) override; 83 std::shared_ptr<OHOS::Camera::CameraMetadata> GetDeviceAbility(); 84 std::shared_ptr<OHOS::Camera::CameraMetadata> CloneCachedSettings(); 85 std::string GetCameraId(); 86 int32_t GetCameraType(); 87 bool IsOpenedCameraDevice(); 88 int32_t GetCallerToken(); 89 90 int32_t OperatePermissionCheck(uint32_t interfaceCode) override; 91 92 int32_t ResetDeviceSettings(); 93 int32_t DispatchDefaultSettingToHdi(); 94 void SetDeviceMuteMode(bool muteMode); 95 uint8_t GetUsedAsPosition(); 96 bool GetDeviceMuteMode(); 97 void EnableMovingPhoto(bool isMovingPhotoEnabled); 98 static void DeviceEjectCallBack(); 99 static void DeviceFaultCallBack(); 100 SetStreamOperatorCallback(wptr<IStreamOperatorCallback> operatorCallback)101 inline void SetStreamOperatorCallback(wptr<IStreamOperatorCallback> operatorCallback) 102 { 103 std::lock_guard<std::mutex> lock(proxyStreamOperatorCallbackMutex_); 104 proxyStreamOperatorCallback_ = operatorCallback; 105 } 106 GetStreamOperatorCallback()107 inline sptr<IStreamOperatorCallback> GetStreamOperatorCallback() 108 { 109 std::lock_guard<std::mutex> lock(proxyStreamOperatorCallbackMutex_); 110 return proxyStreamOperatorCallback_.promote(); 111 } 112 SetCameraPrivacy(sptr<CameraPrivacy> cameraPrivacy)113 inline void SetCameraPrivacy(sptr<CameraPrivacy> cameraPrivacy) 114 { 115 std::lock_guard<std::mutex> lock(cameraPrivacyMutex_); 116 cameraPrivacy_ = cameraPrivacy; 117 } 118 GetCameraPrivacy()119 inline sptr<CameraPrivacy> GetCameraPrivacy() 120 { 121 std::lock_guard<std::mutex> lock(cameraPrivacyMutex_); 122 return cameraPrivacy_; 123 } 124 125 void NotifyCameraSessionStatus(bool running); 126 127 void RemoveResourceWhenHostDied(); 128 129 int64_t GetSecureCameraSeq(uint64_t* secureSeqId); 130 131 bool CheckMovingPhotoSupported(int32_t mode); 132 133 void NotifyCameraStatus(int32_t state, int32_t msg = 0); 134 135 bool GetCameraResourceCost(int32_t &cost, std::set<std::string> &conflicting); 136 137 int32_t CloseDevice(); 138 139 int32_t closeDelayedDevice(); 140 141 void SetMovingPhotoStartTimeCallback(std::function<void(int64_t, int64_t)> callback); 142 143 void SetMovingPhotoEndTimeCallback(std::function<void(int64_t, int64_t)> callback); 144 SetCameraConcurrentType(int32_t cameraConcurrentTypenum)145 inline void SetCameraConcurrentType(int32_t cameraConcurrentTypenum) 146 { 147 cameraConcurrentType_ = cameraConcurrentTypenum; 148 } 149 150 std::vector<std::vector<std::int32_t>> GetConcurrentDevicesTable(); 151 GetTargetConcurrencyType()152 inline int32_t GetTargetConcurrencyType() 153 { 154 return cameraConcurrentType_; 155 } 156 int32_t GetStreamOperator(const sptr<IStreamOperatorCallback> &callbackObj, 157 sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> &streamOperator); 158 SetCameraCloseListener(wptr<IHCameraCloseListener> listener)159 inline void SetCameraCloseListener(wptr<IHCameraCloseListener> listener) 160 { 161 std::lock_guard<std::mutex> lock(cameraCloseListenerMutex_); 162 cameraCloseListenerVec_.push_back(listener); 163 } 164 IsDeviceOpenedByConcurrent()165 inline bool IsDeviceOpenedByConcurrent() 166 { 167 return isDeviceOpenedByConcurrent_; 168 } 169 EnableDeviceOpenedByConcurrent(bool enable)170 inline void EnableDeviceOpenedByConcurrent(bool enable) 171 { 172 isDeviceOpenedByConcurrent_ = enable; 173 } 174 175 private: 176 class FoldScreenListener; 177 static const std::vector<std::tuple<uint32_t, std::string, DFX_UB_NAME>> reportTagInfos_; 178 179 std::mutex opMutex_; // Lock the operations updateSettings_, streamOperator_, and hdiCameraDevice_. 180 std::shared_ptr<OHOS::Camera::CameraMetadata> updateSettings_; 181 sptr<OHOS::HDI::Camera::V1_0::ICameraDevice> hdiCameraDevice_; 182 std::shared_ptr<OHOS::Camera::CameraMetadata> cachedSettings_; 183 int32_t cameraConcurrentType_ = 0; 184 std::atomic<bool> isDeviceOpenedByConcurrent_ = false; 185 186 sptr<HCameraHostManager> cameraHostManager_; 187 std::string cameraID_; 188 std::atomic<bool> isOpenedCameraDevice_; 189 std::mutex deviceSvcCbMutex_; 190 std::mutex cachedSettingsMutex_; 191 static std::mutex g_deviceOpenCloseMutex_; 192 sptr<ICameraDeviceServiceCallback> deviceSvcCallback_; 193 std::map<int32_t, wptr<ICameraServiceCallback>> statusSvcCallbacks_; 194 195 uint32_t callerToken_; 196 std::mutex cameraPrivacyMutex_; 197 sptr<CameraPrivacy> cameraPrivacy_; 198 int32_t cameraPid_; 199 200 std::mutex proxyStreamOperatorCallbackMutex_; 201 wptr<IStreamOperatorCallback> proxyStreamOperatorCallback_; 202 203 std::mutex deviceAbilityMutex_; 204 std::shared_ptr<OHOS::Camera::CameraMetadata> deviceAbility_; 205 206 std::mutex deviceOpenLifeCycleMutex_; 207 std::shared_ptr<OHOS::Camera::CameraMetadata> deviceOpenLifeCycleSettings_; 208 209 std::string clientName_; 210 int clientUserId_; 211 uint8_t usedAsPosition_ = OHOS_CAMERA_POSITION_OTHER; 212 std::mutex unPrepareZoomMutex_; 213 uint32_t zoomTimerId_; 214 std::atomic<bool> inPrepareZoom_ {false}; 215 std::atomic<bool> deviceMuteMode_ {false}; 216 bool isHasOpenSecure = false; 217 uint64_t mSecureCameraSeqId = 0L; 218 int32_t lastDeviceProtectionStatus_ = -1; 219 std::mutex deviceProtectionStatusMutex_; 220 int64_t lastDeviceEjectTime_ = 0; 221 std::atomic<uint32_t> deviceEjectTimes_ = 1; 222 223 void UpdateDeviceOpenLifeCycleSettings(std::shared_ptr<OHOS::Camera::CameraMetadata> changedSettings); 224 void ResetDeviceOpenLifeCycleSettings(); 225 226 sptr<ICameraDeviceServiceCallback> GetDeviceServiceCallback(); 227 void ResetCachedSettings(); 228 void ReportMetadataDebugLog(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings); 229 void RegisterFoldStatusListener(); 230 void UnregisterFoldStatusListener(); 231 void CheckOnResultData(std::shared_ptr<OHOS::Camera::CameraMetadata> cameraResult); 232 bool CanOpenCamera(); 233 void ResetZoomTimer(); 234 void CheckZoomChange(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings); 235 void UnPrepareZoom(); 236 int32_t OpenDevice(bool isEnableSecCam = false); 237 void ConfigQosParam(const char *bundleName, int32_t qosLevel, 238 std::unordered_map<std::string, std::string> &qosParamMap); 239 void HandleFoldableDevice(); 240 int32_t CheckPermissionBeforeOpenDevice(); 241 bool HandlePrivacyBeforeOpenDevice(); 242 void HandlePrivacyWhenOpenDeviceFail(); 243 void HandlePrivacyAfterCloseDevice(); 244 void DebugLogForSmoothZoom(const std::shared_ptr<OHOS::Camera::CameraMetadata> &settings, uint32_t tag); 245 void DebugLogForAfRegions(const std::shared_ptr<OHOS::Camera::CameraMetadata> &settings, uint32_t tag); 246 void DebugLogForAeRegions(const std::shared_ptr<OHOS::Camera::CameraMetadata> &settings, uint32_t tag); 247 void DebugLogTag(const std::shared_ptr<OHOS::Camera::CameraMetadata> &settings, 248 uint32_t tag, std::string tagName, DFX_UB_NAME dfxUbStr); 249 void CreateMuteSetting(std::shared_ptr<OHOS::Camera::CameraMetadata>& settings); 250 int32_t UpdateDeviceSetting(); 251 void ReleaseSessionBeforeCloseDevice(); 252 #ifdef MEMMGR_OVERRID 253 int32_t RequireMemory(const std::string& reason); 254 #endif 255 void GetMovingPhotoStartAndEndTime(std::shared_ptr<OHOS::Camera::CameraMetadata> cameraResult); 256 bool isMovingPhotoEnabled_ = false; 257 std::mutex movingPhotoStartTimeCallbackLock_; 258 std::mutex movingPhotoEndTimeCallbackLock_; 259 std::function<void(int32_t, int64_t)> movingPhotoStartTimeCallback_; 260 std::function<void(int32_t, int64_t)> movingPhotoEndTimeCallback_; 261 void ReportDeviceProtectionStatus(const std::shared_ptr<OHOS::Camera::CameraMetadata> &metadata); 262 bool CanReportDeviceProtectionStatus(int32_t status); 263 bool ShowDeviceProtectionDialog(DeviceProtectionStatus status); 264 std::string BuildDeviceProtectionDialogCommand(DeviceProtectionStatus status); 265 void RegisterSensorCallback(); 266 void UnRegisterSensorCallback(); 267 static void DropDetectionDataCallbackImpl(const OHOS::Rosen::MotionSensorEvent &motionData); 268 std::mutex sensorLock_; 269 std::mutex cameraCloseListenerMutex_; 270 std::vector<wptr<IHCameraCloseListener>> cameraCloseListenerVec_; 271 }; 272 } // namespace CameraStandard 273 } // namespace OHOS 274 #endif // OHOS_CAMERA_H_CAMERA_DEVICE_H 275