1 /* 2 * Copyright (c) 2021-2024 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 DISTRIBUTED_CAMERA_STREAM_OPERATOR_H 17 #define DISTRIBUTED_CAMERA_STREAM_OPERATOR_H 18 19 #include <map> 20 #include <set> 21 #include <vector> 22 23 #include "constants.h" 24 #include "dcamera.h" 25 #include "dcamera_stream.h" 26 #include "dmetadata_processor.h" 27 28 #include "json/json.h" 29 #include "v1_0/istream_operator.h" 30 #include "v1_0/types.h" 31 32 namespace OHOS { 33 namespace DistributedHardware { 34 using namespace std; 35 using namespace OHOS::HDI::Camera::V1_0; 36 class DCameraProvider; 37 class DStreamOperator : public IStreamOperator { 38 public: 39 explicit DStreamOperator(std::shared_ptr<DMetadataProcessor> &dMetadataProcessor); 40 DStreamOperator() = default; 41 ~DStreamOperator() override = default; 42 DStreamOperator(const DStreamOperator &other) = delete; 43 DStreamOperator(DStreamOperator &&other) = delete; 44 DStreamOperator& operator=(const DStreamOperator &other) = delete; 45 DStreamOperator& operator=(DStreamOperator &&other) = delete; 46 47 public: 48 int32_t IsStreamsSupported(OperationMode mode, const std::vector<uint8_t> &modeSetting, 49 const std::vector<StreamInfo> &infos, StreamSupportType &type) override; 50 int32_t CreateStreams(const std::vector<StreamInfo> &streamInfos) override; 51 int32_t ReleaseStreams(const std::vector<int32_t> &streamIds) override; 52 int32_t CommitStreams(OperationMode mode, const std::vector<uint8_t> &modeSetting) override; 53 int32_t GetStreamAttributes(std::vector<StreamAttribute> &attributes) override; 54 int32_t AttachBufferQueue(int32_t streamId, const sptr<BufferProducerSequenceable> &bufferProducer) override; 55 int32_t DetachBufferQueue(int32_t streamId) override; 56 int32_t Capture(int32_t captureId, const CaptureInfo &info, bool isStreaming) override; 57 int32_t CancelCapture(int32_t captureId) override; 58 int32_t ChangeToOfflineStream(const std::vector<int32_t> &streamIds, 59 const sptr<IStreamOperatorCallback> &callbackObj, sptr<IOfflineStreamOperator> &offlineOperator) override; 60 61 DCamRetCode InitOutputConfigurations(const DHBase &dhBase, const std::string &sinkAbilityInfo, 62 const std::string &sourceAbilityInfo); 63 std::vector<DCEncodeType> ParseEncoderTypes(Json::Value& rootValue); 64 DCamRetCode ParsePhotoFormats(Json::Value& rootValue); 65 DCamRetCode ParsePreviewFormats(Json::Value& rootValue); 66 DCamRetCode ParseVideoFormats(Json::Value& rootValue); 67 DCamRetCode AcquireBuffer(int streamId, DCameraBuffer &buffer); 68 DCamRetCode ShutterBuffer(int streamId, const DCameraBuffer &buffer); 69 DCamRetCode SetCallBack(OHOS::sptr<IStreamOperatorCallback> const &callback); 70 DCamRetCode SetDeviceCallback(function<void(ErrorType, int)> &errorCbk, 71 function<void(uint64_t, std::shared_ptr<OHOS::Camera::CameraMetadata>)> &resultCbk); 72 void Release(); 73 std::vector<int> GetStreamIds(); 74 75 private: 76 bool IsCapturing(); 77 void SetCapturing(bool isCapturing); 78 DCamRetCode NegotiateSuitableCaptureInfo(const CaptureInfo& srcCaptureInfo, bool isStreaming); 79 void ChooseSuitableFormat(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo, 80 std::shared_ptr<DCCaptureInfo> &captureInfo); 81 void ChooseSuitableResolution(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo, 82 std::shared_ptr<DCCaptureInfo> &captureInfo); 83 void ChooseSuitableDataSpace(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo, 84 std::shared_ptr<DCCaptureInfo> &captureInfo); 85 void ChooseSuitableEncodeType(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo, 86 std::shared_ptr<DCCaptureInfo> &captureInfo); 87 void ChooseSuitableStreamId(std::shared_ptr<DCCaptureInfo> &captureInfo); 88 void ConvertStreamInfo(const StreamInfo &srcInfo, std::shared_ptr<DCStreamInfo> &dstInfo); 89 DCEncodeType ConvertDCEncodeType(std::string &srcEncodeType); 90 std::shared_ptr<DCCaptureInfo> BuildSuitableCaptureInfo(const CaptureInfo& srcCaptureInfo, 91 std::vector<std::shared_ptr<DCStreamInfo>> &srcStreamInfo); 92 void SnapShotStreamOnCaptureEnded(int32_t captureId, int streamId); 93 bool HasContinuousCaptureInfo(int captureId); 94 int32_t ExtractStreamInfo(std::vector<DCStreamInfo>& dCameraStreams); 95 void ExtractCaptureInfo(std::vector<DCCaptureInfo> &captureInfos); 96 void ExtractCameraAttr(Json::Value &rootValue, std::vector<int>& formats, const std::string rootNode); 97 void GetCameraAttr(Json::Value &rootValue, std::string formatStr, const std::string rootNode, int format); 98 DCamRetCode GetInputCaptureInfo(const CaptureInfo& srcCaptureInfo, bool isStreaming, 99 std::shared_ptr<DCCaptureInfo>& inputCaptureInfo); 100 void AppendCaptureInfo(std::shared_ptr<DCCaptureInfo> &appendCaptureInfo, bool isStreaming, 101 std::shared_ptr<DCCaptureInfo> &inputCaptureInfo, const CaptureInfo& srcCaptureInfo); 102 int32_t HalStreamCommit(const DCStreamInfo &streamInfo); 103 104 std::shared_ptr<DCameraStream> FindHalStreamById(int32_t streamId); 105 void InsertHalStream(int32_t streamId, std::shared_ptr<DCameraStream>& dcStream); 106 void EraseHalStream(int32_t streamId); 107 108 std::shared_ptr<CaptureInfo> FindCaptureInfoById(int32_t captureId); 109 void InsertCaptureInfo(int captureId, std::shared_ptr<CaptureInfo>& captureInfo); 110 void EraseCaptureInfo(int32_t captureId); 111 int32_t FindCaptureIdByStreamId(int32_t streamId); 112 113 std::shared_ptr<DCStreamInfo> FindDCStreamById(int32_t streamId); 114 void InsertDCStream(int32_t streamId, std::shared_ptr<DCStreamInfo>& dcStreamInfo); 115 void EraseDCStream(int32_t streamId); 116 void ExtractNotCaptureStream(bool isStreaming, std::vector<std::shared_ptr<DCStreamInfo>>& appendStreamInfo); 117 118 bool FindEnableShutter(int32_t streamId); 119 void InsertEnableShutter(int32_t streamId, bool enableShutterCallback); 120 void EraseEnableShutter(int32_t streamId); 121 122 int32_t FindStreamCaptureBufferNum(const pair<int, int>& streamPair); 123 void AddStreamCaptureBufferNum(const pair<int, int>& streamPair); 124 void EraseStreamCaptureBufferNum(const pair<int, int>& streamPair); 125 126 bool IsStreamInfosInvalid(const std::vector<StreamInfo> &infos); 127 bool IsCaptureInfoInvalid(const CaptureInfo &info); 128 129 int32_t DoCapture(int32_t captureId, const CaptureInfo &info, bool isStreaming); 130 void InsertNotifyCaptureMap(int32_t captureId); 131 void EraseNotifyCaptureMap(int32_t captureId); 132 133 private: 134 constexpr static uint32_t JSON_ARRAY_MAX_SIZE = 1000; 135 std::shared_ptr<DMetadataProcessor> dMetadataProcessor_; 136 OHOS::sptr<IStreamOperatorCallback> dcStreamOperatorCallback_; 137 function<void(ErrorType, int)> errorCallback_; 138 139 DHBase dhBase_; 140 std::vector<DCEncodeType> dcSupportedCodecType_; 141 std::vector<DCEncodeType> sourceEncodeTypes_; 142 143 std::map<DCSceneType, std::vector<int>> dcSupportedFormatMap_; 144 std::map<int, std::vector<DCResolution>> dcSupportedPhotoResolutionMap_; 145 std::map<int, std::vector<DCResolution>> dcSupportedPreviewResolutionMap_; 146 std::map<int, std::vector<DCResolution>> dcSupportedVideoResolutionMap_; 147 148 std::map<int, std::shared_ptr<DCameraStream>> halStreamMap_; 149 std::map<int, std::shared_ptr<DCStreamInfo>> dcStreamInfoMap_; 150 std::map<int, std::shared_ptr<CaptureInfo>> halCaptureInfoMap_; 151 std::vector<std::shared_ptr<DCCaptureInfo>> cachedDCaptureInfoList_; 152 std::map<int, bool> enableShutterCbkMap_; 153 std::map<pair<int, int>, int> acceptedBufferNum_; 154 155 std::mutex streamAttrLock_; 156 std::mutex halStreamLock_; 157 bool isCapturing_ = false; 158 std::mutex isCapturingLock_; 159 OperationMode currentOperMode_ = OperationMode::NORMAL; 160 std::shared_ptr<OHOS::Camera::CameraMetadata> latestStreamSetting_; 161 std::map<int, bool> notifyCaptureStartedMap_; 162 }; 163 } // namespace DistributedHardware 164 } // namespace OHOS 165 #endif // DISTRIBUTED_CAMERA_STREAM_OPERATOR_H 166