1 /* 2 * Copyright (c) 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_STREAM_COMMON_H 17 #define OHOS_CAMERA_H_STREAM_COMMON_H 18 19 #include <atomic> 20 #include <cstdint> 21 #include <iostream> 22 #include <mutex> 23 #include <refbase.h> 24 25 #include "camera/v1_2/types.h" 26 #include "camera/v1_3/types.h" 27 #include "camera_info_dumper.h" 28 #include "camera_metadata_info.h" 29 #include "icapture_session.h" 30 #include "istream_common.h" 31 #include "v1_0/istream_operator.h" 32 #include "v1_1/istream_operator.h" 33 #include "v1_2/istream_operator.h" 34 #include "v1_3/istream_operator.h" 35 #include "ability/camera_ability_const.h" 36 37 namespace OHOS { 38 namespace CameraStandard { 39 using OHOS::HDI::Camera::V1_0::IStreamOperator; 40 using OHOS::HDI::Camera::V1_1::StreamInfo_V1_1; 41 constexpr int32_t CAPTURE_ID_UNSET = 0; 42 constexpr int32_t STREAM_ID_UNSET = 0; 43 class HStreamCommon : virtual public RefBase { 44 public: 45 explicit HStreamCommon( 46 StreamType streamType, sptr<OHOS::IBufferProducer> producer, int32_t format, int32_t width, int32_t height); 47 explicit HStreamCommon(StreamType streamType, int32_t format, int32_t width, int32_t height); 48 virtual ~HStreamCommon(); 49 virtual int32_t LinkInput(wptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator, 50 std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility); 51 virtual int32_t UnlinkInput(); 52 virtual void SetStreamInfo(StreamInfo_V1_1& streamInfo) = 0; 53 virtual int32_t ReleaseStream(bool isDelay) = 0; 54 virtual void DumpStreamInfo(CameraInfoDumper& infoDumper) = 0; 55 56 virtual void SetColorSpace(ColorSpace colorSpace) final; 57 virtual ColorSpace GetColorSpace() final; 58 virtual int32_t StopStream() final; 59 60 virtual int32_t GetPreparedCaptureId() final; 61 62 void PrintCaptureDebugLog(const std::shared_ptr<OHOS::Camera::CameraMetadata> &captureMetadataSetting_); 63 void SetBasicInfo(std::map<int32_t, std::string> basicParam); 64 GetFwkStreamId()65 inline int32_t GetFwkStreamId() 66 { 67 return fwkStreamId_; 68 } 69 GetStreamType()70 inline StreamType GetStreamType() 71 { 72 return streamType_; 73 } 74 SetHdiStreamId(int32_t hdiStreamId)75 inline void SetHdiStreamId(int32_t hdiStreamId) 76 { 77 hdiStreamId_ = hdiStreamId; 78 } 79 GetBasicInfo()80 inline std::map<int32_t, std::string> GetBasicInfo() 81 { 82 std::lock_guard<std::mutex> lock(basicInfoLock_); 83 return param; 84 } 85 GetStreamProducer()86 inline sptr<OHOS::IBufferProducer> GetStreamProducer() 87 { 88 std::lock_guard<std::mutex> lock(producerLock_); 89 return producer_; 90 } 91 GetHdiStreamId()92 inline int32_t GetHdiStreamId() 93 { 94 return hdiStreamId_; 95 } 96 97 int32_t format_; 98 int32_t width_; 99 int32_t height_; 100 int32_t dataSpace_ = 0; 101 std::map<int32_t, std::string> param; 102 sptr<OHOS::IBufferProducer> producer_; 103 std::string surfaceId_ = ""; 104 sptr<Surface> surface_; 105 std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility_ = nullptr; 106 107 protected: 108 /* 109 * Prepare a capture id, return CAMERA_OK or CAMERA_CAPTURE_LIMIT_EXCEED 110 */ 111 virtual int32_t PrepareCaptureId() final; 112 virtual void ResetCaptureId() final; 113 GetStreamOperator()114 inline sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> GetStreamOperator() 115 { 116 std::lock_guard<std::mutex> lock(streamOperatorLock_); 117 if (streamOperatorOffline_ != nullptr) { 118 return streamOperatorOffline_; 119 } 120 return streamOperator_.promote(); 121 } 122 SetStreamOperator(wptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator)123 inline void SetStreamOperator(wptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator) 124 { 125 std::lock_guard<std::mutex> lock(streamOperatorLock_); 126 streamOperator_ = streamOperator; 127 } 128 129 std::mutex producerLock_; 130 std::mutex cameraAbilityLock_; 131 uint32_t callerToken_; 132 133 std::mutex streamOperatorLock_; 134 wptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator_ = nullptr; 135 sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperatorOffline_ = nullptr; 136 137 int32_t captureIdForConfirmCapture_ = CAPTURE_ID_UNSET; 138 139 private: 140 StreamType streamType_; 141 int32_t fwkStreamId_ = STREAM_ID_UNSET; 142 int32_t hdiStreamId_ = STREAM_ID_UNSET; 143 int32_t curCaptureID_ = CAPTURE_ID_UNSET; 144 std::mutex basicInfoLock_; 145 }; 146 } // namespace CameraStandard 147 } // namespace OHOS 148 #endif // OHOS_CAMERA_H_STREAM_COMMON_H 149