• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define EXPORT_API __attribute__((visibility("default")))
19 
20 #include <cstdint>
21 #include <functional>
22 #include <iostream>
23 #include <atomic>
24 #include <mutex>
25 
26 #include "camera_privacy.h"
27 #include "camera_sensor_plugin.h"
28 #include "v1_0/icamera_device_callback.h"
29 #include "camera_metadata_info.h"
30 #include "camera_util.h"
31 #include "hcamera_device_stub.h"
32 #include "hcamera_host_manager.h"
33 #include "v1_0/icamera_device.h"
34 #include "v1_1/icamera_device.h"
35 #include "v1_2/icamera_device.h"
36 #include "v1_3/icamera_device.h"
37 #include "v1_0/icamera_host.h"
38 
39 namespace OHOS {
40 namespace CameraStandard {
41 constexpr int32_t HDI_STREAM_ID_INIT = 1;
42 static std::mutex dropDetectionMutex_;
43 using OHOS::HDI::Camera::V1_0::CaptureEndedInfo;
44 using OHOS::HDI::Camera::V1_0::CaptureErrorInfo;
45 using OHOS::HDI::Camera::V1_0::ICameraDeviceCallback;
46 using OHOS::HDI::Camera::V1_3::IStreamOperatorCallback;
47 class EXPORT_API HCameraDevice
48     : public HCameraDeviceStub, public ICameraDeviceCallback, public IStreamOperatorCallback {
49 public:
50     explicit HCameraDevice(
51         sptr<HCameraHostManager>& cameraHostManager, std::string cameraID, const uint32_t callingTokenId);
52     ~HCameraDevice();
53 
54     int32_t Open() override;
55     int32_t OpenSecureCamera(uint64_t* secureSeqId) override;
56     int32_t Close() override;
57     int32_t Release() override;
58     int32_t UpdateSetting(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings) override;
59     int32_t SetUsedAsPosition(uint8_t value) override;
60     int32_t UpdateSettingOnce(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings);
61     int32_t GetStatus(std::shared_ptr<OHOS::Camera::CameraMetadata> &metaIn,
62             std::shared_ptr<OHOS::Camera::CameraMetadata> &metaOut) override;
63     int32_t GetEnabledResults(std::vector<int32_t>& results) override;
64     int32_t EnableResult(std::vector<int32_t>& results) override;
65     int32_t DisableResult(std::vector<int32_t>& results) override;
66     int32_t ReleaseStreams(std::vector<int32_t>& releaseStreamIds);
67     sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> GetStreamOperator();
68     int32_t SetCallback(sptr<ICameraDeviceServiceCallback>& callback) override;
69     int32_t OnError(OHOS::HDI::Camera::V1_0::ErrorType type, int32_t errorCode) override;
70     int32_t OnResult(uint64_t timestamp, const std::vector<uint8_t>& result) override;
71     int32_t OnResult(int32_t streamId, const std::vector<uint8_t>& result) override;
72     std::shared_ptr<OHOS::Camera::CameraMetadata> GetDeviceAbility();
73     std::shared_ptr<OHOS::Camera::CameraMetadata> CloneCachedSettings();
74     std::string GetCameraId();
75     int32_t GetCameraType();
76     bool IsOpenedCameraDevice();
77     int32_t GetCallerToken();
78     int32_t CreateAndCommitStreams(std::vector<HDI::Camera::V1_1::StreamInfo_V1_1>& streamInfos,
79         std::shared_ptr<OHOS::Camera::CameraMetadata>& deviceSettings, int32_t operationMode);
80     int32_t UpdateStreams(std::vector<StreamInfo_V1_1>& streamInfos);
81 
82     int32_t OperatePermissionCheck(uint32_t interfaceCode) override;
83 
84     int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds) override;
85     int32_t OnCaptureStarted_V1_2(
86         int32_t captureId, const std::vector<OHOS::HDI::Camera::V1_2::CaptureStartedInfo>& infos) override;
87     int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos) override;
88     int32_t OnCaptureEndedExt(
89         int32_t captureId, const std::vector<OHOS::HDI::Camera::V1_3::CaptureEndedInfoExt>& infos) override;
90     int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos) override;
91     int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp) override;
92     int32_t OnFrameShutterEnd(int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp) override;
93     int32_t OnCaptureReady(int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp) override;
94     int32_t ResetDeviceSettings();
95     int32_t DispatchDefaultSettingToHdi();
96     void SetDeviceMuteMode(bool muteMode);
97     uint8_t GetUsedAsPosition();
98     bool GetDeviceMuteMode();
99     void EnableMovingPhoto(bool isMovingPhotoEnabled);
100     static void DeviceEjectCallBack();
101     static void DeviceFaultCallBack();
102 
SetStreamOperatorCallback(wptr<IStreamOperatorCallback> operatorCallback)103     inline void SetStreamOperatorCallback(wptr<IStreamOperatorCallback> operatorCallback)
104     {
105         std::lock_guard<std::mutex> lock(proxyStreamOperatorCallbackMutex_);
106         proxyStreamOperatorCallback_ = operatorCallback;
107     }
108 
GetStreamOperatorCallback()109     inline sptr<IStreamOperatorCallback> GetStreamOperatorCallback()
110     {
111         std::lock_guard<std::mutex> lock(proxyStreamOperatorCallbackMutex_);
112         return proxyStreamOperatorCallback_.promote();
113     }
114 
SetCameraPrivacy(sptr<CameraPrivacy> cameraPrivacy)115     inline void SetCameraPrivacy(sptr<CameraPrivacy> cameraPrivacy)
116     {
117         std::lock_guard<std::mutex> lock(cameraPrivacyMutex_);
118         cameraPrivacy_ = cameraPrivacy;
119     }
120 
GetCameraPrivacy()121     inline sptr<CameraPrivacy> GetCameraPrivacy()
122     {
123         std::lock_guard<std::mutex> lock(cameraPrivacyMutex_);
124         return cameraPrivacy_;
125     }
126 
GenerateHdiStreamId()127     inline int32_t GenerateHdiStreamId()
128     {
129         return hdiStreamIdGenerator_.fetch_add(1);
130     }
131 
ResetHdiStreamId()132     inline void ResetHdiStreamId()
133     {
134         hdiStreamIdGenerator_ = HDI_STREAM_ID_INIT;
135     }
136 
137     void NotifyCameraSessionStatus(bool running);
138 
139     void RemoveResourceWhenHostDied();
140 
141     int64_t GetSecureCameraSeq(uint64_t* secureSeqId);
142 
143     bool CheckMovingPhotoSupported(int32_t mode);
144 
145     void NotifyCameraStatus(int32_t type);
146 
147     int32_t CloseDevice();
148 
149     void SetMovingPhotoStartTimeCallback(std::function<void(int64_t, int64_t)> callback);
150 
151     void SetMovingPhotoEndTimeCallback(std::function<void(int64_t, int64_t)> callback);
152 
153 private:
154     class FoldScreenListener;
155     static const std::vector<std::tuple<uint32_t, std::string, std::string>> reportTagInfos_;
156 
157     std::mutex opMutex_; // Lock the operations updateSettings_, streamOperator_, and hdiCameraDevice_.
158     std::shared_ptr<OHOS::Camera::CameraMetadata> updateSettings_;
159     sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator_;
160     sptr<OHOS::HDI::Camera::V1_0::ICameraDevice> hdiCameraDevice_;
161     std::shared_ptr<OHOS::Camera::CameraMetadata> cachedSettings_;
162 
163     sptr<HCameraHostManager> cameraHostManager_;
164     std::string cameraID_;
165     std::atomic<bool> isOpenedCameraDevice_;
166     std::mutex deviceSvcCbMutex_;
167     std::mutex cachedSettingsMutex_;
168     static std::mutex g_deviceOpenCloseMutex_;
169     sptr<ICameraDeviceServiceCallback> deviceSvcCallback_;
170     std::map<int32_t, wptr<ICameraServiceCallback>> statusSvcCallbacks_;
171 
172     uint32_t callerToken_;
173     std::mutex cameraPrivacyMutex_;
174     sptr<CameraPrivacy> cameraPrivacy_;
175     std::mutex proxyStreamOperatorCallbackMutex_;
176     wptr<IStreamOperatorCallback> proxyStreamOperatorCallback_;
177 
178     std::mutex deviceAbilityMutex_;
179     std::shared_ptr<OHOS::Camera::CameraMetadata> deviceAbility_;
180 
181     std::mutex deviceOpenLifeCycleMutex_;
182     std::shared_ptr<OHOS::Camera::CameraMetadata> deviceOpenLifeCycleSettings_;
183 
184     std::string clientName_;
185     int clientUserId_;
186     uint8_t usedAsPosition_ = OHOS_CAMERA_POSITION_OTHER;
187     std::mutex unPrepareZoomMutex_;
188     uint32_t zoomTimerId_;
189     std::atomic<bool> inPrepareZoom_;
190     std::atomic<bool> deviceMuteMode_;
191     bool isHasOpenSecure = false;
192     uint64_t mSecureCameraSeqId = 0L;
193     int32_t lastDeviceProtectionStatus_ = -1;
194     std::mutex deviceProtectionStatusMutex_;
195     int64_t lastDeviceEjectTime_ = 0;
196     std::atomic<int> deviceEjectTimes_ = 1;
197 
198     std::atomic<int32_t> hdiStreamIdGenerator_ = HDI_STREAM_ID_INIT;
199     void UpdateDeviceOpenLifeCycleSettings(std::shared_ptr<OHOS::Camera::CameraMetadata> changedSettings);
200     void ResetDeviceOpenLifeCycleSettings();
201 
202     sptr<ICameraDeviceServiceCallback> GetDeviceServiceCallback();
203     void ResetCachedSettings();
204     int32_t InitStreamOperator();
205     void ReportMetadataDebugLog(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings);
206     void RegisterFoldStatusListener();
207     void UnRegisterFoldStatusListener();
208     void CheckOnResultData(std::shared_ptr<OHOS::Camera::CameraMetadata> cameraResult);
209     int32_t CreateStreams(std::vector<HDI::Camera::V1_1::StreamInfo_V1_1>& streamInfos);
210     int32_t CommitStreams(std::shared_ptr<OHOS::Camera::CameraMetadata>& deviceSettings, int32_t operationMode);
211     bool CanOpenCamera();
212     void ResetZoomTimer();
213     void CheckZoomChange(const std::shared_ptr<OHOS::Camera::CameraMetadata>& settings);
214     void UnPrepareZoom();
215     int32_t OpenDevice(bool isEnableSecCam = false);
216     void HandleFoldableDevice();
217     int32_t CheckPermissionBeforeOpenDevice();
218     bool HandlePrivacyBeforeOpenDevice();
219     void HandlePrivacyWhenOpenDeviceFail();
220     void HandlePrivacyAfterCloseDevice();
221     void DebugLogForSmoothZoom(const std::shared_ptr<OHOS::Camera::CameraMetadata> &settings, uint32_t tag);
222     void DebugLogForAfRegions(const std::shared_ptr<OHOS::Camera::CameraMetadata> &settings, uint32_t tag);
223     void DebugLogForAeRegions(const std::shared_ptr<OHOS::Camera::CameraMetadata> &settings, uint32_t tag);
224     void DebugLogTag(const std::shared_ptr<OHOS::Camera::CameraMetadata> &settings,
225                      uint32_t tag, std::string tagName, std::string dfxUbStr);
226     void CreateMuteSetting(std::shared_ptr<OHOS::Camera::CameraMetadata>& settings);
227     int32_t UpdateDeviceSetting();
228 #ifdef MEMMGR_OVERRID
229     int32_t RequireMemory(const std::string& reason);
230 #endif
231     void GetMovingPhotoStartAndEndTime(std::shared_ptr<OHOS::Camera::CameraMetadata> cameraResult);
232     void ReportDeviceProtectionStatus(const std::shared_ptr<OHOS::Camera::CameraMetadata> &metadata);
233     bool CanReportDeviceProtectionStatus(int32_t status);
234     bool ShowDeviceProtectionDialog(DeviceProtectionStatus status);
235     std::string BuildDeviceProtectionDialogCommand(DeviceProtectionStatus status);
236     bool isMovingPhotoEnabled_ = false;
237     std::mutex movingPhotoStartTimeCallbackLock_;
238     std::mutex movingPhotoEndTimeCallbackLock_;
239     std::function<void(int32_t, int64_t)> movingPhotoStartTimeCallback_;
240     std::function<void(int32_t, int64_t)> movingPhotoEndTimeCallback_;
241 };
242 } // namespace CameraStandard
243 } // namespace OHOS
244 #endif // OHOS_CAMERA_H_CAMERA_DEVICE_H
245