NVIDIA DeepStream SDK API Reference

6.4 Release
nvds3d_gst_ptr.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_NVDS3D_GST_H
15 #define NVDS3D_GST_NVDS3D_GST_H
16 
19 //#include <ds3d/common/hpp/datamap.hpp>
20 
21 #include <gst/gst.h>
22 #include <gst/gstminiobject.h>
23 #include <gst/gstobject.h>
24 
25 namespace ds3d { namespace gst {
26 
27 struct GstObjectFunc {
28  static gpointer ref(gpointer p)
29  {
30  DS_ASSERT(p);
31  return gst_object_ref(p);
32  }
33  static void unref(gpointer p)
34  {
35  if (p) {
36  return gst_object_unref(p);
37  }
38  }
39 };
40 
41 template<class GstMiniObjDerived>
43  static GstMiniObjDerived* ref(GstMiniObjDerived* p) {
44  return (GstMiniObjDerived*)gst_mini_object_ref(GST_MINI_OBJECT_CAST(p));
45  }
46  static void unref(GstMiniObjDerived* p) {
47  if (p) {
48  gst_mini_object_unref(GST_MINI_OBJECT_CAST(p));
49  }
50  }
51 };
52 
53 template <class GstObjT, class ObjFunc>
54 class GstPtr {
55 private:
56  std::shared_ptr<GstObjT> _gst_obj;
57  std::string _name;
58 
59 public:
60  GstPtr() = default;
61  GstPtr(GstObjT* obj, const std::string& name = "", bool takeOwner = true)
62  {
63  reset(obj, takeOwner);
64  setName(name);
65  }
66  virtual ~GstPtr() = default;
67 
68  void setName(const std::string& name) { _name = name; }
69 
70  GstPtr(GstPtr&& other)
71  {
72  _gst_obj = std::move(other._gst_obj);
73  _name = std::move(other._name);
74  }
75 
76  GstPtr(const GstPtr& other)
77  {
78  _gst_obj = other._gst_obj;
79  _name = other._name;
80  }
81 
82  GstPtr& operator=(const GstPtr& other)
83  {
84  _gst_obj = other._gst_obj;
85  _name = other._name;
86  return *this;
87  }
88 
89  void reset(GstObjT* obj = nullptr, bool takeOwner = true)
90  {
91  GstObjT* entity = obj;
92  if (!takeOwner && obj) {
93  entity = (GstObjT*)ObjFunc::ref(obj);
94  }
95  _gst_obj.reset(entity, ObjFunc::unref);
96  }
97 
98  GstObjT* copy() const
99  {
100  GstObjT* raw = get();
101  if (raw) {
102  raw = (GstObjT*)ObjFunc::ref(raw);
103  }
104  return raw;
105  }
106  const std::string& name() const { return _name; }
107 
108  operator GstObjT*() const { return get(); }
109 
110  GstObjT* get() const { return _gst_obj.get(); }
111  operator bool() const { return (bool)_gst_obj; }
112 };
113 
114 template <class GstObj>
116 template <class GstMiniObj>
118 
122 
123 class PadPtr : public GstObjPtr<GstPad> {
124 public:
125  PadPtr(GstPad* pad, bool takeOwner = true)
126  : GstObjPtr<GstPad>(pad, (GST_PAD_NAME(pad) ? GST_PAD_NAME(pad) : ""), takeOwner)
127  {
128  }
129  template <typename... Args>
130  PadPtr(Args&&... args) : GstObjPtr<GstPad>(std::forward<Args>(args)...)
131  {
132  }
133  ~PadPtr() = default;
134 
135  uint32_t addProbe(
136  GstPadProbeType mask, GstPadProbeCallback callback, gpointer udata,
137  GDestroyNotify destroyData)
138  {
139  DS_ASSERT(get());
140  return gst_pad_add_probe(get(), mask, callback, udata, destroyData);
141  }
142 
143  void removeProbe(uint32_t id)
144  {
145  DS_ASSERT(get());
146  gst_pad_remove_probe(get(), id);
147  }
148 };
149 
150 class ElePtr : public GstObjPtr<GstElement> {
151 public:
152  ElePtr(GstElement* ele, bool takeOwner = true)
153  : GstObjPtr<GstElement>(
154  ele, ((ele && GST_ELEMENT_NAME(ele)) ? GST_ELEMENT_NAME(ele) : ""), takeOwner)
155  {
156  }
157  template <typename... Args>
158  ElePtr(Args&&... args) : GstObjPtr<GstElement>(std::forward<Args>(args)...)
159  {
160  }
161  ~ElePtr() = default;
162 
163  PadPtr staticPad(const std::string& padName)
164  {
165  DS_ASSERT(get());
166  PadPtr pad(gst_element_get_static_pad(get(), padName.c_str()), true);
167  return pad;
168  }
169 
171  {
173  gst_element_link(get(), next.get()), ErrCode::kGst, "link element %s to %s failed",
174  name().c_str(), next.name().c_str());
175  return next;
176  }
177 
178  ElePtr& link(ElePtr& next, std::string& sinkPadName)
179  {
180  auto srcPad = gst_element_get_static_pad(get(), "src");
181  auto sinkPad = gst_element_request_pad_simple(next.get(), sinkPadName.c_str());
183  gst_pad_link(srcPad, sinkPad) == GST_PAD_LINK_OK, ErrCode::kGst, "link element %s[%s] to %s[%s] failed",
184  name().c_str(), "src", next.name().c_str(), sinkPadName.c_str());
185  return next;
186  }
187 };
188 
189 inline ElePtr
190 elementMake(const std::string& factoryName, const std::string& name = "")
191 {
192  GstElement* ele = gst_element_factory_make(factoryName.c_str(), name.c_str());
194  ele, nullptr, "create element: %s, name:%s failed.", factoryName.c_str(), name.c_str());
195  ElePtr ptr(ele, true);
196  DS_ASSERT(ele);
197  return ptr;
198 }
199 
200 class BinPtr : public ElePtr {
201 public:
202  template <typename... Args>
203  BinPtr(Args&&... args) : ElePtr(std::forward<Args>(args)...)
204  {
205  }
206  ~BinPtr() = default;
207 
208  BinPtr& pushBack(const ElePtr& element)
209  {
211  gst_bin_add(GST_BIN(get()), element.copy()), ErrCode::kGst,
212  "add element: %s to bin: %s failed", element.name().c_str(), name().c_str());
213  _list.push_back(element);
214  return *this;
215  }
216 
217  BinPtr& pushFront(const ElePtr& element)
218  {
220  gst_bin_add(GST_BIN(get()), element.copy()), ErrCode::kGst,
221  "add element: %s to bin: %s failed", element.name().c_str(), name().c_str());
222  _list.push_front(element);
223  return *this;
224  }
225 
226  ElePtr addSrcQueue(bool link = true, const ElePtr& back = nullptr)
227  {
228  ElePtr lastEle = back;
229  std::string lastEleName;
230  if (back) {
231  lastEleName = back.name();
232  } else if (!_list.empty()) {
233  lastEle = _list.back();
234  lastEleName = lastEle.name();
235  }
236  gst::ElePtr q(gst_element_factory_make("queue", (lastEleName + "_src_queue").c_str()));
237  DS_ASSERT(q);
238  pushBack(q);
239 
240  if (lastEle && link) {
241  lastEle.link(q);
242  }
243  return q;
244  }
245 
246  ElePtr addSinkQueue(bool link = true, const ElePtr& front = nullptr)
247  {
248  ElePtr firstEle = front;
249  std::string firstEleName;
250  if (front) {
251  firstEleName = front.name();
252  } else if (!_list.empty()) {
253  firstEle = _list.front();
254  firstEleName = firstEle.name();
255  }
256  gst::ElePtr q(gst_element_factory_make("queue", (firstEleName + "_sink_queue").c_str()));
257  DS_ASSERT(q);
258  pushFront(q);
259 
260  if (firstEle && link) {
261  q.link(firstEle);
262  }
263  return q;
264  }
265 
266  ErrCode addGhostSinkPad(const ElePtr& sinkEle = nullptr)
267  {
268  const char* padName = "sink";
269  ElePtr element = sinkEle;
270  if (!sinkEle && !_list.empty()) {
271  element = _list.front();
272  }
273  DS3D_FAILED_RETURN(element, ErrCode::kGst, "No sink element found in bin");
274  gst::PadPtr sinkPad = element.staticPad(padName);
275  DS_ASSERT(sinkPad);
277  gst_element_add_pad(get(), gst_ghost_pad_new(padName, sinkPad.get())), ErrCode::kGst,
278  "Failed to add ghost sink pad into bin");
279  return ErrCode::kGood;
280  }
281 
282  ErrCode addGhostSrcPad(const ElePtr& srcEle = nullptr)
283  {
284  const char* padName = "src";
285  ElePtr element = srcEle;
286  if (!srcEle && !_list.empty()) {
287  element = _list.back();
288  }
289  DS3D_FAILED_RETURN(element, ErrCode::kGst, "No src element found in bin");
290  gst::PadPtr srcPad = element.staticPad(padName);
291  DS_ASSERT(srcPad);
293  gst_element_add_pad(get(), gst_ghost_pad_new(padName, srcPad.get())), ErrCode::kGst,
294  "Failed to add ghost src pad into bin");
295  return ErrCode::kGood;
296  }
297 
298 private:
299  std::deque<ElePtr> _list;
300 };
301 
302 }} // namespace ds3d::gst
303 
304 #endif // NVDS3D_GST_NVDS3D_GST_H
ds3d::gst::BinPtr::addGhostSinkPad
ErrCode addGhostSinkPad(const ElePtr &sinkEle=nullptr)
Definition: nvds3d_gst_ptr.h:266
ds3d::gst::GstPtr::GstPtr
GstPtr(GstObjT *obj, const std::string &name="", bool takeOwner=true)
Definition: nvds3d_gst_ptr.h:61
ds3d::gst::ElePtr::link
ElePtr & link(ElePtr &next, std::string &sinkPadName)
Definition: nvds3d_gst_ptr.h:178
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::gst::GstPtr::operator=
GstPtr & operator=(const GstPtr &other)
Definition: nvds3d_gst_ptr.h:82
ds3d::gst::BinPtr::BinPtr
BinPtr(Args &&... args)
Definition: nvds3d_gst_ptr.h:203
ds3d::gst::BinPtr::addSrcQueue
ElePtr addSrcQueue(bool link=true, const ElePtr &back=nullptr)
Definition: nvds3d_gst_ptr.h:226
ds3d::gst::BinPtr::pushFront
BinPtr & pushFront(const ElePtr &element)
Definition: nvds3d_gst_ptr.h:217
ds3d::gst::ElePtr::staticPad
PadPtr staticPad(const std::string &padName)
Definition: nvds3d_gst_ptr.h:163
ds3d::gst::BinPtr::addSinkQueue
ElePtr addSinkQueue(bool link=true, const ElePtr &front=nullptr)
Definition: nvds3d_gst_ptr.h:246
ds3d::ErrCode::kGst
@ kGst
ds3d::gst::PadPtr::addProbe
uint32_t addProbe(GstPadProbeType mask, GstPadProbeCallback callback, gpointer udata, GDestroyNotify destroyData)
Definition: nvds3d_gst_ptr.h:135
ds3d::gst::PadPtr::PadPtr
PadPtr(GstPad *pad, bool takeOwner=true)
Definition: nvds3d_gst_ptr.h:125
ds3d::gst::GstPtr::GstPtr
GstPtr()=default
ds3d::gst::GstPtr
Definition: nvds3d_gst_ptr.h:54
ds3d::ErrCode::kGood
@ kGood
dataloader.hpp
ds3d::gst::ElePtr
Definition: nvds3d_gst_ptr.h:150
ds3d::gst::GstPtr::GstPtr
GstPtr(GstPtr &&other)
Definition: nvds3d_gst_ptr.h:70
ds3d::gst::BinPtr::~BinPtr
~BinPtr()=default
ds3d::gst::ElePtr::ElePtr
ElePtr(Args &&... args)
Definition: nvds3d_gst_ptr.h:158
ds3d::ErrCode
ErrCode
Definition: common.h:43
ds3d::gst::PadPtr::~PadPtr
~PadPtr()=default
datamap.hpp
ds3d::gst::GstPtr::setName
void setName(const std::string &name)
Definition: nvds3d_gst_ptr.h:68
ds3d::gst::GstObjectFunc::unref
static void unref(gpointer p)
Definition: nvds3d_gst_ptr.h:33
ds3d::gst::PadPtr
Definition: nvds3d_gst_ptr.h:123
ds3d::gst::BinPtr::addGhostSrcPad
ErrCode addGhostSrcPad(const ElePtr &srcEle=nullptr)
Definition: nvds3d_gst_ptr.h:282
ds3d::gst::GstPtr::name
const std::string & name() const
Definition: nvds3d_gst_ptr.h:106
ds3d::gst::ElePtr::ElePtr
ElePtr(GstElement *ele, bool takeOwner=true)
Definition: nvds3d_gst_ptr.h:152
ds3d::gst::GstPtr::copy
GstObjT * copy() const
Definition: nvds3d_gst_ptr.h:98
ds3d::gst::GstPtr::get
GstObjT * get() const
Definition: nvds3d_gst_ptr.h:110
ds3d::gst::GstMiniObjectFunc::unref
static void unref(GstMiniObjDerived *p)
Definition: nvds3d_gst_ptr.h:46
ds3d::gst::PadPtr::removeProbe
void removeProbe(uint32_t id)
Definition: nvds3d_gst_ptr.h:143
ds3d::gst::GstPtr::GstPtr
GstPtr(const GstPtr &other)
Definition: nvds3d_gst_ptr.h:76
ds3d::gst::ElePtr::link
ElePtr & link(ElePtr &next)
Definition: nvds3d_gst_ptr.h:170
ds3d::gst::BinPtr
Definition: nvds3d_gst_ptr.h:200
ds3d::gst::PadPtr::PadPtr
PadPtr(Args &&... args)
Definition: nvds3d_gst_ptr.h:130
ds3d::gst::GstMiniObjectFunc::ref
static GstMiniObjDerived * ref(GstMiniObjDerived *p)
Definition: nvds3d_gst_ptr.h:43
ds3d::gst::GstObjectFunc
Definition: nvds3d_gst_ptr.h:27
DS3D_THROW_ERROR_FMT
#define DS3D_THROW_ERROR_FMT(statement, code, fmt,...)
Definition: defines.h:83
DS3D_FAILED_RETURN
#define DS3D_FAILED_RETURN(condition, ret, fmt,...)
Definition: defines.h:64
ds3d::gst::BinPtr::pushBack
BinPtr & pushBack(const ElePtr &element)
Definition: nvds3d_gst_ptr.h:208
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::gst::GstMiniObjectFunc
Definition: nvds3d_gst_ptr.h:42
ds3d::gst::ElePtr::~ElePtr
~ElePtr()=default
ds3d::gst::GstPtr::~GstPtr
virtual ~GstPtr()=default
ds3d::gst::GstObjectFunc::ref
static gpointer ref(gpointer p)
Definition: nvds3d_gst_ptr.h:28