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