• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OHOS_CAMERA_H_CAPTURE_SESSION_H
17 #define OHOS_CAMERA_H_CAPTURE_SESSION_H
18 #include "camera_datashare_helper.h"
19 #include "icapture_session_callback.h"
20 #include <stdint.h>
21 #define EXPORT_API __attribute__((visibility("default")))
22 
23 #include <atomic>
24 #include <cstdint>
25 #include <functional>
26 #include <iostream>
27 #include <list>
28 #include <memory>
29 #include <mutex>
30 #include <refbase.h>
31 #include <unordered_map>
32 #include <unordered_set>
33 #include "camera_rotate_strategy_parser.h"
34 #include "hcamera_device.h"
35 #include "capture_session_stub.h"
36 
37 #include "hstream_repeat.h"
38 #include "hstream_operator.h"
39 #include "icapture_session.h"
40 #include "istream_common.h"
41 #include "camera_photo_proxy.h"
42 #include "iconsumer_surface.h"
43 #include "blocking_queue.h"
44 #include "drain_manager.h"
45 #include "moving_photo_proxy.h"
46 #include "safe_map.h"
47 #ifdef CAMERA_USE_SENSOR
48 #include "sensor_agent.h"
49 #include "sensor_agent_type.h"
50 #endif
51 namespace OHOS {
52 namespace CameraStandard {
53 
54 
55 enum class CaptureSessionReleaseType : int32_t {
56     RELEASE_TYPE_CLIENT = 0,
57     RELEASE_TYPE_CLIENT_DIED,
58     RELEASE_TYPE_SECURE,
59     RELEASE_TYPE_OBJ_DIED,
60 };
61 
62 enum class MechDeliveryState : int32_t {
63     NOT_ENABLED = 0,
64     NEED_ENABLE,
65     ENABLED,
66 };
67 
68 class StateMachine {
69 public:
70     explicit StateMachine();
71     virtual ~StateMachine() = default;
72     bool CheckTransfer(CaptureSessionState targetState);
73     bool Transfer(CaptureSessionState targetState);
74 
GetCurrentState()75     inline CaptureSessionState GetCurrentState()
76     {
77         std::lock_guard<std::recursive_mutex> lock(sessionStateLock_);
78         return currentState_;
79     }
80 
StateGuard(const std::function<void (const CaptureSessionState)> & fun)81     inline void StateGuard(const std::function<void(const CaptureSessionState)>& fun)
82     {
83         std::lock_guard<std::recursive_mutex> lock(sessionStateLock_);
84         fun(currentState_);
85     }
86 
IsStateNoLock(CaptureSessionState targetState)87     inline bool IsStateNoLock(CaptureSessionState targetState)
88     {
89         return currentState_ == targetState;
90     }
91 
92 private:
93     std::vector<CaptureSessionState> stateTransferMap_[static_cast<uint32_t>(CaptureSessionState::SESSION_STATE_MAX)];
94     std::recursive_mutex sessionStateLock_;
95     CaptureSessionState currentState_ = CaptureSessionState::SESSION_INIT;
96 };
97 using MetaElementType = std::pair<int64_t, sptr<SurfaceBuffer>>;
98 using UpdateControlCenterCallback = std::function<void(bool)>;
99 
100 class CameraInfoDumper;
101 
102 class EXPORT_API HCaptureSession : public CaptureSessionStub, public IHCameraCloseListener, public ICameraIpcChecker {
103 public:
104     static CamServiceError NewInstance(const uint32_t callerToken, int32_t opMode, sptr<HCaptureSession>& outSession);
105     virtual ~HCaptureSession();
106 
107     int32_t BeginConfig() override;
108     int32_t CommitConfig() override;
109 
110     int32_t CanAddInput(const sptr<ICameraDeviceService>& cameraDevice, bool& result) override;
111     int32_t AddInput(const sptr<ICameraDeviceService>& cameraDevice) override;
112     int32_t AddOutput(StreamType streamType, const sptr<IRemoteObject>& stream) override;
113 
114     int32_t RemoveInput(const sptr<ICameraDeviceService>& cameraDevice) override;
115     int32_t RemoveOutput(StreamType streamType, const sptr<IRemoteObject>& stream) override;
116 
117     int32_t Start() override;
118     int32_t Stop() override;
119     int32_t Release() override;
120     int32_t Release(CaptureSessionReleaseType type);
121 
122     static void DestroyStubObjectForPid(pid_t pid);
123     int32_t SetCallback(const sptr<ICaptureSessionCallback>& callback) override;
124     int32_t UnSetCallback() override;
125 
126     int32_t SetPressureCallback(const sptr<IPressureStatusCallback>& callback) override;
127     int32_t UnSetPressureCallback() override;
128     void SetPressureStatus(PressureStatus status);
129     int32_t SetControlCenterEffectStatusCallback(const sptr<IControlCenterEffectStatusCallback>& callback) override;
130     int32_t UnSetControlCenterEffectStatusCallback() override;
131     void SetControlCenterEffectCallbackStatus(ControlCenterStatusInfo statusInfo);
132 
133     int32_t GetSessionState(CaptureSessionState& sessionState) override;
134     int32_t GetActiveColorSpace(int32_t& curColorSpace) override;
135     int32_t SetColorSpace(int32_t curColorSpace, bool isNeedUpdate) override;
136     bool QueryFpsAndZoomRatio(
137         float &currentFps, float &currentZoomRatio, std::vector<float> &crossZoomAndTime, int32_t operationMode);
138     bool QueryZoomPerformance(
139         std::vector<float> &crossZoomAndTime, int32_t operationMode, const camera_metadata_item_t &zoomItem);
140     int32_t GetRangeId(float& zoomRatio, std::vector<float>& crossZoom);
141     float GetCrossWaitTime(std::vector<std::vector<float>>& crossTime, int32_t targetRangeId, int32_t currentRangeId);
142     void GetCrossZoomAndTime(std::vector<float>& crossZoomAndTime,
143         std::vector<float>& crossZoom, std::vector<std::vector<float>>& crossTime);
144     bool QueryZoomBezierValue(std::vector<float>& zoomBezierValue);
145     int32_t SetSmoothZoom(
146         int32_t smoothZoomType, int32_t operationMode, float targetZoomRatio, float& duration) override;
147     float CalculateWaitTime(const std::vector<float>& mCrossZoom, float currentZoomRatio,
148         float targetZoomRatio, float waitMs, float frameIntervalMs, const std::vector<float>& array);
149     void AddZoomAndTimeArray(
150         std::vector<uint32_t> &zoomAndTimeArray, std::vector<float> array, float frameIntervalMs, float waitTime);
151     int32_t EnableMovingPhoto(bool isEnable) override;
152     pid_t GetPid();
153     int32_t GetSessionId();
154     int32_t GetCurrentStreamInfos(std::vector<StreamInfo_V1_1>& streamInfos);
155     int32_t GetopMode();
156 
157     int32_t OperatePermissionCheck(uint32_t interfaceCode) override;
158     int32_t CallbackEnter([[maybe_unused]] uint32_t code) override;
159     int32_t CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result) override;
160     int32_t EnableMovingPhotoMirror(bool isMirror, bool isConfig) override;
161     std::shared_ptr<PhotoAssetIntf> ProcessPhotoProxy(int32_t captureId,
162         std::shared_ptr<PictureIntf> picturePtr, bool isBursting,
163         sptr<CameraServerPhotoProxy> cameraPhotoProxy, std::string &uri);
164     int32_t SetFeatureMode(int32_t featureMode) override;
165     void GetOutputStatus(int32_t &status);
166     int32_t SetPreviewRotation(const std::string &deviceClass) override;
167     int32_t SetCommitConfigFlag(bool isNeedCommitting) override;
168 
169     void DumpSessionInfo(CameraInfoDumper& infoDumper);
170     static void DumpSessions(CameraInfoDumper& infoDumper);
171     static void DumpCameraSessionSummary(CameraInfoDumper& infoDumper);
172     void ReleaseStreams();
173     bool isEqual(float zoomPointA, float zoomPointB);
174     int32_t GetVirtualApertureMetadate(std::vector<float>& virtualApertureMetadata) override;
175     int32_t GetVirtualApertureValue(float& value) override;
176     int32_t SetVirtualApertureValue(float value, bool needPersist) override;
177 
178     int32_t GetBeautyMetadata(std::vector<int32_t>& beautyApertureMetadata) override;
179     int32_t GetBeautyRange(std::vector<int32_t>& range, int32_t type) override;
180     int32_t GetBeautyValue(int32_t type, int32_t& value) override;
181     int32_t SetBeautyValue(int32_t type, int32_t value, bool needPersist) override;
182 
183     void SetControlCenterPrecondition(bool precondition);
184     void SetDeviceControlCenterAbility(bool ability);
185 
186     std::string GetBundleForControlCenter();
187     void SetBundleForControlCenter(std::string bundleName);
SetStreamOperator(wptr<HStreamOperator> hStreamOperator)188     inline void SetStreamOperator(wptr<HStreamOperator> hStreamOperator)
189     {
190         std::lock_guard<std::mutex> lock(streamOperatorLock_);
191         if (hStreamOperator == nullptr) {
192             return;
193         }
194         hStreamOperator_ = hStreamOperator;
195         auto hStreamOperatorSptr = hStreamOperator_.promote();
196         if (hStreamOperatorSptr != nullptr) {
197             hStreamOperatorSptr->SetCameraDevice(cameraDevice_);
198         }
199     }
200 
GetStreamOperator()201     inline sptr<HStreamOperator> GetStreamOperator()
202     {
203         std::lock_guard<std::mutex> lock(streamOperatorLock_);
204         return hStreamOperator_.promote();
205     }
206 
207     void ConfigPayload(uint32_t pid, uint32_t tid, const char *bundleName, int32_t qosLevel,
208         std::unordered_map<std::string, std::string> &mapPayload);
209     void SetCameraRotateStrategyInfos(std::vector<CameraRotateStrategyInfo> infos);
210     std::vector<CameraRotateStrategyInfo> GetCameraRotateStrategyInfos();
211 
212     void BeforeDeviceClose() override;
213 
214     void OnSessionPreempt();
215     void UpdateHookBasicInfo(std::map<int32_t, std::string> ParameterMap);
216 
217     void SetUserId(int32_t userId);
218     int32_t GetUserId();
219     int32_t EnableMechDelivery(bool isEnableMech);
220     void SetMechDeliveryState(MechDeliveryState state);
221     void SetUpdateControlCenterCallback(UpdateControlCenterCallback cb);
222     bool GetCaptureSessionInfo(CaptureSessionInfo& sessionInfo);
223 
224 private:
225     int32_t CommitConfigWithValidation();
226     void InitDefaultColortSpace(SceneMode opMode);
227     explicit HCaptureSession(const uint32_t callingTokenId, int32_t opMode);
228     void UpdateBasicInfoForStream(std::map<int32_t, std::string> ParameterMap,
229         sptr<OHOS::IBufferProducer> previewProducer, sptr<OHOS::IBufferProducer> videoProducer,
230         std::shared_ptr<OHOS::Camera::CameraMetadata> settings);
231     void DealPluginCode(std::map<int32_t, std::string> ParameterMap,
232         std::shared_ptr<OHOS::Camera::CameraMetadata> settings, int32_t code, int32_t value);
233     string lastDisplayName_ = "";
234     string lastBurstPrefix_ = "";
235     int32_t saveIndex = 0;
236     bool isNeedCommitting_ = false;
237     std::shared_ptr<CameraDataShareHelper> cameraDataShareHelper_;
238     void SetCameraDevice(sptr<HCameraDevice> device);
GetCameraDevice()239     inline const sptr<HCameraDevice> GetCameraDevice()
240     {
241         std::lock_guard<std::mutex> lock(cameraDeviceLock_);
242         return cameraDevice_;
243     }
244     string CreateDisplayName(const std::string& suffix);
245     string CreateBurstDisplayName(int32_t imageSeqId, int32_t seqId);
246     int32_t ValidateSessionInputs();
247     int32_t ValidateSessionOutputs();
248     int32_t ValidateSession();
249     int32_t RemoveOutputStream(sptr<HStreamCommon> stream);
250     int32_t LinkInputAndOutputs();
251     int32_t UnlinkInputAndOutputs();
252 
253     void ClearSketchRepeatStream();
254     void ExpandSketchRepeatStream();
255     void ExpandMovingPhotoRepeatStream();
256 
257     int32_t SetVirtualApertureToDataShareHelper(float value);
258     int32_t GetVirtualApertureFromDataShareHelper(float &value);
259 
260     int32_t SetBeautyToDataShareHelper(int32_t value);
261     int32_t GetBeautyFromDataShareHelper(int32_t &value);
262 
263     void ProcessMetaZoomArray(std::vector<uint32_t>& zoomAndTimeArray, sptr<HCameraDevice>& cameraDevice);
264     void UpdateMuteSetting(bool muteMode, std::shared_ptr<OHOS::Camera::CameraMetadata> &settings);
265     void UpdateCameraControl(bool isStart);
266     void StartMovingPhoto(sptr<HStreamRepeat>& curStreamRepeat);
267     int32_t GetSensorOritation();
268     std::string GetSessionState();
269 
270     void DynamicConfigStream();
271     bool IsNeedDynamicConfig();
272     int32_t SetHasFitedRotation(bool isHasFitedRotation) override;
273     void InitialHStreamOperator();
274     void UpdateSettingForSpecialBundle();
275     int32_t UpdateSettingForFocusTrackingMech(bool isEnableMech);
276     void UpdateSettingForFocusTrackingMechBeforeStart(std::shared_ptr<OHOS::Camera::CameraMetadata> &settings);
277     void SetDeviceMechCallback();
278     void ClearMovingPhotoRepeatStream();
279     StateMachine stateMachine_;
280     sptr<IPressureStatusCallback> innerPressureCallback_;
281     sptr<IControlCenterEffectStatusCallback> innerControlCenterEffectCallback_;
282     std::mutex innerControlCenterEffectCallbackLock_;
283     bool isBeautyActive = false;
284     bool isApertureActive = false;
285     float biggestAperture = 0;
286     bool controlCenterPrecondition = true;
287     bool deviceControlCenterAbility = false;
288 
289     std::string bundleForControlCenter_;
290 
291     #ifdef CAMERA_USE_SENSOR
292         std::mutex sensorLock_;
293         bool isRegisterSensorSuccess_ = false;
294         void RegisterSensorCallback();
295         void UnregisterSensorCallback();
296         static void GravityDataCallbackImpl(SensorEvent *event);
297         static int32_t CalcSensorRotation(int32_t sensorDegree);
298         static int32_t CalcRotationDegree(GravityData data);
299     #endif
300 
301     std::string GetConcurrentCameraIds(pid_t pid);
302     int32_t AddOutputInner(StreamType streamType, const sptr<IStreamCommon>& stream);
303     int32_t RemoveOutputInner(StreamType streamType, const sptr<IStreamCommon>& stream);
304 
305     uint32_t GetEquivalentFocus();
306     std::vector<OutputInfo> GetOutputInfos();
307     void OnCaptureSessionConfiged();
308     void OnZoomInfoChange(const ZoomInfo& zoomInfo);
309     void OnSessionStatusChange(bool status);
310 
311     std::mutex cbMutex_;
312 
313     // Make sure device thread safe,set device by {SetCameraDevice}, get device by {GetCameraDevice}
314     std::mutex cameraDeviceLock_;
315     std::mutex streamOperatorLock_;
316     sptr<HCameraDevice> cameraDevice_;
317 
318     StreamContainer streamContainer_;
319     #ifdef CAMERA_USE_SENSOR
320         SensorUser user;
321     #endif
322     pid_t pid_ = 0;
323     uid_t uid_ = 0;
324     int32_t sessionId_ = 0;
325     uint32_t callerToken_ = 0;
326     int32_t opMode_ = 0;
327     int32_t featureMode_ = 0;
328     bool isSessionStarted_ = false;
329     bool isDynamicConfiged_ = false;
330     std::string deviceClass_ = "phone";
331     wptr<HStreamOperator> hStreamOperator_ = nullptr;
332     std::mutex cameraRotateStrategyInfosLock_;
333     std::vector<CameraRotateStrategyInfo> cameraRotateStrategyInfos_;
334     bool isHasFitedRotation_ = false;
335     std::string bundleName_ = "";
336     std::mutex cameraRotateUpdateBasicInfo_;
337     int32_t userId_ = 0;
338     std::mutex mechDeliveryStateLock_;
339     MechDeliveryState mechDeliveryState_ = MechDeliveryState::NOT_ENABLED;
340     UpdateControlCenterCallback updateControlCenterCallback_;
341 };
342 } // namespace CameraStandard
343 } // namespace OHOS
344 #endif // OHOS_CAMERA_H_CAPTURE_SESSION_H
345