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 #include "inode.h"
15 #include "stream_pipeline_builder.h"
16
17 namespace OHOS::Camera {
StreamPipelineBuilder(const std::shared_ptr<HostStreamMgr> & streamMgr,const std::shared_ptr<Pipeline> & p)18 StreamPipelineBuilder::StreamPipelineBuilder(const std::shared_ptr<HostStreamMgr>& streamMgr,
19 const std::shared_ptr<Pipeline>& p) : hostStreamMgr_(streamMgr), pipeline_(p)
20 {
21 }
22
SetMaxSize(const std::set<std::vector<int32_t>> & sizeSet)23 void StreamPipelineBuilder::SetMaxSize(const std::set<std::vector<int32_t>>& sizeSet)
24 {
25 std::vector<int32_t> max(2);
26 for (auto i : sizeSet) {
27 if (i[0] * i[1] > max[0] * max[1]) {
28 max[0] = i[0];
29 max[1] = i[1];
30 }
31 }
32 pipeline_->maxWidth_ = static_cast<uint32_t>(max[0]);
33 pipeline_->maxHeight_ = static_cast<uint32_t>(max[1]);
34 }
35
Build(const std::shared_ptr<PipelineSpec> & pipelineSpec,const std::string & cameraId)36 std::shared_ptr<Pipeline> StreamPipelineBuilder::Build(const std::shared_ptr<PipelineSpec>& pipelineSpec,
37 const std::string &cameraId)
38 {
39 CHECK_IF_PTR_NULL_RETURN_VALUE(pipelineSpec, nullptr);
40
41 CAMERA_LOGI("------------------------Node Instantiation Begin-------------\n");
42 RetCode re = RC_OK;
43 std::set<std::vector<int32_t>> sizeSet;
44 for (auto& it : pipelineSpec->nodeSpecSet_) {
45 if (it.status_ == "new") {
46 std::string nodeName;
47 size_t pos = it.name_.find_first_of('#');
48 nodeName = it.name_.substr(0, pos);
49 std::shared_ptr<INode> newNode = NodeFactory::Instance().CreateShared(nodeName, it.name_,
50 it.type_, cameraId);
51 if (newNode == nullptr) {
52 CAMERA_LOGI("create node failed! \n");
53 return nullptr;
54 }
55 std::optional<int32_t> typeId = GetTypeId(it.type_, G_STREAM_TABLE_PTR, G_STREAM_TABLE_SIZE);
56 if (typeId) {
57 newNode->SetCallBack(hostStreamMgr_->GetBufferCb(it.streamId_));
58 }
59 pipeline_->nodes_.push_back(newNode);
60 it.status_ = "remain";
61 for (const auto& portSpec : it.portSpecSet_) {
62 std::vector<int32_t> vectorSize;
63 vectorSize.push_back(portSpec.format_.w_);
64 vectorSize.push_back(portSpec.format_.h_);
65 sizeSet.insert(vectorSize);
66 auto peerNode = std::find_if(pipeline_->nodes_.begin(), pipeline_->nodes_.end(),
67 [portSpec](const std::shared_ptr<INode>& n) {
68 return n->GetName() == portSpec.info_.peerPortNodeName_;
69 });
70 if (peerNode != pipeline_->nodes_.end()) {
71 std::shared_ptr<IPort> peerPort = (*peerNode)->GetPort(portSpec.info_.peerPortName_);
72 re = peerPort->SetFormat(portSpec.format_);
73 CHECK_IF_NOT_EQUAL_RETURN_VALUE(re, RC_OK, nullptr);
74 std::shared_ptr<IPort> port = newNode->GetPort(portSpec.info_.name_);
75 re = port->SetFormat(portSpec.format_);
76 CHECK_IF_NOT_EQUAL_RETURN_VALUE(re, RC_OK, nullptr);
77 re = port->Connect(peerPort);
78 CHECK_IF_NOT_EQUAL_RETURN_VALUE(re, RC_OK, nullptr);
79 re = peerPort->Connect(port);
80 CHECK_IF_NOT_EQUAL_RETURN_VALUE(re, RC_OK, nullptr);
81 }
82 }
83 }
84 }
85
86 SetMaxSize(sizeSet);
87
88 CAMERA_LOGI("------------------------Node Instantiation End-------------\n");
89 return pipeline_;
90 }
91
Destroy(int streamId)92 RetCode StreamPipelineBuilder::Destroy(int streamId)
93 {
94 CHECK_IF_PTR_NULL_RETURN_VALUE(pipeline_, RC_ERROR);
95 (void)streamId;
96 pipeline_->nodes_.clear();
97 return RC_OK;
98 }
99
Create(const std::shared_ptr<HostStreamMgr> & streamMgr)100 std::unique_ptr<StreamPipelineBuilder> StreamPipelineBuilder::Create(const std::shared_ptr<HostStreamMgr>& streamMgr)
101 {
102 std::shared_ptr<Pipeline> p = std::make_shared<Pipeline>();
103 return std::make_unique<StreamPipelineBuilder>(streamMgr, p);
104 }
105 }
106