NVIDIA DeepStream SDK API Reference

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