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