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