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