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