NVIDIA DeepStream SDK API Reference

6.4 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  holder.reset();
100  });
101  customProcessor = std::move(processor);
102  lib = std::move(customLib);
103  return ErrCode::kGood;
104 }
105 
115 inline ErrCode
117  const config::ComponentConfig& compConfig, DataLoaderSrc& loaderSrc, bool start)
118 {
119  GuardDataLoader loader;
121  loadCustomProcessor(compConfig, loader, loaderSrc.customlib),
122  "load custom dataloader failed");
123 
124  loaderSrc.config = compConfig;
125  loaderSrc.customProcessor = loader;
126  ElePtr loaderEle = elementMake("appsrc", compConfig.name);
127  DS3D_FAILED_RETURN(loaderEle, ErrCode::kGst, "create appsrc failed.");
128  // update loaderSrc appsrc
129  loaderSrc.gstElement = loaderEle;
130 
131  // set gst element properties
132  constexpr static size_t kPoolSize = 6;
133  std::string caps = compConfig.gstOutCaps.empty() ? loader.getOutputCaps()
134  : compConfig.gstOutCaps;
136  !caps.empty(), ErrCode::kConfig, "caps must be configure for dataloader source");
137  CapsPtr srcCaps(gst_caps_from_string(caps.c_str()));
138  DS3D_FAILED_RETURN(srcCaps, ErrCode::kGst, "gst_caps_from_string: %s failed", caps.c_str());
139  g_object_set(
140  G_OBJECT(loaderEle.get()), "do-timestamp", TRUE, "stream-type", GST_APP_STREAM_TYPE_STREAM,
141  "max-bytes", (uint64_t)(kPoolSize * sizeof(NvDs3DBuffer)), "min-percent", 80, "caps",
142  srcCaps.get(), NULL);
143 
144  if (start) {
145  DS3D_ERROR_RETURN(loader.start(compConfig.rawContent, compConfig.filePath),
146  "Dataloader start config: %s failed", compConfig.filePath.c_str());
147  }
148 
149  GstAppSrc* appSrc = GST_APP_SRC(loaderEle.get());
150  DS_ASSERT(appSrc);
152  NvDs3D_GstAppSrcSetDataloader(appSrc, loader.release()),
153  "Set dataloader into GstAppsrc failed.");
154 
155  return ErrCode::kGood;
156 }
157 
158 inline ErrCode
160  const config::ComponentConfig& compConfig, DataRenderSink& renderSink, bool start)
161 {
162  GuardDataRender render;
164  loadCustomProcessor(compConfig, render, renderSink.customlib),
165  "load custom datarender failed");
166 
167  renderSink.config = compConfig;
168  renderSink.customProcessor = render;
169  ElePtr renderEle = elementMake("appsink", compConfig.name);
170  DS3D_FAILED_RETURN(renderEle, ErrCode::kGst, "create appsink failed.");
171  // update renderSink appsink
172  renderSink.gstElement = renderEle;
173 
174  // set gst element properties
175  uint32_t maxBuffers = 4;
176  std::string caps = compConfig.gstInCaps.empty() ? render.getInputCaps()
177  : compConfig.gstInCaps;
179  !caps.empty(), ErrCode::kConfig, "caps must be configure for datarender source");
180  CapsPtr sinkCaps(gst_caps_from_string(caps.c_str()));
181  DS3D_FAILED_RETURN(sinkCaps, ErrCode::kGst, "gst_caps_from_string: %s failed", caps.c_str());
182  GObject* eleObj = G_OBJECT(renderEle.get());
183  g_object_set(
184  eleObj, "wait-on-eos", TRUE, "max-buffers", (uint32_t)maxBuffers, "caps", sinkCaps.get(),
185  nullptr);
186 
187  auto setGstProperties = [eleObj, &compConfig]() {
188  YAML::Node node = YAML::Load(compConfig.rawContent);
189  auto properties = node["gst_properties"];
190  if (properties) {
191  auto sync = properties["sync"];
192  auto async = properties["async"];
193  auto drop = properties["drop"];
194  if (sync) {
195  g_object_set(eleObj, "sync", sync.as<bool>(), nullptr);
196  }
197  if (async) {
198  g_object_set(eleObj, "async", async.as<bool>(), nullptr);
199  }
200  if (drop) {
201  g_object_set(eleObj, "drop", drop.as<bool>(), nullptr);
202  }
203  }
204  return ErrCode::kGood;
205  };
207  config::CatchYamlCall(setGstProperties), "parse gst_properties failed for datareander");
208 
209  if (start) {
210  DS3D_ERROR_RETURN(render.start(compConfig.rawContent, compConfig.filePath),
211  "Dataloader start config: %s failed", compConfig.filePath.c_str());
212  }
213 
214  GstAppSink* appSink = GST_APP_SINK(renderEle.get());
215  DS_ASSERT(appSink);
217  NvDs3D_GstAppSinkSetDataRender(appSink, render.release()),
218  "Set datarender into GstAppSink failed.");
219 
220  return ErrCode::kGood;
221 }
222 
223 }} // namespace ds3d::gst
224 
225 #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:159
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:116
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:33
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