• 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_CAPTURE_SESSION_H
17 #define OHOS_CAMERA_H_CAPTURE_SESSION_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <iostream>
22 #include <list>
23 #include <mutex>
24 #include <refbase.h>
25 #include <unordered_map>
26 
27 #include "accesstoken_kit.h"
28 #include "camera_util.h"
29 #include "hcamera_device.h"
30 #include "hcapture_session_stub.h"
31 #include "hstream_capture.h"
32 #include "hstream_metadata.h"
33 #include "hstream_repeat.h"
34 #include "icapture_session.h"
35 #include "istream_common.h"
36 #include "perm_state_change_callback_customize.h"
37 #include "privacy_kit.h"
38 #include "state_customized_cbk.h"
39 #include "v1_0/istream_operator.h"
40 #include "v1_1/istream_operator.h"
41 #include "v1_2/istream_operator.h"
42 #include "v1_2/istream_operator_callback.h"
43 #include "hcamera_restore_param.h"
44 
45 namespace OHOS {
46 namespace CameraStandard {
47 using OHOS::HDI::Camera::V1_0::CaptureEndedInfo;
48 using OHOS::HDI::Camera::V1_0::CaptureErrorInfo;
49 using namespace OHOS::HDI::Display::Graphic::Common::V1_0;
50 class PermissionStatusChangeCb;
51 class CameraUseStateChangeCb;
52 
53 static const int32_t STREAM_NOT_FOUNT = -1;
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 class StateMachine {
63 public:
64     explicit StateMachine();
65     virtual ~StateMachine() = default;
66     bool CheckTransfer(CaptureSessionState targetState);
67     bool Transfer(CaptureSessionState targetState);
68 
GetCurrentState()69     inline CaptureSessionState GetCurrentState()
70     {
71         std::lock_guard<std::recursive_mutex> lock(sessionStateLock_);
72         return currentState_;
73     }
74 
StateGuard(const std::function<void (const CaptureSessionState)> & fun)75     inline void StateGuard(const std::function<void(const CaptureSessionState)>& fun)
76     {
77         std::lock_guard<std::recursive_mutex> lock(sessionStateLock_);
78         fun(currentState_);
79     }
80 
81 private:
82     std::vector<CaptureSessionState> stateTransferMap_[static_cast<uint32_t>(CaptureSessionState::SESSION_STATE_MAX)];
83     std::recursive_mutex sessionStateLock_;
84     CaptureSessionState currentState_ = CaptureSessionState::SESSION_INIT;
85 };
86 
87 class StreamContainer {
88 public:
StreamContainer()89     StreamContainer() {};
90     virtual ~StreamContainer() = default;
91 
92     bool AddStream(sptr<HStreamCommon> stream);
93     bool RemoveStream(sptr<HStreamCommon> stream);
94     sptr<HStreamCommon> GetStream(int32_t streamId);
95     void Clear();
96     size_t Size();
97 
98     std::list<sptr<HStreamCommon>> GetStreams(const StreamType streamType);
99     std::list<sptr<HStreamCommon>> GetAllStreams();
100 
101 private:
102     std::mutex streamsLock_;
103     std::map<const StreamType, std::list<sptr<HStreamCommon>>> streams_;
104 };
105 
106 class StreamOperatorCallback : public OHOS::HDI::Camera::V1_2::IStreamOperatorCallback {
107 public:
108     StreamOperatorCallback() = default;
109     virtual ~StreamOperatorCallback() = default;
110 
111     int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds) override;
112     int32_t OnCaptureStarted_V1_2(
113         int32_t captureId, const std::vector<OHOS::HDI::Camera::V1_2::CaptureStartedInfo>& infos) override;
114     int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos) override;
115     int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos) override;
116     int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp) override;
117 
118     virtual const sptr<HStreamCommon> GetStreamByStreamID(int32_t streamId) = 0;
119 
120 private:
121     std::mutex cbMutex_;
122 };
123 
124 class HCaptureSession : public HCaptureSessionStub, public StreamOperatorCallback {
125 public:
126     explicit HCaptureSession(const uint32_t callingTokenId, int32_t opMode);
127     virtual ~HCaptureSession();
128 
129     int32_t BeginConfig() override;
130     int32_t CommitConfig() override;
131 
132     int32_t CanAddInput(sptr<ICameraDeviceService> cameraDevice, bool& result) override;
133     int32_t AddInput(sptr<ICameraDeviceService> cameraDevice) override;
134     int32_t AddOutput(StreamType streamType, sptr<IStreamCommon> stream) override;
135 
136     int32_t RemoveInput(sptr<ICameraDeviceService> cameraDevice) override;
137     int32_t RemoveOutput(StreamType streamType, sptr<IStreamCommon> stream) override;
138 
139     int32_t Start() override;
140     int32_t Stop() override;
141     int32_t Release() override;
142     int32_t Release(CaptureSessionReleaseType type);
143 
144     static void DestroyStubObjectForPid(pid_t pid);
145     int32_t SetCallback(sptr<ICaptureSessionCallback>& callback) override;
146 
147     int32_t GetSessionState(CaptureSessionState& sessionState) override;
148     int32_t GetActiveColorSpace(ColorSpace& colorSpace) override;
149     int32_t SetColorSpace(ColorSpace colorSpace, ColorSpace captureColorSpace, bool isNeedUpdate) override;
150     bool QueryFpsAndZoomRatio(float& currentFps, float& currentZoomRatio);
151     bool QueryZoomPerformance(std::vector<float>& crossZoomAndTime, int32_t operationMode);
152     int32_t SetSmoothZoom(int32_t smoothZoomType, int32_t operationMode, float targetZoomRatio,
153         float &duration) override;
154 
155     static void dumpSessions(std::string& dumpString);
156     void dumpSessionInfo(std::string& dumpString);
157     static void CameraSessionSummary(std::string& dumpString);
158     pid_t GetPid();
159     int32_t GetCurrentStreamInfos(std::vector<StreamInfo_V1_1>& streamInfos);
160     int32_t GetopMode();
161 
162     int32_t OperatePermissionCheck(uint32_t interfaceCode) override;
163     const sptr<HStreamCommon> GetStreamByStreamID(int32_t streamId) override;
164 
165 private:
SetCameraDevice(sptr<HCameraDevice> device)166     inline void SetCameraDevice(sptr<HCameraDevice> device)
167     {
168         std::lock_guard<std::mutex> lock(cameraDeviceLock_);
169         cameraDevice_ = device;
170     }
171 
GetCameraDevice()172     inline const sptr<HCameraDevice> GetCameraDevice()
173     {
174         std::lock_guard<std::mutex> lock(cameraDeviceLock_);
175         return cameraDevice_;
176     }
177 
178     int32_t ValidateSessionInputs();
179     int32_t ValidateSessionOutputs();
180     int32_t AddOutputStream(sptr<HStreamCommon> stream);
181     int32_t RemoveOutputStream(sptr<HStreamCommon> stream);
182     int32_t LinkInputAndOutputs();
183     int32_t UnlinkInputAndOutputs();
184 
185     void ReleaseStreams();
186     void RegisterPermissionCallback(const uint32_t callingTokenId, const std::string permissionName);
187     void UnregisterPermissionCallback(const uint32_t callingTokenId);
188     void StartUsingPermissionCallback(const uint32_t callingTokenId, const std::string permissionName);
189     void StopUsingPermissionCallback(const uint32_t callingTokenId, const std::string permissionName);
190 
191     void ClearSketchRepeatStream();
192     void ExpandSketchRepeatStream();
193     int32_t CheckIfColorSpaceMatchesFormat(ColorSpace colorSpace);
194     void CancelStreamsAndGetStreamInfos(std::vector<StreamInfo_V1_1>& streamInfos);
195     void RestartStreams();
196     int32_t UpdateStreamInfos();
197     void SetColorSpaceForStreams();
198 
199     std::string GetSessionState();
200 
201     StateMachine stateMachine_;
202 
203     // Make sure device thread safe,set device by {SetCameraDevice}, get device by {GetCameraDevice}
204     std::mutex cameraDeviceLock_;
205     sptr<HCameraDevice> cameraDevice_;
206 
207     StreamContainer streamContainer_;
208 
209     pid_t pid_;
210     uid_t uid_;
211     uint32_t callerToken_;
212     std::shared_ptr<PermissionStatusChangeCb> callbackPtr_;
213     std::shared_ptr<CameraUseStateChangeCb> cameraUseCallbackPtr_;
214     int32_t opMode_;
215     ColorSpace currColorSpace_ = ColorSpace::COLOR_SPACE_UNKNOWN;
216     ColorSpace currCaptureColorSpace_ = ColorSpace::COLOR_SPACE_UNKNOWN;
217     bool isSessionStarted_ = false;
218 };
219 
220 class PermissionStatusChangeCb : public Security::AccessToken::PermStateChangeCallbackCustomize {
221 public:
PermissionStatusChangeCb(wptr<HCaptureSession> session,const Security::AccessToken::PermStateChangeScope & scopeInfo)222     explicit PermissionStatusChangeCb(
223         wptr<HCaptureSession> session, const Security::AccessToken::PermStateChangeScope& scopeInfo)
224         : PermStateChangeCallbackCustomize(scopeInfo), captureSession_(session)
225     {}
226     virtual ~PermissionStatusChangeCb() = default;
227     void PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo& result) override;
228 
229 private:
230     wptr<HCaptureSession> captureSession_;
231 };
232 
233 class CameraUseStateChangeCb : public Security::AccessToken::StateCustomizedCbk {
234 public:
CameraUseStateChangeCb(wptr<HCaptureSession> session)235     explicit CameraUseStateChangeCb(wptr<HCaptureSession> session) : captureSession_(session) {}
236     virtual ~CameraUseStateChangeCb() = default;
237     void StateChangeNotify(Security::AccessToken::AccessTokenID tokenId, bool isShowing) override;
238 
239 private:
240     wptr<HCaptureSession> captureSession_;
241 };
242 } // namespace CameraStandard
243 } // namespace OHOS
244 #endif // OHOS_CAMERA_H_CAPTURE_SESSION_H
245