1 /*
2  * Copyright (c) 2021 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  *     http://www.apache.org/licenses/LICENSE-2.0
7  * Unless required by applicable law or agreed to in writing, software
8  * distributed under the License is distributed on an "AS IS" BASIS,
9  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10  * See the License for the specific language governing permissions and
11  * limitations under the License.
12  */
13 
14 #ifndef HOS_CAMERA_SOURCE_NODE_H
15 #define HOS_CAMERA_SOURCE_NODE_H
16 
17 #include "camera.h"
18 #include "node_base.h"
19 #include "utils.h"
20 #include <vector>
21 
22 namespace OHOS::Camera {
23 constexpr uint32_t LOOP_MAX_COUNT = 10;
24 class SourceNode : virtual public NodeBase {
25 public:
26     SourceNode(const std::string& name, const std::string& type, const std::string &cameraId);
27     ~SourceNode() override;
28     RetCode Init(const int32_t streamId) override;
29     RetCode Start(const int32_t streamId) override;
30     RetCode Flush(const int32_t streamId) override;
31     RetCode Stop(const int32_t streamId) override;
32     RetCode Capture(const int32_t streamId, const int32_t captureId) override;
33     RetCode CancelCapture(const int32_t streamId) override;
34     RetCode Config(const int32_t streamId, const CaptureMeta& meta) override;
35     void DeliverBuffer(std::shared_ptr<IBuffer>& buffer) override;
36     RetCode ProvideBuffers(std::shared_ptr<FrameSpec> frameSpec) override;
37 
38     virtual void OnPackBuffer(std::shared_ptr<FrameSpec> frameSpec);
39     virtual void SetBufferCallback();
40 
41 protected:
42     class PortHandler {
43     public:
44         PortHandler() = default;
45         ~PortHandler();
46         explicit PortHandler(std::shared_ptr<IPort>& p, bool isResize);
47         RetCode StartCollectBuffers();
48         RetCode StopCollectBuffers();
49         RetCode StartDistributeBuffers();
50         RetCode StopDistributeBuffers();
51         RetCode CollectorJoin();
52         RetCode DistributorJoin();
53         void OnBuffer(std::shared_ptr<IBuffer>& buffer);
54         void setWideAndHigh(int32_t wide, int32_t high);
55 
56     private:
57         void CollectBuffers();
58         void DistributeBuffers();
59         void FlushBuffers();
60 
61     private:
62         std::shared_ptr<IPort> port = nullptr;
63 
64         bool cltRun = false;
65         std::unique_ptr<std::thread> collector = nullptr;
66 
67         std::atomic<bool> dbtRun = false;
68         std::unique_ptr<std::thread> distributor = nullptr;
69 
70         std::shared_ptr<IBufferPool> pool = nullptr;
71 
72         std::condition_variable rbcv;
73         std::mutex rblock;
74         std::list<std::shared_ptr<IBuffer>> respondBufferList = {};
75         std::mutex cltLock;
76         int count_ = 0;
77         std::map<int32_t, uint8_t*> cBuffer;
78         bool isResize_ = false;
79         int32_t maxWide_ = 0;
80         int32_t maxHigh_ = 0;
81         uint32_t loopErrorCount_ = 0;
82     };
83 
84     std::mutex hndl_ = {};
85     std::unordered_map<int32_t, std::shared_ptr<PortHandler>> handler_ = {};
86 
87     std::mutex requestLock_;
88     std::unordered_map<int32_t, std::list<int32_t>> captureRequests_ = {};
89     std::string cameraIds_;
90     bool isAdjust_ = false;
91 };
92 } // namespace OHOS::Camera
93 #endif
94