• 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 "accesstoken_kit.h"
20 #include "state_customized_cbk.h"
21 #include "hcamera_device.h"
22 #include "hcapture_session_stub.h"
23 #include "hstream_capture.h"
24 #include "hstream_metadata.h"
25 #include "hstream_repeat.h"
26 #include "perm_state_change_callback_customize.h"
27 #include "privacy_kit.h"
28 #include "v1_0/istream_operator_callback.h"
29 #include "v1_0/istream_operator.h"
30 
31 #include <refbase.h>
32 #include <iostream>
33 
34 namespace OHOS {
35 namespace CameraStandard {
36 using namespace OHOS::HDI::Camera::V1_0;
37 class StreamOperatorCallback;
38 class PermissionStatusChangeCb;
39 class CameraUseStateChangeCb;
40 
41 static const int32_t STREAMID_BEGIN = 1;
42 
43 class HCaptureSession : public HCaptureSessionStub {
44 public:
45     HCaptureSession(sptr<HCameraHostManager> cameraHostManager,
46         sptr<StreamOperatorCallback> streamOperatorCb, const uint32_t callingTokenId);
47     ~HCaptureSession();
48 
49     int32_t BeginConfig() override;
50     int32_t CommitConfig() override;
51 
52     int32_t AddInput(sptr<ICameraDeviceService> cameraDevice) override;
53     int32_t AddOutput(StreamType streamType, sptr<IStreamCommon> stream) override;
54 
55     int32_t RemoveInput(sptr<ICameraDeviceService> cameraDevice) override;
56     int32_t RemoveOutput(StreamType streamType, sptr<IStreamCommon> stream) override;
57 
58     int32_t Start() override;
59     int32_t Stop() override;
60     int32_t Release(pid_t pid) override;
61     int32_t ReleaseInner();
62     static void DestroyStubObjectForPid(pid_t pid);
63     int32_t SetCallback(sptr<ICaptureSessionCallback> &callback) override;
64 
65     int32_t GetSessionState(CaptureSessionState &sessionState) override;
66 
67     friend class StreamOperatorCallback;
68     static void dumpSessions(std::string& dumpString);
69     void dumpSessionInfo(std::string& dumpString);
70     static void CameraSessionSummary(std::string& dumpString);
71 
72 private:
73     int32_t ValidateSessionInputs();
74     int32_t ValidateSessionOutputs();
75     int32_t AddOutputStream(sptr<HStreamCommon> stream);
76     int32_t RemoveOutputStream(sptr<HStreamCommon> stream);
77     int32_t GetCameraDevice(sptr<HCameraDevice> &device);
78     int32_t HandleCaptureOuputsConfig(sptr<HCameraDevice> &device);
79     int32_t CreateAndCommitStreams(sptr<HCameraDevice> &device,
80 	                               std::shared_ptr<OHOS::Camera::CameraMetadata> &deviceSettings,
81                                    std::vector<StreamInfo> &streamInfos);
82     int32_t CheckAndCommitStreams(sptr<HCameraDevice> &device,
83 	                              std::shared_ptr<OHOS::Camera::CameraMetadata> &deviceSettings,
84                                   std::vector<StreamInfo> &allStreamInfos,
85                                   std::vector<StreamInfo> &newStreamInfos);
86     int32_t GetCurrentStreamInfos(sptr<HCameraDevice> &device,
87 	                              std::shared_ptr<OHOS::Camera::CameraMetadata> &deviceSettings,
88                                   std::vector<StreamInfo> &streamInfos);
89     void UpdateSessionConfig(sptr<HCameraDevice> &device);
90     void DeleteReleasedStream();
91     void RestorePreviousState(sptr<HCameraDevice> &device, bool isCreateReleaseStreams);
92     void ReleaseStreams();
93     void ClearCaptureSession(pid_t pid);
94     void RegisterPermissionCallback(const uint32_t callingTokenId, const std::string permissionName);
95     void UnregisterPermissionCallback(const uint32_t callingTokenId);
96     void StartUsingPermissionCallback(const uint32_t callingTokenId, const std::string permissionName);
97     void StopUsingPermissionCallback(const uint32_t callingTokenId, const std::string permissionName);
98 
99     std::string GetSessionState();
100 
101     CaptureSessionState curState_ = CaptureSessionState::SESSION_INIT;
102     CaptureSessionState prevState_ = CaptureSessionState::SESSION_INIT;
103     sptr<HCameraDevice> cameraDevice_;
104     std::vector<sptr<HStreamCommon>> repeatStreams_;
105     std::vector<sptr<HStreamCommon>> captureStreams_;
106     std::vector<sptr<HStreamCommon>> metadataStreams_;
107     std::vector<sptr<HStreamCommon>> streams_;
108     std::vector<sptr<HStreamCommon>> tempStreams_;
109     std::vector<sptr<HCameraDevice>> tempCameraDevices_;
110     std::vector<int32_t> deletedStreamIds_;
111     sptr<HCameraHostManager> cameraHostManager_;
112     sptr<StreamOperatorCallback> streamOperatorCallback_;
113     sptr<ICaptureSessionCallback> sessionCallback_;
114     int32_t streamId_ = STREAMID_BEGIN;
115     pid_t pid_;
116     int32_t uid_;
117     uint32_t callerToken_;
118     std::shared_ptr<PermissionStatusChangeCb> callbackPtr_;
119     std::shared_ptr<CameraUseStateChangeCb> cameraUseCallbackPtr_;
120 };
121 
122 class PermissionStatusChangeCb : public Security::AccessToken::PermStateChangeCallbackCustomize {
123 public:
PermissionStatusChangeCb(const Security::AccessToken::PermStateChangeScope & scopeInfo)124     explicit PermissionStatusChangeCb(const Security::AccessToken::PermStateChangeScope &scopeInfo)
125         : PermStateChangeCallbackCustomize(scopeInfo) {}
126     ~PermissionStatusChangeCb();
127     void PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo& result) override;
128     void SetCaptureSession(sptr<HCaptureSession> captureSession);
129 
130 private:
131     wptr<HCaptureSession> captureSession_;
132 };
133 
134 class CameraUseStateChangeCb : public Security::AccessToken::StateCustomizedCbk {
135 public:
CameraUseStateChangeCb()136     CameraUseStateChangeCb() {}
137     ~CameraUseStateChangeCb();
138     void StateChangeNotify(Security::AccessToken::AccessTokenID tokenId, bool isShowing) override;
139     void SetCaptureSession(sptr<HCaptureSession> captureSession);
140 
141 private:
142     wptr<HCaptureSession> captureSession_;
143 };
144 
145 class StreamOperatorCallback : public IStreamOperatorCallback {
146 public:
147     StreamOperatorCallback() = default;
148     explicit StreamOperatorCallback(sptr<HCaptureSession> session);
149     virtual ~StreamOperatorCallback();
150 
151     int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds) override;
152     int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos) override;
153     int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos) override;
154     int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp) override;
155     void SetCaptureSession(sptr<HCaptureSession> captureSession);
156 
157 private:
158     sptr<HStreamCommon> GetStreamByStreamID(int32_t streamId);
159     sptr<HCaptureSession> captureSession_;
160 };
161 } // namespace CameraStandard
162 } // namespace OHOS
163 #endif // OHOS_CAMERA_H_CAPTURE_SESSION_H
164