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 "hcamera_device.h" 20 #include "hcapture_session_stub.h" 21 #include "hstream_capture.h" 22 #include "hstream_repeat.h" 23 #include "stream_operator_callback_stub.h" 24 25 #include <refbase.h> 26 #include <iostream> 27 28 namespace OHOS { 29 namespace CameraStandard { 30 class StreamOperatorCallback; 31 32 enum class CaptureSessionState { 33 SESSION_INIT = 0, 34 SESSION_CONFIG_INPROGRESS, 35 SESSION_CONFIG_COMMITTED, 36 }; 37 38 static const int32_t STREAMID_BEGIN = 1; 39 40 class HCaptureSession : public HCaptureSessionStub { 41 public: 42 HCaptureSession(sptr<HCameraHostManager> cameraHostManager, sptr<StreamOperatorCallback> streamOperatorCb); 43 ~HCaptureSession(); 44 45 int32_t BeginConfig() override; 46 int32_t CommitConfig() override; 47 48 int32_t AddInput(sptr<ICameraDeviceService> cameraDevice) override; 49 int32_t AddOutput(sptr<IStreamRepeat> streamRepeat) override; 50 int32_t AddOutput(sptr<IStreamCapture> streamCapture) override; 51 52 int32_t RemoveInput(sptr<ICameraDeviceService> cameraDevice) override; 53 int32_t RemoveOutput(sptr<IStreamCapture> streamCapture) override; 54 int32_t RemoveOutput(sptr<IStreamRepeat> streamRepeat) override; 55 56 int32_t Start() override; 57 int32_t Stop() override; 58 int32_t Release(pid_t pid) override; 59 static void DestroyStubObjectForPid(pid_t pid); 60 int32_t SetCallback(sptr<ICaptureSessionCallback> &callback) override; 61 62 friend class StreamOperatorCallback; 63 static void dumpSessions(std::string& dumpString); 64 void dumpSessionInfo(std::string& dumpString); 65 static void CameraSessionSummary(std::string& dumpString); 66 67 private: 68 int32_t ValidateSessionInputs(); 69 int32_t ValidateSessionOutputs(); 70 int32_t GetCameraDevice(sptr<HCameraDevice> &device); 71 int32_t HandleCaptureOuputsConfig(sptr<HCameraDevice> &device); 72 int32_t CreateAndCommitStreams(sptr<HCameraDevice> &device, std::shared_ptr<Camera::CameraMetadata> &deviceSettings, 73 std::vector<std::shared_ptr<Camera::StreamInfo>> &streamInfos); 74 int32_t CheckAndCommitStreams(sptr<HCameraDevice> &device, std::shared_ptr<Camera::CameraMetadata> &deviceSettings, 75 std::vector<std::shared_ptr<Camera::StreamInfo>> &allStreamInfos, 76 std::vector<std::shared_ptr<Camera::StreamInfo>> &newStreamInfos); 77 int32_t GetCurrentStreamInfos(sptr<HCameraDevice> &device, std::shared_ptr<Camera::CameraMetadata> &deviceSettings, 78 std::vector<std::shared_ptr<Camera::StreamInfo>> &streamInfos); 79 void UpdateSessionConfig(sptr<HCameraDevice> &device); 80 void DeleteReleasedStream(); 81 void RestorePreviousState(sptr<HCameraDevice> &device, bool isCreateReleaseStreams); 82 void ReleaseStreams(); 83 void ClearCaptureSession(pid_t pid); 84 std::string GetSessionState(); 85 86 CaptureSessionState curState_ = CaptureSessionState::SESSION_INIT; 87 CaptureSessionState prevState_ = CaptureSessionState::SESSION_INIT; 88 sptr<HCameraDevice> cameraDevice_; 89 std::vector<sptr<HStreamRepeat>> streamRepeats_; 90 std::vector<sptr<HStreamCapture>> streamCaptures_; 91 std::vector<sptr<HCameraDevice>> cameraDevices_; 92 std::vector<sptr<HStreamRepeat>> tempStreamRepeats_; 93 std::vector<sptr<HStreamCapture>> tempStreamCaptures_; 94 std::vector<sptr<HCameraDevice>> tempCameraDevices_; 95 std::vector<int32_t> deletedStreamIds_; 96 sptr<HCameraHostManager> cameraHostManager_; 97 sptr<StreamOperatorCallback> streamOperatorCallback_; 98 sptr<ICaptureSessionCallback> sessionCallback_; 99 int32_t streamId_ = STREAMID_BEGIN; 100 std::map<CaptureSessionState, std::string> sessionState_; 101 pid_t pid_; 102 int32_t uid_; 103 }; 104 105 class StreamOperatorCallback : public Camera::StreamOperatorCallbackStub { 106 public: 107 StreamOperatorCallback() = default; 108 StreamOperatorCallback(sptr<HCaptureSession> session); 109 virtual ~StreamOperatorCallback() = default; 110 111 virtual void OnCaptureStarted(int32_t captureId, const std::vector<int32_t> &streamId) override; 112 virtual void OnCaptureEnded(int32_t captureId, 113 const std::vector<std::shared_ptr<Camera::CaptureEndedInfo>> &info) override; 114 virtual void OnCaptureError(int32_t captureId, 115 const std::vector<std::shared_ptr<Camera::CaptureErrorInfo>> &info) override; 116 virtual void OnFrameShutter(int32_t captureId, 117 const std::vector<int32_t> &streamId, uint64_t timestamp) override; 118 void SetCaptureSession(sptr<HCaptureSession> captureSession); 119 120 private: 121 sptr<HStreamCapture> GetStreamCaptureByStreamID(std::int32_t streamId); 122 sptr<HStreamRepeat> GetStreamRepeatByStreamID(std::int32_t streamId); 123 sptr<HCaptureSession> captureSession_; 124 }; 125 } // namespace CameraStandard 126 } // namespace OHOS 127 #endif // OHOS_CAMERA_H_CAPTURE_SESSION_H 128