NVIDIA DeepStream SDK API Reference

9.1 Release
sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp
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 DS3D_APP_DEEPSTREAM_3D_CONTEXT_APP_H
19 #define DS3D_APP_DEEPSTREAM_3D_CONTEXT_APP_H
20 
21 #include "gstnvdsmeta.h"
22 
23 // inlcude all ds3d hpp header files
24 #include <ds3d/common/hpp/dataloader.hpp>
25 #include <ds3d/common/hpp/datamap.hpp>
26 #include <ds3d/common/hpp/frame.hpp>
27 #include <ds3d/common/hpp/yaml_config.hpp>
28 
29 // inlucde nvds3d Gst header files
30 #include <ds3d/gst/nvds3d_gst_plugin.h>
31 #include <ds3d/gst/nvds3d_gst_ptr.h>
32 #include <ds3d/gst/nvds3d_meta.h>
33 #include <gst/gst.h>
34 
35 namespace ds3d { namespace app {
36 
38 public:
40  virtual ~Ds3dAppContext() { deinit(); }
41 
42  void setMainloop(GMainLoop* loop) { _mainLoop.reset(loop); }
43 
44  ErrCode init(const std::string& name)
45  {
48  _pipeline.reset(gst_pipeline_new(name.c_str()));
49  DS3D_FAILED_RETURN(pipeline(), ErrCode::kGst, "create pipeline: %s failed", name.c_str());
50  _pipeline.setName(name);
51  _bus.reset(gst_pipeline_get_bus(pipeline()));
52  DS3D_FAILED_RETURN(bus(), ErrCode::kGst, "get bus from pipeline: %s failed", name.c_str());
53  _busWatchId = gst_bus_add_watch(bus(), sBusCall, this);
54  return ErrCode::kGood;
55  }
56 
58  {
61  gst_bin_add(GST_BIN(pipeline()), ele.copy()), ErrCode::kGst, "add element failed");
62  _elementList.emplace_back(ele);
63  return *this;
64  }
65 
67  {
69  return setPipelineState(GST_STATE_PLAYING);
70  }
71 
72  virtual ErrCode stop()
73  {
75  ErrCode c = setPipelineState(GST_STATE_NULL);
76  if (!isGood(c)) {
77  LOG_WARNING("set pipeline state to GST_STATE_NULL failed");
78  }
79  if (!isGood(c)) {
80  LOG_WARNING("set pipeline state to GST_STATE_NULL failed");
81  }
82  GstState end = GST_STATE_NULL;
83  c = getState(_pipeline.get(), &end, nullptr, 3000);
84  if (!isGood(c) || end != GST_STATE_NULL) {
85  LOG_WARNING("waiting for pipeline state to null failed, force to quit");
86  }
87  for (auto& each : _elementList) {
88  if (each) {
89  c = setState(each.get(), GST_STATE_NULL);
90  }
91  }
92  return c;
93  }
94 
95  /* timeout: milliseconds, 0 means never timeout */
96  bool isRunning(size_t timeout = 0)
97  {
99  GstState state = GST_STATE_NULL;
100  GstState pending = GST_STATE_NULL;
101  if (gst_element_get_state(
102  GST_ELEMENT(pipeline()), &state, &pending,
103  (timeout ? timeout * 1000000 : GST_CLOCK_TIME_NONE)) == GST_STATE_CHANGE_FAILURE) {
104  return false;
105  }
106  if (state == GST_STATE_PLAYING || pending == GST_STATE_PLAYING) {
107  return true;
108  }
109  return false;
110  }
111 
113  {
114  if (mainLoop()) {
115  g_main_loop_quit(mainLoop());
116  }
117  }
118 
119  void runMainLoop()
120  {
121  if (mainLoop()) {
122  g_main_loop_run(mainLoop());
123  }
124  }
125 
126  virtual void deinit()
127  {
128  if (bus()) {
129  gst_bus_remove_watch(bus());
130  }
131  _bus.reset();
132  _pipeline.reset();
133  _elementList.clear();
134  _mainLoop.reset();
135  }
136 
138  {
140  gst_element_send_event(GST_ELEMENT(pipeline()), gst_event_new_eos()), ErrCode::kGst,
141  "send EOS failed");
142  return ErrCode::kGood;
143  }
144 
145  GstPipeline* pipeline() const { return GST_PIPELINE_CAST(_pipeline.get()); }
146  GstBus* bus() const { return _bus.get(); }
147  GMainLoop* mainLoop() const { return _mainLoop.get(); }
148 
149 private:
150  // no need to free msg
151  virtual bool busCall(GstMessage* msg) = 0;
152 
153 protected:
154  ErrCode setPipelineState(GstState state)
155  {
157  return setState(_pipeline.get(), state);
158  }
159 
160  ErrCode setState(GstElement* ele, GstState state)
161  {
162  DS_ASSERT(ele);
164  gst_element_set_state(ele, state) != GST_STATE_CHANGE_FAILURE, ErrCode::kGst,
165  "element set state: %d failed", state);
166  return ErrCode::kGood;
167  }
168  /* get element states. timeout in milliseconds.
169  */
171  GstElement* ele, GstState* state, GstState* pending = nullptr, size_t timeout = 0)
172  {
173  DS_ASSERT(ele);
174  GstStateChangeReturn ret = gst_element_get_state(
175  ele, state, pending, (timeout ? timeout * 1000000 : GST_CLOCK_TIME_NONE));
176  switch (ret) {
177  case GST_STATE_CHANGE_FAILURE:
178  return ErrCode::kGst;
179  case GST_STATE_CHANGE_SUCCESS:
180  case GST_STATE_CHANGE_NO_PREROLL:
181  return ErrCode::kGood;
182  default:
183  return ErrCode::kUnknown;
184  }
185  return ErrCode::kGood;
186  }
187 
188  static gboolean sBusCall(GstBus* bus, GstMessage* msg, gpointer data)
189  {
190  Ds3dAppContext* ctx = static_cast<Ds3dAppContext*>(data);
191  DS_ASSERT(ctx->bus() == bus);
192  return ctx->busCall(msg);
193  }
194 
195  // members
198  uint32_t _busWatchId = 0;
199  std::vector<gst::ElePtr> _elementList;
200  ds3d::UniqPtr<GMainLoop> _mainLoop{nullptr, g_main_loop_unref};
202 };
203 
204 }} // namespace ds3d::app
205 
206 #endif // DS3D_APP_DEEPSTREAM_3D_CONTEXT_APP_H
ds3d::isGood
bool isGood(ErrCode c)
Definition: sources/includes/ds3d/common/func_utils.h:32
ds3d::app::Ds3dAppContext::_bus
gst::BusPtr _bus
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:197
ds3d::app::Ds3dAppContext::sendEOS
ErrCode sendEOS()
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:137
ds3d::app::Ds3dAppContext::sBusCall
static gboolean sBusCall(GstBus *bus, GstMessage *msg, gpointer data)
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:188
ds3d::UniqPtr
std::unique_ptr< T, std::function< void(T *)> > UniqPtr
Definition: sources/includes/ds3d/common/hpp/obj.hpp:35
ds3d::ErrCode::kGst
@ kGst
ds3d::app::Ds3dAppContext::play
ErrCode play()
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:66
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::app::Ds3dAppContext::stop
virtual ErrCode stop()
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:72
ds3d::app::Ds3dAppContext::~Ds3dAppContext
virtual ~Ds3dAppContext()
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:40
ds3d::ErrCode::kGood
@ kGood
ds3d::app::Ds3dAppContext::setState
ErrCode setState(GstElement *ele, GstState state)
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:160
ds3d::gst::ElePtr
Definition: sources/libs/ds3d/gst/nvds3d_gst_ptr.h:154
ds3d::app::Ds3dAppContext::pipeline
GstPipeline * pipeline() const
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:145
ds3d::app::Ds3dAppContext::quitMainLoop
void quitMainLoop()
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:112
ds3d::app::Ds3dAppContext::_elementList
std::vector< gst::ElePtr > _elementList
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:199
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::GstPtr::setName
void setName(const std::string &name)
Definition: sources/libs/ds3d/gst/nvds3d_gst_ptr.h:72
ds3d::app::Ds3dAppContext::add
Ds3dAppContext & add(const gst::ElePtr &ele)
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:57
ds3d::app::Ds3dAppContext::DS3D_DISABLE_CLASS_COPY
DS3D_DISABLE_CLASS_COPY(Ds3dAppContext)
ds3d::app::Ds3dAppContext::init
ErrCode init(const std::string &name)
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:44
ds3d::gst::GstPtr::copy
GstObjT * copy() const
Definition: sources/libs/ds3d/gst/nvds3d_gst_ptr.h:102
ds3d::app::Ds3dAppContext::_busWatchId
uint32_t _busWatchId
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:198
ds3d::gst::GstPtr::get
GstObjT * get() const
Definition: sources/libs/ds3d/gst/nvds3d_gst_ptr.h:114
ds3d::app::Ds3dAppContext::setPipelineState
ErrCode setPipelineState(GstState state)
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:154
ds3d::app::Ds3dAppContext::isRunning
bool isRunning(size_t timeout=0)
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:96
LOG_WARNING
#define LOG_WARNING
Definition: sources/gst-plugins/gst-nvtracker/logging.h:24
ds3d::app::Ds3dAppContext::_pipeline
gst::ElePtr _pipeline
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:196
ds3d::app::Ds3dAppContext::bus
GstBus * bus() const
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:146
ds3d::app::Ds3dAppContext::Ds3dAppContext
Ds3dAppContext()
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:39
ds3d::app::Ds3dAppContext
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:37
ds3d::ErrCode::kUnknown
@ kUnknown
ds3d::app::Ds3dAppContext::_mainLoop
ds3d::UniqPtr< GMainLoop > _mainLoop
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:200
ds3d::app::Ds3dAppContext::runMainLoop
void runMainLoop()
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:119
ds3d::app::Ds3dAppContext::setMainloop
void setMainloop(GMainLoop *loop)
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:42
ds3d::app::Ds3dAppContext::getState
ErrCode getState(GstElement *ele, GstState *state, GstState *pending=nullptr, size_t timeout=0)
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:170
DS3D_THROW_ERROR
#define DS3D_THROW_ERROR(statement, code, msg)
Definition: sources/includes/ds3d/common/defines.h:83
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::app::Ds3dAppContext::deinit
virtual void deinit()
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:126
ds3d::app::Ds3dAppContext::mainLoop
GMainLoop * mainLoop() const
Definition: sources/apps/sample_apps/deepstream-3d-depth-camera/deepstream_3d_context.hpp:147