NVIDIA DeepStream SDK API Reference

6.4 Release
deepstream_3d_context.hpp
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: MIT
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef DS3D_APP_DEEPSTREAM_3D_CONTEXT_APP_H
25 #define DS3D_APP_DEEPSTREAM_3D_CONTEXT_APP_H
26 
27 #include "gstnvdsmeta.h"
28 
29 // inlcude all ds3d hpp header files
34 
35 // inlucde nvds3d Gst header files
38 #include <ds3d/gst/nvds3d_meta.h>
39 #include <gst/gst.h>
40 
41 namespace ds3d { namespace app {
42 
44 public:
46  virtual ~Ds3dAppContext() { deinit(); }
47 
48  void setMainloop(GMainLoop* loop) { _mainLoop.reset(loop); }
49 
50  ErrCode init(const std::string& name)
51  {
54  _pipeline.reset(gst_pipeline_new(name.c_str()));
55  DS3D_FAILED_RETURN(pipeline(), ErrCode::kGst, "create pipeline: %s failed", name.c_str());
56  _pipeline.setName(name);
57  _bus.reset(gst_pipeline_get_bus(pipeline()));
58  DS3D_FAILED_RETURN(bus(), ErrCode::kGst, "get bus from pipeline: %s failed", name.c_str());
59  _busWatchId = gst_bus_add_watch(bus(), sBusCall, this);
60  return ErrCode::kGood;
61  }
62 
64  {
67  gst_bin_add(GST_BIN(pipeline()), ele.copy()), ErrCode::kGst, "add element failed");
68  _elementList.emplace_back(ele);
69  return *this;
70  }
71 
73  {
75  return setPipelineState(GST_STATE_PLAYING);
76  }
77 
78  virtual ErrCode stop()
79  {
81  ErrCode c = setPipelineState(GST_STATE_NULL);
82  if (!isGood(c)) {
83  LOG_WARNING("set pipeline state to GST_STATE_NULL failed");
84  }
85  if (!isGood(c)) {
86  LOG_WARNING("set pipeline state to GST_STATE_NULL failed");
87  }
88  GstState end = GST_STATE_NULL;
89  c = getState(_pipeline.get(), &end, nullptr, 3000);
90  if (!isGood(c) || end != GST_STATE_NULL) {
91  LOG_WARNING("waiting for pipeline state to null failed, force to quit");
92  }
93  for (auto& each : _elementList) {
94  if (each) {
95  c = setState(each.get(), GST_STATE_NULL);
96  }
97  }
98  return c;
99  }
100 
101  /* timeout: milliseconds, 0 means never timeout */
102  bool isRunning(size_t timeout = 0)
103  {
104  DS_ASSERT(pipeline());
105  GstState state = GST_STATE_NULL;
106  GstState pending = GST_STATE_NULL;
107  if (gst_element_get_state(
108  GST_ELEMENT(pipeline()), &state, &pending,
109  (timeout ? timeout * 1000000 : GST_CLOCK_TIME_NONE)) == GST_STATE_CHANGE_FAILURE) {
110  return false;
111  }
112  if (state == GST_STATE_PLAYING || pending == GST_STATE_PLAYING) {
113  return true;
114  }
115  return false;
116  }
117 
119  {
120  if (mainLoop()) {
121  g_main_loop_quit(mainLoop());
122  }
123  }
124 
125  void runMainLoop()
126  {
127  if (mainLoop()) {
128  g_main_loop_run(mainLoop());
129  }
130  }
131 
132  virtual void deinit()
133  {
134  if (bus()) {
135  gst_bus_remove_watch(bus());
136  }
137  _bus.reset();
138  _pipeline.reset();
139  _elementList.clear();
140  _mainLoop.reset();
141  }
142 
144  {
146  gst_element_send_event(GST_ELEMENT(pipeline()), gst_event_new_eos()), ErrCode::kGst,
147  "send EOS failed");
148  return ErrCode::kGood;
149  }
150 
151  GstPipeline* pipeline() const { return GST_PIPELINE_CAST(_pipeline.get()); }
152  GstBus* bus() const { return _bus.get(); }
153  GMainLoop* mainLoop() const { return _mainLoop.get(); }
154 
155 private:
156  // no need to free msg
157  virtual bool busCall(GstMessage* msg) = 0;
158 
159 protected:
160  ErrCode setPipelineState(GstState state)
161  {
163  return setState(_pipeline.get(), state);
164  }
165 
166  ErrCode setState(GstElement* ele, GstState state)
167  {
168  DS_ASSERT(ele);
170  gst_element_set_state(ele, state) != GST_STATE_CHANGE_FAILURE, ErrCode::kGst,
171  "element set state: %d failed", state);
172  return ErrCode::kGood;
173  }
174  /* get element states. timeout in milliseconds.
175  */
177  GstElement* ele, GstState* state, GstState* pending = nullptr, size_t timeout = 0)
178  {
179  DS_ASSERT(ele);
180  GstStateChangeReturn ret = gst_element_get_state(
181  ele, state, pending, (timeout ? timeout * 1000000 : GST_CLOCK_TIME_NONE));
182  switch (ret) {
183  case GST_STATE_CHANGE_FAILURE:
184  return ErrCode::kGst;
185  case GST_STATE_CHANGE_SUCCESS:
186  case GST_STATE_CHANGE_NO_PREROLL:
187  return ErrCode::kGood;
188  default:
189  return ErrCode::kUnknown;
190  }
191  return ErrCode::kGood;
192  }
193 
194  static gboolean sBusCall(GstBus* bus, GstMessage* msg, gpointer data)
195  {
196  Ds3dAppContext* ctx = static_cast<Ds3dAppContext*>(data);
197  DS_ASSERT(ctx->bus() == bus);
198  return ctx->busCall(msg);
199  }
200 
201  // members
204  uint32_t _busWatchId = 0;
205  std::vector<gst::ElePtr> _elementList;
206  ds3d::UniqPtr<GMainLoop> _mainLoop{nullptr, g_main_loop_unref};
208 };
209 
210 }} // namespace ds3d::app
211 
212 #endif // DS3D_APP_DEEPSTREAM_3D_CONTEXT_APP_H
ds3d::isGood
bool isGood(ErrCode c)
Definition: func_utils.h:28
yaml_config.hpp
ds3d::app::Ds3dAppContext::sendEOS
ErrCode sendEOS()
Definition: deepstream_3d_context.hpp:143
ds3d::app::Ds3dAppContext::sBusCall
static gboolean sBusCall(GstBus *bus, GstMessage *msg, gpointer data)
Definition: deepstream_3d_context.hpp:194
nvds3d_gst_plugin.h
ds3d::UniqPtr
std::unique_ptr< T, std::function< void(T *)> > UniqPtr
Definition: obj.hpp:31
DS_ASSERT
#define DS_ASSERT(...)
Definition: defines.h:31
ds3d::app::Ds3dAppContext::_mainLoop
ds3d::UniqPtr< GMainLoop > _mainLoop
Definition: deepstream_3d_context.hpp:206
LOG_WARNING
#define LOG_WARNING
Definition: logging.h:18
ds3d::ErrCode::kGst
@ kGst
ds3d::app::Ds3dAppContext::play
ErrCode play()
Definition: deepstream_3d_context.hpp:72
ds3d::gst::GstPtr
Definition: nvds3d_gst_ptr.h:54
ds3d::app::Ds3dAppContext::stop
virtual ErrCode stop()
Definition: deepstream_3d_context.hpp:78
ds3d::app::Ds3dAppContext::~Ds3dAppContext
virtual ~Ds3dAppContext()
Definition: deepstream_3d_context.hpp:46
ds3d::ErrCode::kGood
@ kGood
ds3d::app::Ds3dAppContext::setState
ErrCode setState(GstElement *ele, GstState state)
Definition: deepstream_3d_context.hpp:166
dataloader.hpp
ds3d::gst::ElePtr
Definition: nvds3d_gst_ptr.h:150
ds3d::app::Ds3dAppContext::_busWatchId
uint32_t _busWatchId
Definition: deepstream_3d_context.hpp:204
ds3d::app::Ds3dAppContext::pipeline
GstPipeline * pipeline() const
Definition: deepstream_3d_context.hpp:151
ds3d::app::Ds3dAppContext::quitMainLoop
void quitMainLoop()
Definition: deepstream_3d_context.hpp:118
ds3d::ErrCode
ErrCode
Definition: common.h:43
nvds3d_gst_ptr.h
datamap.hpp
ds3d::gst::GstPtr::setName
void setName(const std::string &name)
Definition: nvds3d_gst_ptr.h:68
ds3d::app::Ds3dAppContext::add
Ds3dAppContext & add(const gst::ElePtr &ele)
Definition: deepstream_3d_context.hpp:63
ds3d::app::Ds3dAppContext::DS3D_DISABLE_CLASS_COPY
DS3D_DISABLE_CLASS_COPY(Ds3dAppContext)
frame.hpp
ds3d::app::Ds3dAppContext::init
ErrCode init(const std::string &name)
Definition: deepstream_3d_context.hpp:50
gstnvdsmeta.h
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::app::Ds3dAppContext::setPipelineState
ErrCode setPipelineState(GstState state)
Definition: deepstream_3d_context.hpp:160
ds3d::app::Ds3dAppContext::isRunning
bool isRunning(size_t timeout=0)
Definition: deepstream_3d_context.hpp:102
nvds3d_meta.h
ds3d::app::Ds3dAppContext::_bus
gst::BusPtr _bus
Definition: deepstream_3d_context.hpp:203
ds3d::app::Ds3dAppContext::bus
GstBus * bus() const
Definition: deepstream_3d_context.hpp:152
DS3D_THROW_ERROR
#define DS3D_THROW_ERROR(statement, code, msg)
Definition: defines.h:78
ds3d::app::Ds3dAppContext::_elementList
std::vector< gst::ElePtr > _elementList
Definition: deepstream_3d_context.hpp:205
ds3d::app::Ds3dAppContext::Ds3dAppContext
Ds3dAppContext()
Definition: deepstream_3d_context.hpp:45
ds3d::app::Ds3dAppContext::_pipeline
gst::ElePtr _pipeline
Definition: deepstream_3d_context.hpp:202
ds3d::app::Ds3dAppContext
Definition: deepstream_3d_context.hpp:43
ds3d::ErrCode::kUnknown
@ kUnknown
ds3d::app::Ds3dAppContext::runMainLoop
void runMainLoop()
Definition: deepstream_3d_context.hpp:125
DS3D_FAILED_RETURN
#define DS3D_FAILED_RETURN(condition, ret, fmt,...)
Definition: defines.h:64
ds3d::app::Ds3dAppContext::setMainloop
void setMainloop(GMainLoop *loop)
Definition: deepstream_3d_context.hpp:48
ds3d::app::Ds3dAppContext::getState
ErrCode getState(GstElement *ele, GstState *state, GstState *pending=nullptr, size_t timeout=0)
Definition: deepstream_3d_context.hpp:176
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::app::Ds3dAppContext::deinit
virtual void deinit()
Definition: deepstream_3d_context.hpp:132
ds3d::app::Ds3dAppContext::mainLoop
GMainLoop * mainLoop() const
Definition: deepstream_3d_context.hpp:153