NVIDIA DeepStream SDK API Reference

7.0 Release
nvds3d_gst_plugin.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
13 
14 #ifndef NVDS3D_GST_GST_PLUGINS_H
15 #define NVDS3D_GST_GST_PLUGINS_H
16 
17 #include <ds3d/common/func_utils.h>
18 #include <ds3d/common/config.h>
19 #include <gst/app/gstappsink.h>
20 #include <gst/app/gstappsrc.h>
21 
22 #include <ds3d/common/hpp/obj.hpp>
27 
30 #include <ds3d/gst/nvds3d_meta.h>
31 
32 namespace ds3d {
33 
34 constexpr uint32_t kDataProcessUserDataMagic = NVDS3D_MAGIC_ID('D', '3', 'U', 'D');
38  std::string configContent;
39  std::string configPath;
40 };
41 
42 } // namespace ds3d
43 
45 
47  GstAppSrc* src, ds3d::abiRefDataLoader* refLoader);
48 
50  GstAppSink* sink, ds3d::abiRefDataRender* refRender);
51 
53 
54 namespace ds3d { namespace gst {
55 
56 template<class GuardProcess>
59  GuardProcess customProcessor;
62 
63  DataProcessInfo() = default;
65  void reset()
66  {
67  gstElement.reset();
68  customProcessor.reset();
69  customlib.reset();
70  }
71 };
72 
75 
76 template <class GuardProcess>
77 inline ErrCode
79  const config::ComponentConfig& compConfig, GuardProcess& customProcessor,
81 {
82  using abiRefType = typename GuardProcess::abiRefType;
84  DS_ASSERT(customLib);
85  GuardProcess processor;
86  processor.reset(customLib->CreateCtx<abiRefType>(
87  compConfig.customLibPath, compConfig.customCreateFunction));
89  processor, ErrCode::kLoadLib, "create custom processor: %s from lib: %s failed",
90  componentTypeStr(compConfig.type), compConfig.customLibPath.c_str());
91 
92  // holder a customlib pointer to keep dataloader safe
94  uData->customlib = customLib.get();
95  uData->configContent = compConfig.rawContent;
96  uData->configPath = compConfig.filePath;
97  processor.setUserData(uData.get(), [holder = customLib, uData = uData](void*) mutable {
98  uData.reset();
99  if (holder && holder.use_count() == 1) {
100  holder->keepOpen(true);
101  }
102  holder.reset();
103  });
104  customProcessor = std::move(processor);
105  lib = std::move(customLib);
106  return ErrCode::kGood;
107 }
108 
118 inline ErrCode
120  const config::ComponentConfig& compConfig, DataLoaderSrc& loaderSrc, bool start)
121 {
122  GuardDataLoader loader;
124  loadCustomProcessor(compConfig, loader, loaderSrc.customlib),
125  "load custom dataloader failed");
126 
127  loaderSrc.config = compConfig;
128  loaderSrc.customProcessor = loader;
129  ElePtr loaderEle = elementMake("appsrc", compConfig.name);
130  DS3D_FAILED_RETURN(loaderEle, ErrCode::kGst, "create appsrc failed.");
131  // update loaderSrc appsrc
132  loaderSrc.gstElement = loaderEle;
133 
134  // set gst element properties
135  constexpr static size_t kPoolSize = 6;
136  std::string caps = compConfig.gstOutCaps.empty() ? loader.getOutputCaps()
137  : compConfig.gstOutCaps;
139  !caps.empty(), ErrCode::kConfig, "caps must be configure for dataloader source");
140  CapsPtr srcCaps(gst_caps_from_string(caps.c_str()));
141  DS3D_FAILED_RETURN(srcCaps, ErrCode::kGst, "gst_caps_from_string: %s failed", caps.c_str());
142  g_object_set(
143  G_OBJECT(loaderEle.get()), "do-timestamp", TRUE, "stream-type", GST_APP_STREAM_TYPE_STREAM,
144  "max-bytes", (uint64_t)(kPoolSize * sizeof(NvDs3DBuffer)), "min-percent", 80, "caps",
145  srcCaps.get(), NULL);
146 
147  if (start) {
148  DS3D_ERROR_RETURN(loader.start(compConfig.rawContent, compConfig.filePath),
149  "Dataloader start config: %s failed", compConfig.filePath.c_str());
150  }
151 
152  GstAppSrc* appSrc = GST_APP_SRC(loaderEle.get());
153  DS_ASSERT(appSrc);
155  NvDs3D_GstAppSrcSetDataloader(appSrc, loader.release()),
156  "Set dataloader into GstAppsrc failed.");
157 
158  return ErrCode::kGood;
159 }
160 
161 inline ErrCode
163  const config::ComponentConfig& compConfig, DataRenderSink& renderSink, bool start)
164 {
165  GuardDataRender render;
167  loadCustomProcessor(compConfig, render, renderSink.customlib),
168  "load custom datarender failed");
169 
170  renderSink.config = compConfig;
171  renderSink.customProcessor = render;
172  ElePtr renderEle = elementMake("appsink", compConfig.name);
173  DS3D_FAILED_RETURN(renderEle, ErrCode::kGst, "create appsink failed.");
174  // update renderSink appsink
175  renderSink.gstElement = renderEle;
176 
177  // set gst element properties
178  uint32_t maxBuffers = 4;
179  std::string caps = compConfig.gstInCaps.empty() ? render.getInputCaps()
180  : compConfig.gstInCaps;
182  !caps.empty(), ErrCode::kConfig, "caps must be configure for datarender source");
183  CapsPtr sinkCaps(gst_caps_from_string(caps.c_str()));
184  DS3D_FAILED_RETURN(sinkCaps, ErrCode::kGst, "gst_caps_from_string: %s failed", caps.c_str());
185  GObject* eleObj = G_OBJECT(renderEle.get());
186  g_object_set(
187  eleObj, "wait-on-eos", TRUE, "max-buffers", (uint32_t)maxBuffers, "caps", sinkCaps.get(),
188  nullptr);
189 
190  auto setGstProperties = [eleObj, &compConfig]() {
191  YAML::Node node = YAML::Load(compConfig.rawContent);
192  auto properties = node["gst_properties"];
193  if (properties) {
194  auto sync = properties["sync"];
195  auto async = properties["async"];
196  auto drop = properties["drop"];
197  if (sync) {
198  g_object_set(eleObj, "sync", sync.as<bool>(), nullptr);
199  }
200  if (async) {
201  g_object_set(eleObj, "async", async.as<bool>(), nullptr);
202  }
203  if (drop) {
204  g_object_set(eleObj, "drop", drop.as<bool>(), nullptr);
205  }
206  }
207  return ErrCode::kGood;
208  };
210  config::CatchYamlCall(setGstProperties), "parse gst_properties failed for datareander");
211 
212  if (start) {
213  DS3D_ERROR_RETURN(render.start(compConfig.rawContent, compConfig.filePath),
214  "Dataloader start config: %s failed", compConfig.filePath.c_str());
215  }
216 
217  GstAppSink* appSink = GST_APP_SINK(renderEle.get());
218  DS_ASSERT(appSink);
220  NvDs3D_GstAppSinkSetDataRender(appSink, render.release()),
221  "Set datarender into GstAppSink failed.");
222 
223  return ErrCode::kGood;
224 }
225 
226 }} // namespace ds3d::gst
227 
228 #endif // NVDS3D_GST_GST_PLUGINS_H
ds3d::DataProcessUserData::pluginMagicUD
uint32_t pluginMagicUD
Definition: nvds3d_gst_plugin.h:36
yaml_config.hpp
ds3d::gst::DataProcessInfo::gstElement
ElePtr gstElement
Definition: nvds3d_gst_plugin.h:58
ds3d::config::ComponentConfig::type
ComponentType type
Definition: includes/ds3d/common/config.h:36
NvDs3D_GstAppSrcSetDataloader
DS3D_EXTERN_C_BEGIN DS3D_EXPORT_API ds3d::ErrCode NvDs3D_GstAppSrcSetDataloader(GstAppSrc *src, ds3d::abiRefDataLoader *refLoader)
DS_ASSERT
#define DS_ASSERT(...)
Definition: defines.h:31
ds3d::gst::elementMake
ElePtr elementMake(const std::string &factoryName, const std::string &name="")
Definition: nvds3d_gst_ptr.h:190
ds3d::config::componentTypeStr
const char * componentTypeStr(ComponentType type)
Definition: includes/ds3d/common/config.h:72
ds3d::gst::DataProcessInfo::config
config::ComponentConfig config
Definition: nvds3d_gst_plugin.h:60
ds3d::ErrCode::kGst
@ kGst
ds3d::gst::DataProcessInfo::~DataProcessInfo
~DataProcessInfo()
Definition: nvds3d_gst_plugin.h:64
ds3d::gst::GstPtr
Definition: nvds3d_gst_ptr.h:54
ds3d::gst::DataProcessInfo::DataProcessInfo
DataProcessInfo()=default
ds3d::GuardDataProcess::getInputCaps
std::string getInputCaps()
Definition: dataprocess.hpp:101
DS3D_EXTERN_C_BEGIN
#define DS3D_EXTERN_C_BEGIN
Definition: defines.h:125
ds3d::gst::NvDs3D_CreateDataRenderSink
ErrCode NvDs3D_CreateDataRenderSink(const config::ComponentConfig &compConfig, DataRenderSink &renderSink, bool start)
Definition: nvds3d_gst_plugin.h:162
ds3d::ErrCode::kGood
@ kGood
ds3d::gst::loadCustomProcessor
ErrCode loadCustomProcessor(const config::ComponentConfig &compConfig, GuardProcess &customProcessor, Ptr< CustomLibFactory > &lib)
Definition: nvds3d_gst_plugin.h:78
ds3d::DataProcessUserData::configPath
std::string configPath
Definition: nvds3d_gst_plugin.h:39
ds3d::gst::DataProcessInfo
Definition: nvds3d_gst_plugin.h:57
ds3d::GuardDataLoader
GuardDataLoader is the safe access entry for abiDataLoader.
Definition: dataloader.hpp:46
dataloader.hpp
ds3d::config::ComponentConfig::gstInCaps
std::string gstInCaps
Definition: includes/ds3d/common/config.h:37
datafilter.hpp
ds3d::gst::ElePtr
Definition: nvds3d_gst_ptr.h:150
datarender.hpp
ds3d::ErrCode::kLoadLib
@ kLoadLib
ds3d::DataProcessUserData
Definition: nvds3d_gst_plugin.h:35
ds3d::abiRefT< abiDataLoader >
custom_lib_factory.h
ds3d::ErrCode
ErrCode
Definition: common.h:43
nvds3d_gst_ptr.h
NvDs3DBuffer
Definition: nvds3d_meta.h:31
NvDs3D_GstAppSinkSetDataRender
DS3D_EXPORT_API ds3d::ErrCode NvDs3D_GstAppSinkSetDataRender(GstAppSink *sink, ds3d::abiRefDataRender *refRender)
obj.hpp
ds3d::kDataProcessUserDataMagic
constexpr uint32_t kDataProcessUserDataMagic
Definition: nvds3d_gst_plugin.h:34
DS3D_EXTERN_C_END
#define DS3D_EXTERN_C_END
Definition: defines.h:126
ds3d::gst::GstPtr::get
GstObjT * get() const
Definition: nvds3d_gst_ptr.h:110
config.h
DS3D_ERROR_RETURN
#define DS3D_ERROR_RETURN(code, fmt,...)
Definition: defines.h:72
NVDS3D_MAGIC_ID
#define NVDS3D_MAGIC_ID(a, b, c, d)
Definition: nvds3d_meta.h:24
ds3d::config::CatchYamlCall
ErrCode CatchYamlCall(std::function< ErrCode()> f)
Definition: yaml_config.hpp:28
nvds3d_meta.h
ds3d::ErrCode::kConfig
@ kConfig
ds3d::config::ComponentConfig::filePath
std::string filePath
Definition: includes/ds3d/common/config.h:48
ds3d::config::ComponentConfig::customCreateFunction
std::string customCreateFunction
Definition: includes/ds3d/common/config.h:43
ds3d::gst::NvDs3D_CreateDataLoaderSrc
ErrCode NvDs3D_CreateDataLoaderSrc(const config::ComponentConfig &compConfig, DataLoaderSrc &loaderSrc, bool start)
Generate DataLoaderSrc from config file.
Definition: nvds3d_gst_plugin.h:119
ds3d::config::ComponentConfig::name
std::string name
Definition: includes/ds3d/common/config.h:35
ds3d::DataProcessUserData::customlib
CustomLibFactory * customlib
Definition: nvds3d_gst_plugin.h:37
ds3d::config::ComponentConfig::rawContent
std::string rawContent
Definition: includes/ds3d/common/config.h:47
ds3d::Ptr
ShrdPtr< T > Ptr
Definition: obj.hpp:33
ds3d::GuardRef::release
ref * release()
Definition: obj.hpp:253
DS3D_EXPORT_API
#define DS3D_EXPORT_API
Definition: defines.h:124
ds3d::config::ComponentConfig
Definition: includes/ds3d/common/config.h:34
ds3d::GuardDataRender
GuardDataRender is the safe access entry for abiDataRender.
Definition: datarender.hpp:53
func_utils.h
ds3d::GuardDataProcess::start
ErrCode start(const std::string &content, const std::string &path="")
Definition: dataprocess.hpp:70
ds3d::config::ComponentConfig::gstOutCaps
std::string gstOutCaps
Definition: includes/ds3d/common/config.h:38
DS3D_FAILED_RETURN
#define DS3D_FAILED_RETURN(condition, ret, fmt,...)
Definition: defines.h:64
ds3d::gst::DataProcessInfo::customlib
Ptr< CustomLibFactory > customlib
Definition: nvds3d_gst_plugin.h:61
ds3d::DataProcessUserData::configContent
std::string configContent
Definition: nvds3d_gst_plugin.h:38
ds3d::CustomLibFactory
Definition: custom_lib_factory.h:32
ds3d
Definition: lidar_3d_datatype.h:35
ds3d::gst::GstPtr::reset
void reset(GstObjT *obj=nullptr, bool takeOwner=true)
Definition: nvds3d_gst_ptr.h:89
ds3d::GuardDataLoader::getOutputCaps
std::string getOutputCaps()
Definition: dataloader.hpp:57
ds3d::gst::DataProcessInfo::customProcessor
GuardProcess customProcessor
Definition: nvds3d_gst_plugin.h:59
ds3d::gst::DataProcessInfo::reset
void reset()
Definition: nvds3d_gst_plugin.h:65
ds3d::config::ComponentConfig::customLibPath
std::string customLibPath
Definition: includes/ds3d/common/config.h:42