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 std::map<CaptureSessionState, std::string> sessionState_; 116 pid_t pid_; 117 int32_t uid_; 118 uint32_t callerToken_; 119 std::shared_ptr<PermissionStatusChangeCb> callbackPtr_; 120 std::shared_ptr<CameraUseStateChangeCb> cameraUseCallbackPtr_; 121 }; 122 123 class PermissionStatusChangeCb : public Security::AccessToken::PermStateChangeCallbackCustomize { 124 public: PermissionStatusChangeCb(const Security::AccessToken::PermStateChangeScope & scopeInfo)125 explicit PermissionStatusChangeCb(const Security::AccessToken::PermStateChangeScope &scopeInfo) 126 : PermStateChangeCallbackCustomize(scopeInfo) {} ~PermissionStatusChangeCb()127 ~PermissionStatusChangeCb() {} 128 void PermStateChangeCallback(Security::AccessToken::PermStateChangeInfo& result) override; 129 void SetCaptureSession(sptr<HCaptureSession> captureSession); 130 131 private: 132 sptr<HCaptureSession> captureSession_; 133 }; 134 135 class CameraUseStateChangeCb : public Security::AccessToken::StateCustomizedCbk { 136 public: CameraUseStateChangeCb()137 CameraUseStateChangeCb() {} ~CameraUseStateChangeCb()138 ~CameraUseStateChangeCb() {} 139 void StateChangeNotify(Security::AccessToken::AccessTokenID tokenId, bool isShowing) override; 140 void SetCaptureSession(sptr<HCaptureSession> captureSession); 141 142 private: 143 sptr<HCaptureSession> captureSession_; 144 }; 145 146 class StreamOperatorCallback : public IStreamOperatorCallback { 147 public: 148 StreamOperatorCallback() = default; 149 explicit StreamOperatorCallback(sptr<HCaptureSession> session); 150 virtual ~StreamOperatorCallback() = default; 151 152 int32_t OnCaptureStarted(int32_t captureId, const std::vector<int32_t>& streamIds) override; 153 int32_t OnCaptureEnded(int32_t captureId, const std::vector<CaptureEndedInfo>& infos) override; 154 int32_t OnCaptureError(int32_t captureId, const std::vector<CaptureErrorInfo>& infos) override; 155 int32_t OnFrameShutter(int32_t captureId, const std::vector<int32_t>& streamIds, uint64_t timestamp) override; 156 void SetCaptureSession(sptr<HCaptureSession> captureSession); 157 158 private: 159 sptr<HStreamCommon> GetStreamByStreamID(int32_t streamId); 160 sptr<HCaptureSession> captureSession_; 161 }; 162 } // namespace CameraStandard 163 } // namespace OHOS 164 #endif // OHOS_CAMERA_H_CAPTURE_SESSION_H 165