NVIDIA DeepStream SDK API Reference

9.1 Release
sources/includes/ds3d/common/hpp/yaml_config.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef DS3D_COMMON_HPP_YAML_CONFIG_HPP
19 #define DS3D_COMMON_HPP_YAML_CONFIG_HPP
20 
21 #include <ds3d/common/common.h>
22 #include <ds3d/common/idatatype.h>
23 #include <ds3d/common/type_trait.h>
24 #include <ds3d/common/config.h>
25 #include <yaml-cpp/yaml.h>
26 
27 #include <stdexcept>
28 #include <string>
29 
30 namespace ds3d { namespace config {
31 
32 ErrCode inline CatchYamlCall(std::function<ErrCode()> f)
33 {
34  ErrCode code = ErrCode::kGood;
35  DS3D_TRY { code = f(); }
36  DS3D_CATCH_ERROR(Exception, ErrCode::kConfig, "parse config error")
37  DS3D_CATCH_ERROR(std::runtime_error, ErrCode::kConfig, "parse config failed")
38  DS3D_CATCH_ERROR(std::exception, ErrCode::kConfig, "parse config failed")
39  DS3D_CATCH_ANY(ErrCode::kConfig, "parse config failed")
40  return code;
41 }
42 
43 template <class F, typename... Args>
44 ErrCode
45 CatchConfigCall(F f, Args&&... args)
46 {
47  ErrCode code = ErrCode::kGood;
48  DS3D_TRY { code = f(std::forward<Args>(args)...); }
49  DS3D_CATCH_ERROR(Exception, ErrCode::kConfig, "parse config error")
50  DS3D_CATCH_ERROR(std::runtime_error, ErrCode::kConfig, "parse config failed")
51  DS3D_CATCH_ERROR(std::exception, ErrCode::kConfig, "parse config failed")
52  DS3D_CATCH_ANY(ErrCode::kConfig, "parse config failed")
53  return code;
54 }
55 
56 inline ErrCode
57 parseComponentConfig(const std::string& yamlComp, const std::string& path, ComponentConfig& config)
58 {
59  config.filePath = path;
60 
61  YAML::Node node = YAML::Load(yamlComp);
62  config.name = node["name"].as<std::string>();
63  DS3D_FAILED_RETURN(!config.name.empty(), ErrCode::kConfig, "name not found in config");
64  std::string type = node["type"].as<std::string>();
65  DS3D_FAILED_RETURN(!type.empty(), ErrCode::kConfig, "component type must be specified");
66  config.type = componentType(type);
68  config.type != ComponentType::kNone, ErrCode::kConfig, "type not found in config");
69  auto inCapsNode = node["in_caps"];
70  if (inCapsNode) {
71  config.gstInCaps = inCapsNode.as<std::string>();
72  }
73  auto outCapsNode = node["out_caps"];
74  if (outCapsNode) {
75  config.gstOutCaps = outCapsNode.as<std::string>();
76  }
77  auto linkToNode = node["link_to"];
78  if (linkToNode) {
79  config.linkTo = linkToNode.as<std::string>();
80  }
81  auto linkFromNode = node["link_from"];
82  if (linkFromNode) {
83  config.linkFrom = linkFromNode.as<std::string>();
84  }
85 
86  auto withQueueNode = node["with_queue"];
87  if (withQueueNode) {
88  config.withQueue = withQueueNode.as<std::string>();
89  }
90 
91  if (node["custom_lib_path"]) {
92  config.customLibPath = node["custom_lib_path"].as<std::string>();
94  !config.customLibPath.empty(), ErrCode::kConfig, "custom_lib_path not found in config");
95  }
96  if (node["custom_create_function"]) {
97  config.customCreateFunction = node["custom_create_function"].as<std::string>();
99  !config.customCreateFunction.empty(), ErrCode::kConfig,
100  "custom_create_function not found in config");
101  }
102  //
103  auto bodyNode = node["config_body"];
104  if (bodyNode) {
105  // some components(e.g. lidarinfer) need how to figure out file path from config_path
106  if (!bodyNode["config_path"] && !path.empty()) {
107  bodyNode["config_path"] = path;
108  }
109  YAML::Emitter body;
110  body << bodyNode;
112  body.good(), ErrCode::kConfig,
113  "config_body error in config, yaml error: " + body.GetLastError());
114  config.configBody = body.c_str();
115  }
116 
117  YAML::Emitter yRawContent;
118  yRawContent << node;
119  config.rawContent = yRawContent.c_str();
120  return ErrCode::kGood;
121 }
122 
123 inline ErrCode
125  const std::string& yamlDoc, const std::string& path, std::vector<ComponentConfig>& all)
126 {
127  auto nodes = YAML::LoadAll(yamlDoc);
128  for (const auto& doc : nodes) {
129  if (!doc) {
130  continue;
131  }
132  YAML::Emitter content;
133  content << doc;
135  content.good(), ErrCode::kConfig,
136  "component error in config, yaml error: " + content.GetLastError());
137  ComponentConfig config;
139  parseComponentConfig(content.c_str(), path, config),
140  "parsing a component failed in config");
141  all.push_back(config);
142  }
143  return ErrCode::kGood;
144 }
145 
146 }} // namespace ds3d::config
147 
148 #endif // DS3D_COMMON_HPP_YAML_CONFIG_HPP
DS3D_TRY
#define DS3D_TRY
Definition: sources/includes/ds3d/common/defines.h:113
ds3d::config::ComponentConfig::type
ComponentType type
Definition: sources/includes/ds3d/common/config.h:40
ds3d::config::ComponentType::kNone
@ kNone
DS3D_FAILED_RETURN
#define DS3D_FAILED_RETURN(condition, ret, fmt,...)
Definition: sources/includes/ds3d/common/defines.h:69
ds3d::Exception
Definition: sources/includes/ds3d/common/common.h:148
ds3d::ErrCode::kGood
@ kGood
ds3d::config::ComponentConfig::gstInCaps
std::string gstInCaps
Definition: sources/includes/ds3d/common/config.h:41
ds3d::config::parseComponentConfig
ErrCode parseComponentConfig(const std::string &yamlComp, const std::string &path, ComponentConfig &config)
Definition: sources/includes/ds3d/common/hpp/yaml_config.hpp:57
ds3d::ErrCode
ErrCode
Definition: sources/includes/ds3d/common/common.h:47
ds3d::config::ComponentConfig::withQueue
std::string withQueue
Definition: sources/includes/ds3d/common/config.h:45
ds3d::config::ComponentConfig::linkFrom
std::string linkFrom
Definition: sources/includes/ds3d/common/config.h:44
ds3d::config::CatchYamlCall
ErrCode CatchYamlCall(std::function< ErrCode()> f)
Definition: sources/includes/ds3d/common/hpp/yaml_config.hpp:32
ds3d::ErrCode::kConfig
@ kConfig
DS3D_CATCH_ERROR
#define DS3D_CATCH_ERROR(type, errcode, fmt,...)
Definition: sources/includes/ds3d/common/defines.h:115
ds3d::config::ComponentConfig::configBody
std::string configBody
Definition: sources/includes/ds3d/common/config.h:48
ds3d::config::CatchConfigCall
ErrCode CatchConfigCall(F f, Args &&... args)
Definition: sources/includes/ds3d/common/hpp/yaml_config.hpp:45
ds3d::config::ComponentConfig::filePath
std::string filePath
Definition: sources/includes/ds3d/common/config.h:52
ds3d::config::ComponentConfig::customCreateFunction
std::string customCreateFunction
Definition: sources/includes/ds3d/common/config.h:47
ds3d::config::ComponentConfig::name
std::string name
Definition: sources/includes/ds3d/common/config.h:39
DS3D_CATCH_ANY
#define DS3D_CATCH_ANY(errcode, fmt,...)
Definition: sources/includes/ds3d/common/defines.h:122
ds3d::config::ComponentConfig::rawContent
std::string rawContent
Definition: sources/includes/ds3d/common/config.h:51
ds3d::config::ComponentConfig
Definition: sources/includes/ds3d/common/config.h:38
ds3d::config::ComponentConfig::gstOutCaps
std::string gstOutCaps
Definition: sources/includes/ds3d/common/config.h:42
DS3D_THROW_ERROR
#define DS3D_THROW_ERROR(statement, code, msg)
Definition: sources/includes/ds3d/common/defines.h:83
ds3d::config::componentType
ComponentType componentType(const std::string &strType)
Definition: sources/includes/ds3d/common/config.h:56
DS3D_ERROR_RETURN
#define DS3D_ERROR_RETURN(code, fmt,...)
Definition: sources/includes/ds3d/common/defines.h:77
ds3d
Definition: sources/includes/ds3d/common/abi_dataprocess.h:25
ds3d::config::parseFullConfig
ErrCode parseFullConfig(const std::string &yamlDoc, const std::string &path, std::vector< ComponentConfig > &all)
Definition: sources/includes/ds3d/common/hpp/yaml_config.hpp:124
ds3d::config::ComponentConfig::linkTo
std::string linkTo
Definition: sources/includes/ds3d/common/config.h:43
ds3d::config::ComponentConfig::customLibPath
std::string customLibPath
Definition: sources/includes/ds3d/common/config.h:46