NVIDIA DeepStream SDK API Reference

9.1 Release
sources/libs/ds3d/gst/nvds3d_gst_plugin.h
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 NVDS3D_GST_GST_PLUGINS_H
19 #define NVDS3D_GST_GST_PLUGINS_H
20 
21 #include <ds3d/common/func_utils.h>
22 #include <ds3d/common/config.h>
23 #include <gst/app/gstappsink.h>
24 #include <gst/app/gstappsrc.h>
25 
26 #include <ds3d/common/hpp/obj.hpp>
27 #include <ds3d/common/hpp/dataloader.hpp>
28 #include <ds3d/common/hpp/datarender.hpp>
29 #include <ds3d/common/hpp/datafilter.hpp>
30 #include <ds3d/common/hpp/yaml_config.hpp>
31 
32 #include <ds3d/gst/custom_lib_factory.h>
33 #include <ds3d/gst/nvds3d_gst_ptr.h>
34 #include <ds3d/gst/nvds3d_meta.h>
35 
36 namespace ds3d {
37 
38 constexpr uint32_t kDataProcessUserDataMagic = NVDS3D_MAGIC_ID('D', '3', 'U', 'D');
42  std::string configContent;
43  std::string configPath;
44 };
45 
46 } // namespace ds3d
47 
49 
51  GstAppSrc* src, ds3d::abiRefDataLoader* refLoader);
52 
54  GstAppSink* sink, ds3d::abiRefDataRender* refRender);
55 
57 
58 namespace ds3d { namespace gst {
59 
60 template<class GuardProcess>
63  GuardProcess customProcessor;
66 
67  DataProcessInfo() = default;
69  void reset()
70  {
71  gstElement.reset();
72  customProcessor.reset();
73  customlib.reset();
74  }
75 };
76 
79 
80 template <class GuardProcess>
81 inline ErrCode
83  const config::ComponentConfig& compConfig, GuardProcess& customProcessor,
85 {
86  using abiRefType = typename GuardProcess::abiRefType;
88  DS_ASSERT(customLib);
89  GuardProcess processor;
90  processor.reset(customLib->CreateCtx<abiRefType>(
91  compConfig.customLibPath, compConfig.customCreateFunction));
93  processor, ErrCode::kLoadLib, "create custom processor: %s from lib: %s failed",
94  componentTypeStr(compConfig.type), compConfig.customLibPath.c_str());
95 
96  // holder a customlib pointer to keep dataloader safe
98  uData->customlib = customLib.get();
99  uData->configContent = compConfig.rawContent;
100  uData->configPath = compConfig.filePath;
101  processor.setUserData(uData.get(), [holder = customLib, uData = uData](void*) mutable {
102  uData.reset();
103  if (holder && holder.use_count() == 1) {
104  holder->keepOpen(true);
105  }
106  holder.reset();
107  });
108  customProcessor = std::move(processor);
109  lib = std::move(customLib);
110  return ErrCode::kGood;
111 }
112 
122 inline ErrCode
124  const config::ComponentConfig& compConfig, DataLoaderSrc& loaderSrc, bool start)
125 {
126  GuardDataLoader loader;
128  loadCustomProcessor(compConfig, loader, loaderSrc.customlib),
129  "load custom dataloader failed");
130 
131  loaderSrc.config = compConfig;
132  loaderSrc.customProcessor = loader;
133  ElePtr loaderEle = elementMake("appsrc", compConfig.name);
134  DS3D_FAILED_RETURN(loaderEle, ErrCode::kGst, "create appsrc failed.");
135  // update loaderSrc appsrc
136  loaderSrc.gstElement = loaderEle;
137 
138  // set gst element properties
139  constexpr static size_t kPoolSize = 6;
140  std::string caps = compConfig.gstOutCaps.empty() ? loader.getOutputCaps()
141  : compConfig.gstOutCaps;
143  !caps.empty(), ErrCode::kConfig, "caps must be configure for dataloader source");
144  CapsPtr srcCaps(gst_caps_from_string(caps.c_str()));
145  DS3D_FAILED_RETURN(srcCaps, ErrCode::kGst, "gst_caps_from_string: %s failed", caps.c_str());
146  g_object_set(
147  G_OBJECT(loaderEle.get()), "do-timestamp", TRUE, "stream-type", GST_APP_STREAM_TYPE_STREAM,
148  "max-bytes", (uint64_t)(kPoolSize * sizeof(NvDs3DBuffer)), "min-percent", 80, "caps",
149  srcCaps.get(), NULL);
150 
151  if (start) {
152  DS3D_ERROR_RETURN(loader.start(compConfig.rawContent, compConfig.filePath),
153  "Dataloader start config: %s failed", compConfig.filePath.c_str());
154  }
155 
156  GstAppSrc* appSrc = GST_APP_SRC(loaderEle.get());
157  DS_ASSERT(appSrc);
159  NvDs3D_GstAppSrcSetDataloader(appSrc, loader.release()),
160  "Set dataloader into GstAppsrc failed.");
161 
162  return ErrCode::kGood;
163 }
164 
165 inline ErrCode
167  const config::ComponentConfig& compConfig, DataRenderSink& renderSink, bool start)
168 {
169  GuardDataRender render;
171  loadCustomProcessor(compConfig, render, renderSink.customlib),
172  "load custom datarender failed");
173 
174  renderSink.config = compConfig;
175  renderSink.customProcessor = render;
176  ElePtr renderEle = elementMake("appsink", compConfig.name);
177  DS3D_FAILED_RETURN(renderEle, ErrCode::kGst, "create appsink failed.");
178  // update renderSink appsink
179  renderSink.gstElement = renderEle;
180 
181  // set gst element properties
182  uint32_t maxBuffers = 4;
183  std::string caps = compConfig.gstInCaps.empty() ? render.getInputCaps()
184  : compConfig.gstInCaps;
186  !caps.empty(), ErrCode::kConfig, "caps must be configure for datarender source");
187  CapsPtr sinkCaps(gst_caps_from_string(caps.c_str()));
188  DS3D_FAILED_RETURN(sinkCaps, ErrCode::kGst, "gst_caps_from_string: %s failed", caps.c_str());
189  GObject* eleObj = G_OBJECT(renderEle.get());
190  g_object_set(
191  eleObj, "wait-on-eos", TRUE, "max-buffers", (uint32_t)maxBuffers, "caps", sinkCaps.get(),
192  nullptr);
193 
194  auto setGstProperties = [eleObj, &compConfig]() {
195  YAML::Node node = YAML::Load(compConfig.rawContent);
196  auto properties = node["gst_properties"];
197  if (properties) {
198  auto sync = properties["sync"];
199  auto async = properties["async"];
200  auto drop = properties["drop"];
201  if (sync) {
202  g_object_set(eleObj, "sync", sync.as<bool>(), nullptr);
203  }
204  if (async) {
205  g_object_set(eleObj, "async", async.as<bool>(), nullptr);
206  }
207  if (drop) {
208  g_object_set(eleObj, "drop", drop.as<bool>(), nullptr);
209  }
210  }
211  return ErrCode::kGood;
212  };
214  config::CatchYamlCall(setGstProperties), "parse gst_properties failed for datareander");
215 
216  if (start) {
217  DS3D_ERROR_RETURN(render.start(compConfig.rawContent, compConfig.filePath),
218  "Dataloader start config: %s failed", compConfig.filePath.c_str());
219  }
220 
221  GstAppSink* appSink = GST_APP_SINK(renderEle.get());
222  DS_ASSERT(appSink);
224  NvDs3D_GstAppSinkSetDataRender(appSink, render.release()),
225  "Set datarender into GstAppSink failed.");
226 
227  return ErrCode::kGood;
228 }
229 
230 }} // namespace ds3d::gst
231 
232 #endif // NVDS3D_GST_GST_PLUGINS_H
ds3d::DataProcessUserData::pluginMagicUD
uint32_t pluginMagicUD
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:40
ds3d::gst::DataProcessInfo::gstElement
ElePtr gstElement
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:62
DS3D_EXTERN_C_BEGIN
#define DS3D_EXTERN_C_BEGIN
Definition: sources/includes/ds3d/common/defines.h:130
ds3d::config::ComponentConfig::type
ComponentType type
Definition: sources/includes/ds3d/common/config.h:40
ds3d::gst::elementMake
ElePtr elementMake(const std::string &factoryName, const std::string &name="")
Definition: sources/libs/ds3d/gst/nvds3d_gst_ptr.h:194
ds3d::gst::DataProcessInfo::config
config::ComponentConfig config
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:64
ds3d::ErrCode::kGst
@ kGst
ds3d::gst::DataProcessInfo::~DataProcessInfo
~DataProcessInfo()
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:68
DS3D_FAILED_RETURN
#define DS3D_FAILED_RETURN(condition, ret, fmt,...)
Definition: sources/includes/ds3d/common/defines.h:69
ds3d::gst::GstPtr
Definition: sources/libs/ds3d/gst/nvds3d_gst_ptr.h:58
ds3d::gst::DataProcessInfo::DataProcessInfo
DataProcessInfo()=default
ds3d::GuardDataProcess::getInputCaps
std::string getInputCaps()
Definition: sources/includes/ds3d/common/hpp/dataprocess.hpp:105
ds3d::gst::NvDs3D_CreateDataRenderSink
ErrCode NvDs3D_CreateDataRenderSink(const config::ComponentConfig &compConfig, DataRenderSink &renderSink, bool start)
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:166
ds3d::ErrCode::kGood
@ kGood
ds3d::gst::loadCustomProcessor
ErrCode loadCustomProcessor(const config::ComponentConfig &compConfig, GuardProcess &customProcessor, Ptr< CustomLibFactory > &lib)
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:82
ds3d::DataProcessUserData::configPath
std::string configPath
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:43
ds3d::gst::DataProcessInfo
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:61
ds3d::GuardDataLoader
GuardDataLoader is the safe access entry for abiDataLoader.
Definition: sources/includes/ds3d/common/hpp/dataloader.hpp:50
ds3d::config::ComponentConfig::gstInCaps
std::string gstInCaps
Definition: sources/includes/ds3d/common/config.h:41
ds3d::gst::ElePtr
Definition: sources/libs/ds3d/gst/nvds3d_gst_ptr.h:154
ds3d::ErrCode::kLoadLib
@ kLoadLib
ds3d::DataProcessUserData
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:39
DS3D_EXPORT_API
#define DS3D_EXPORT_API
Definition: sources/includes/ds3d/common/defines.h:129
ds3d::abiRefT
Definition: sources/includes/ds3d/common/abi_obj.h:43
DS_ASSERT
#define DS_ASSERT(...)
Definition: sources/includes/ds3d/common/defines.h:36
ds3d::ErrCode
ErrCode
Definition: sources/includes/ds3d/common/common.h:47
NvDs3DBuffer
Definition: sources/libs/ds3d/gst/nvds3d_meta.h:35
NVDS3D_MAGIC_ID
#define NVDS3D_MAGIC_ID(a, b, c, d)
Definition: sources/libs/ds3d/gst/nvds3d_meta.h:28
ds3d::kDataProcessUserDataMagic
constexpr uint32_t kDataProcessUserDataMagic
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:38
ds3d::gst::GstPtr::get
GstObjT * get() const
Definition: sources/libs/ds3d/gst/nvds3d_gst_ptr.h:114
DS3D_EXTERN_C_END
#define DS3D_EXTERN_C_END
Definition: sources/includes/ds3d/common/defines.h:131
ds3d::DataProcessUserData::customlib
CustomLibFactory * customlib
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:41
ds3d::config::CatchYamlCall
ErrCode CatchYamlCall(std::function< ErrCode()> f)
Definition: sources/includes/ds3d/common/hpp/yaml_config.hpp:32
NvDs3D_GstAppSinkSetDataRender
DS3D_EXPORT_API ds3d::ErrCode NvDs3D_GstAppSinkSetDataRender(GstAppSink *sink, ds3d::abiRefDataRender *refRender)
ds3d::ErrCode::kConfig
@ kConfig
ds3d::config::componentTypeStr
const char * componentTypeStr(ComponentType type)
Definition: sources/includes/ds3d/common/config.h:76
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::gst::NvDs3D_CreateDataLoaderSrc
ErrCode NvDs3D_CreateDataLoaderSrc(const config::ComponentConfig &compConfig, DataLoaderSrc &loaderSrc, bool start)
Generate DataLoaderSrc from config file.
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:123
ds3d::config::ComponentConfig::name
std::string name
Definition: sources/includes/ds3d/common/config.h:39
ds3d::config::ComponentConfig::rawContent
std::string rawContent
Definition: sources/includes/ds3d/common/config.h:51
ds3d::Ptr
ShrdPtr< T > Ptr
Definition: sources/includes/ds3d/common/hpp/obj.hpp:37
ds3d::GuardRef::release
ref * release()
Definition: sources/includes/ds3d/common/hpp/obj.hpp:257
NvDs3D_GstAppSrcSetDataloader
DS3D_EXTERN_C_BEGIN DS3D_EXPORT_API ds3d::ErrCode NvDs3D_GstAppSrcSetDataloader(GstAppSrc *src, ds3d::abiRefDataLoader *refLoader)
ds3d::config::ComponentConfig
Definition: sources/includes/ds3d/common/config.h:38
ds3d::GuardDataRender
GuardDataRender is the safe access entry for abiDataRender.
Definition: sources/includes/ds3d/common/hpp/datarender.hpp:57
ds3d::GuardDataProcess::start
ErrCode start(const std::string &content, const std::string &path="")
Definition: sources/includes/ds3d/common/hpp/dataprocess.hpp:74
ds3d::config::ComponentConfig::gstOutCaps
std::string gstOutCaps
Definition: sources/includes/ds3d/common/config.h:42
DS3D_ERROR_RETURN
#define DS3D_ERROR_RETURN(code, fmt,...)
Definition: sources/includes/ds3d/common/defines.h:77
ds3d::DataProcessUserData::configContent
std::string configContent
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:42
ds3d::CustomLibFactory
Definition: sources/libs/ds3d/gst/custom_lib_factory.h:37
ds3d
Definition: sources/includes/ds3d/common/abi_dataprocess.h:25
ds3d::gst::GstPtr::reset
void reset(GstObjT *obj=nullptr, bool takeOwner=true)
Definition: sources/libs/ds3d/gst/nvds3d_gst_ptr.h:93
ds3d::GuardDataLoader::getOutputCaps
std::string getOutputCaps()
Definition: sources/includes/ds3d/common/hpp/dataloader.hpp:61
ds3d::gst::DataProcessInfo::customProcessor
GuardProcess customProcessor
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:63
ds3d::gst::DataProcessInfo::reset
void reset()
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:69
ds3d::gst::DataProcessInfo::customlib
Ptr< CustomLibFactory > customlib
Definition: sources/libs/ds3d/gst/nvds3d_gst_plugin.h:65
ds3d::config::ComponentConfig::customLibPath
std::string customLibPath
Definition: sources/includes/ds3d/common/config.h:46