• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 &abilityInfo);
62     DCamRetCode ParsePhotoFormats(Json::Value& rootValue);
63     DCamRetCode ParsePreviewFormats(Json::Value& rootValue);
64     DCamRetCode ParseVideoFormats(Json::Value& rootValue);
65     DCamRetCode AcquireBuffer(int streamId, DCameraBuffer &buffer);
66     DCamRetCode ShutterBuffer(int streamId, const DCameraBuffer &buffer);
67     DCamRetCode SetCallBack(OHOS::sptr<IStreamOperatorCallback> const &callback);
68     DCamRetCode SetDeviceCallback(function<void(ErrorType, int)> &errorCbk,
69         function<void(uint64_t, std::shared_ptr<OHOS::Camera::CameraMetadata>)> &resultCbk);
70     void Release();
71     std::vector<int> GetStreamIds();
72 
73 private:
74     bool IsCapturing();
75     void SetCapturing(bool isCapturing);
76     DCamRetCode NegotiateSuitableCaptureInfo(const CaptureInfo& srcCaptureInfo, bool isStreaming);
77     void ChooseSuitableFormat(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo,
78         std::shared_ptr<DCCaptureInfo> &captureInfo);
79     void ChooseSuitableResolution(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo,
80         std::shared_ptr<DCCaptureInfo> &captureInfo);
81     void ChooseSuitableDataSpace(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo,
82         std::shared_ptr<DCCaptureInfo> &captureInfo);
83     void ChooseSuitableEncodeType(std::vector<std::shared_ptr<DCStreamInfo>> &streamInfo,
84         std::shared_ptr<DCCaptureInfo> &captureInfo);
85     void ChooseSuitableStreamId(std::shared_ptr<DCCaptureInfo> &captureInfo);
86     void ConvertStreamInfo(const StreamInfo &srcInfo, std::shared_ptr<DCStreamInfo> &dstInfo);
87     DCEncodeType ConvertDCEncodeType(std::string &srcEncodeType);
88     std::shared_ptr<DCCaptureInfo> BuildSuitableCaptureInfo(const CaptureInfo& srcCaptureInfo,
89         std::vector<std::shared_ptr<DCStreamInfo>> &srcStreamInfo);
90     void SnapShotStreamOnCaptureEnded(int32_t captureId, int streamId);
91     bool HasContinuousCaptureInfo(int captureId);
92     int32_t ExtractStreamInfo(std::vector<DCStreamInfo>& dCameraStreams);
93     void ExtractCaptureInfo(std::vector<DCCaptureInfo> &captureInfos);
94     void ExtractCameraAttr(Json::Value &rootValue, std::vector<int>& formats, const std::string rootNode);
95     void GetCameraAttr(Json::Value &rootValue, std::string formatStr, const std::string rootNode, int format);
96     DCamRetCode GetInputCaptureInfo(const CaptureInfo& srcCaptureInfo, bool isStreaming,
97         std::shared_ptr<DCCaptureInfo>& inputCaptureInfo);
98     void AppendCaptureInfo(std::shared_ptr<DCCaptureInfo> &appendCaptureInfo, bool isStreaming,
99         std::shared_ptr<DCCaptureInfo> &inputCaptureInfo, const CaptureInfo& srcCaptureInfo);
100     int32_t HalStreamCommit(const DCStreamInfo &streamInfo);
101 
102     std::shared_ptr<DCameraStream> FindHalStreamById(int32_t streamId);
103     void InsertHalStream(int32_t streamId, std::shared_ptr<DCameraStream>& dcStream);
104     void EraseHalStream(int32_t streamId);
105 
106     std::shared_ptr<CaptureInfo> FindCaptureInfoById(int32_t captureId);
107     void InsertCaptureInfo(int captureId, std::shared_ptr<CaptureInfo>& captureInfo);
108     void EraseCaptureInfo(int32_t captureId);
109     int32_t FindCaptureIdByStreamId(int32_t streamId);
110 
111     std::shared_ptr<DCStreamInfo> FindDCStreamById(int32_t streamId);
112     void InsertDCStream(int32_t streamId, std::shared_ptr<DCStreamInfo>& dcStreamInfo);
113     void EraseDCStream(int32_t streamId);
114     void ExtractNotCaptureStream(bool isStreaming, std::vector<std::shared_ptr<DCStreamInfo>>& appendStreamInfo);
115 
116     bool FindEnableShutter(int32_t streamId);
117     void InsertEnableShutter(int32_t streamId, bool enableShutterCallback);
118     void EraseEnableShutter(int32_t streamId);
119 
120     int32_t FindStreamCaptureBufferNum(const pair<int, int>& streamPair);
121     void AddStreamCaptureBufferNum(const pair<int, int>& streamPair);
122     void EraseStreamCaptureBufferNum(const pair<int, int>& streamPair);
123 
124     bool IsStreamInfosInvalid(const std::vector<StreamInfo> &infos);
125     bool IsCaptureInfoInvalid(const CaptureInfo &info);
126 
127     int32_t DoCapture(int32_t captureId, const CaptureInfo &info, bool isStreaming);
128     void InsertNotifyCaptureMap(int32_t captureId);
129     void EraseNotifyCaptureMap(int32_t captureId);
130 
131 private:
132     constexpr static uint32_t JSON_ARRAY_MAX_SIZE = 1000;
133     std::shared_ptr<DMetadataProcessor> dMetadataProcessor_;
134     OHOS::sptr<IStreamOperatorCallback> dcStreamOperatorCallback_;
135     function<void(ErrorType, int)> errorCallback_;
136 
137     DHBase dhBase_;
138     std::vector<DCEncodeType> dcSupportedCodecType_;
139     std::map<DCSceneType, std::vector<int>> dcSupportedFormatMap_;
140     std::map<int, std::vector<DCResolution>> dcSupportedPhotoResolutionMap_;
141     std::map<int, std::vector<DCResolution>> dcSupportedPreviewResolutionMap_;
142     std::map<int, std::vector<DCResolution>> dcSupportedVideoResolutionMap_;
143 
144     std::map<int, std::shared_ptr<DCameraStream>> halStreamMap_;
145     std::map<int, std::shared_ptr<DCStreamInfo>> dcStreamInfoMap_;
146     std::map<int, std::shared_ptr<CaptureInfo>> halCaptureInfoMap_;
147     std::vector<std::shared_ptr<DCCaptureInfo>> cachedDCaptureInfoList_;
148     std::map<int, bool> enableShutterCbkMap_;
149     std::map<pair<int, int>, int> acceptedBufferNum_;
150 
151     std::mutex streamAttrLock_;
152     std::mutex halStreamLock_;
153     bool isCapturing_ = false;
154     std::mutex isCapturingLock_;
155     OperationMode currentOperMode_ = OperationMode::NORMAL;
156     std::shared_ptr<OHOS::Camera::CameraMetadata> latestStreamSetting_;
157     std::map<int, bool> notifyCaptureStartedMap_;
158 };
159 } // namespace DistributedHardware
160 } // namespace OHOS
161 #endif // DISTRIBUTED_CAMERA_STREAM_OPERATOR_H
162