NVIDIA DeepStream SDK API Reference

8.0 Release
pipeline.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2025 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 
22 #ifndef PIPELINE_HPP
23 #define PIPELINE_HPP
24 
25 #include <string>
26 #include <memory>
27 #include <thread>
28 
29 #include "object.hpp"
30 #include "element.hpp"
31 #include "buffer_probe.hpp"
32 #include "signal_handler.hpp"
33 
34 namespace deepstream
35 {
36 
46 class Pipeline : public Object {
47  public:
48 
49  typedef enum {
55  } State;
56 
63  class Message {
64  public:
65  virtual ~Message() {}
66  uint32_t type() { return type_; }
67  protected:
68  uint32_t type_;
69 
70  Message(uint32_t type): type_(type) {}
71  };
72 
76  class EOSMessage : public Message {
77  public:
78  EOSMessage(void*);
79  };
80 
84  class DynamicSourceMessage : public Message {
85  public:
86  DynamicSourceMessage(void*);
87 
88  inline bool isSourceAdded() const { return source_added_; }
89  inline uint32_t getSourceId() const { return source_id_; }
90  inline const std::string& getSensorId() const { return sensor_id_; }
91  inline const std::string& getSensorName() const { return sensor_name_; }
92  inline const std::string& getUri() const { return uri_; }
93 
94  protected:
95  bool source_added_ = false;
96  uint32_t source_id_ = 0;
97  std::string sensor_id_;
98  std::string sensor_name_;
99  std::string uri_;
100  };
101 
106  public:
107  StateTransitionMessage(void*);
108  inline const std::string& getName() const { return name_; };
109  inline void getState(State& old_state, State& new_state) const {
110  old_state = old_state_;
111  new_state = new_state_;
112  }
113  protected:
116  std::string name_;
117  };
118 
119  Pipeline(const char* name);
120 
122  Pipeline(const char* name, const std::string& config_file);
123 
125  virtual ~Pipeline();
126 
128  template<typename... Args>
129  Pipeline& add(const std::string& element_type, const std::string& element_name, const Args&... args) {
130  Element element = Element(element_type, element_name);
131  if constexpr (sizeof...(args) > 0) {
132  element.set(args...);
133  }
134  return this->add(element);
135  }
136 
138  Pipeline& add(Element element);
139 
141  Element* find(const::std::string& name);
142 
144  Element& operator[](const std::string& name) {
145  Element* e = this->find(name);
146  if (e == nullptr) {
147  throw std::runtime_error(name + " Not found");
148  }
149  return *e;
150  }
151 
158  Pipeline& link(
159  std::pair<std::string, std::string> route,
160  std::pair<std::string, std::string> tips
161  );
162 
164  template <typename... Args>
165  Pipeline &link(const std::string &arg1, const std::string arg2, const Args &...args)
166  {
167  link(std::make_pair(arg1, arg2), std::make_pair("", ""));
168  if constexpr (sizeof...(args) > 0) {
169  return this->link(arg2, args...);
170  } else {
171  return *this;
172  }
173  }
174 
185  Pipeline& attach(const std::string& elmenent_name, CustomObject* object, const std::string tip="");
186 
198  Pipeline& attach(
199  const std::string& element_name,
200  const std::string& plugin_name,
201  const std::string& object_name,
202  const std::string tip="");
203 
205  template<typename... Args>
207  const std::string& element_name,
208  const std::string& plugin_name,
209  const std::string& object_name,
210  const std::string tip,
211  const Args&... args) {
212  attach(element_name, plugin_name, object_name, tip);
213  if (sizeof...(args) > 0) {
214  auto& element = (*this)[element_name];
215  if (auto handler = element.getSignalHandler(object_name)) {
216  handler->set(args...);
217  } else if (auto probe = element.getProbe(object_name)) {
218  probe->set(args...);
219  }
220  }
221  return *this;
222  }
223 
225  Pipeline& install(std::function<void(Pipeline&, int key)> keyboard_listener) {
226  keyboard_listener_ = keyboard_listener;
227  return *this;
228  }
229 
231  int prepare();
233  int prepare(std::function<void(Pipeline &, const Message &)> listener);
234 
236  Pipeline& start();
238  Pipeline& start(std::function<void(Pipeline&, const Message&)> listener);
240  Pipeline &activate();
242  Pipeline& wait();
243 
245  Pipeline& stop();
246 
247  bool isRunning() const;
248 
250  Pipeline& pause();
252  Pipeline& resume();
254  Pipeline& seek(uint64_t timestamp);
255 
257  uint32_t startRTSP(uint16_t rtsp_port, uint16_t udp_port, uint32_t buffer_size=0);
258 
259  void handleKey(int key);
260 
261 protected:
262  int run();
263  int run_after_prepare();
264 
265  // This is GMainLoop*
266  void *loop_ = NULL;
267  // gstreamer bus watch id
268  uint bus_watch_id_ = 0;
269  // gstreamer bus data
270  void *bus_data_ = NULL;
271  std::function<void(Pipeline&, int key)> keyboard_listener_;
272 
273  // Thread
274  std::thread thread_;
275  // Hold all the element references here
276  std::map<std::string, Element> elements_;
277  // Hold the signal emitters for action control
278  std::map<std::string, std::unique_ptr<SignalEmitter>> action_owners_;
279  // start timestamp for the whole pipeline
280  uint64_t start_pts_ = 0;
281 };
282 
283 } // namespace deepstream
284 
285 #endif
buffer_probe.hpp
deepstream::Pipeline::StateTransitionMessage
Pipeline message on state transition.
Definition: pipeline.hpp:105
deepstream::Pipeline::EMPTY
@ EMPTY
Definition: pipeline.hpp:51
deepstream::Object::Pipeline
friend class Pipeline
Definition: object.hpp:204
deepstream::Pipeline::isRunning
bool isRunning() const
signal_handler.hpp
deepstream::CustomObject
Base class for all the custom objects.
Definition: custom_object.hpp:34
deepstream::Pipeline::StateTransitionMessage::getName
const std::string & getName() const
Definition: pipeline.hpp:108
deepstream::Pipeline::bus_watch_id_
uint bus_watch_id_
Definition: pipeline.hpp:268
deepstream::Object::set
Object & set(const YAML::Node &params)
Set the properties from key/value pairs in the yaml format.
deepstream::Pipeline::thread_
std::thread thread_
Definition: pipeline.hpp:274
deepstream::Element
Element class definition.
Definition: element.hpp:49
deepstream::Pipeline::keyboard_listener_
std::function< void(Pipeline &, int key)> keyboard_listener_
Definition: pipeline.hpp:271
deepstream::Pipeline::prepare
int prepare()
Intialize the pipeline.
deepstream::Pipeline::add
Pipeline & add(const std::string &element_type, const std::string &element_name, const Args &... args)
Template function for creating and adding element with properties.
Definition: pipeline.hpp:129
deepstream::Pipeline::StateTransitionMessage::old_state_
State old_state_
Definition: pipeline.hpp:114
deepstream::Pipeline::StateTransitionMessage::StateTransitionMessage
StateTransitionMessage(void *)
deepstream::Pipeline::DynamicSourceMessage::getSensorName
const std::string & getSensorName() const
Definition: pipeline.hpp:91
element.hpp
deepstream::Pipeline
Pipeline class definition.
Definition: pipeline.hpp:46
deepstream::Pipeline::Message::type
uint32_t type()
Definition: pipeline.hpp:66
deepstream::Pipeline::install
Pipeline & install(std::function< void(Pipeline &, int key)> keyboard_listener)
install a callback to capture keyboard events
Definition: pipeline.hpp:225
deepstream::Pipeline::PLAYING
@ PLAYING
Definition: pipeline.hpp:54
deepstream::Pipeline::DynamicSourceMessage::getSensorId
const std::string & getSensorId() const
Definition: pipeline.hpp:90
deepstream::Pipeline::action_owners_
std::map< std::string, std::unique_ptr< SignalEmitter > > action_owners_
Definition: pipeline.hpp:278
deepstream::Pipeline::DynamicSourceMessage::sensor_id_
std::string sensor_id_
Definition: pipeline.hpp:97
deepstream::Pipeline::StateTransitionMessage::getState
void getState(State &old_state, State &new_state) const
Definition: pipeline.hpp:109
deepstream::Pipeline::loop_
void * loop_
Definition: pipeline.hpp:266
deepstream::Pipeline::link
Pipeline & link(std::pair< std::string, std::string > route, std::pair< std::string, std::string > tips)
Link two elements within the pipeline.
deepstream::Pipeline::bus_data_
void * bus_data_
Definition: pipeline.hpp:270
deepstream::Pipeline::READY
@ READY
Definition: pipeline.hpp:52
deepstream::Pipeline::State
State
Definition: pipeline.hpp:49
deepstream::Pipeline::attach
Pipeline & attach(const std::string &elmenent_name, CustomObject *object, const std::string tip="")
Attach a custom object to an element within the pipeline.
deepstream::Pipeline::start_pts_
uint64_t start_pts_
Definition: pipeline.hpp:280
deepstream::Pipeline::DynamicSourceMessage::getSourceId
uint32_t getSourceId() const
Definition: pipeline.hpp:89
deepstream::Pipeline::DynamicSourceMessage::uri_
std::string uri_
Definition: pipeline.hpp:99
deepstream::Pipeline::DynamicSourceMessage::source_added_
bool source_added_
Definition: pipeline.hpp:95
deepstream::Pipeline::operator[]
Element & operator[](const std::string &name)
Operator for accessing elements in a pipeline.
Definition: pipeline.hpp:144
deepstream::Pipeline::DynamicSourceMessage::source_id_
uint32_t source_id_
Definition: pipeline.hpp:96
deepstream::Pipeline::Message
base class for pipeline message
Definition: pipeline.hpp:63
deepstream
Definition: buffer.hpp:33
deepstream::Pipeline::run_after_prepare
int run_after_prepare()
deepstream::Pipeline::Message::type_
uint32_t type_
Definition: pipeline.hpp:68
deepstream::Pipeline::find
Element * find(const ::std::string &name)
Find an element within the pipeline by name.
deepstream::Pipeline::wait
Pipeline & wait()
Wait until the pipeline ends.
deepstream::Object::Element
friend class Element
Definition: object.hpp:205
deepstream::Pipeline::startRTSP
uint32_t startRTSP(uint16_t rtsp_port, uint16_t udp_port, uint32_t buffer_size=0)
Start an RTSP server.
deepstream::Pipeline::DynamicSourceMessage::sensor_name_
std::string sensor_name_
Definition: pipeline.hpp:98
deepstream::Pipeline::~Pipeline
virtual ~Pipeline()
Destructor.
deepstream::Pipeline::handleKey
void handleKey(int key)
deepstream::Pipeline::INVALID
@ INVALID
Definition: pipeline.hpp:50
deepstream::Pipeline::start
Pipeline & start()
Start the pipeline.
deepstream::Pipeline::resume
Pipeline & resume()
Resume the pipeline.
deepstream::Pipeline::activate
Pipeline & activate()
Start the pipeline after it is already intialized.
deepstream::Pipeline::Message::Message
Message(uint32_t type)
Definition: pipeline.hpp:70
deepstream::Pipeline::run
int run()
deepstream::Pipeline::PAUSED
@ PAUSED
Definition: pipeline.hpp:53
deepstream::Pipeline::pause
Pipeline & pause()
Pause the pipeline.
deepstream::Pipeline::elements_
std::map< std::string, Element > elements_
Definition: pipeline.hpp:276
deepstream::Pipeline::EOSMessage
Pipeline message on EOS.
Definition: pipeline.hpp:76
deepstream::Pipeline::Message::~Message
virtual ~Message()
Definition: pipeline.hpp:65
deepstream::Pipeline::DynamicSourceMessage::DynamicSourceMessage
DynamicSourceMessage(void *)
deepstream::Pipeline::EOSMessage::EOSMessage
EOSMessage(void *)
deepstream::Pipeline::seek
Pipeline & seek(uint64_t timestamp)
Seek to a specified position for processing data within the pipeline.
deepstream::Pipeline::stop
Pipeline & stop()
Stop the pipeline.
object.hpp
deepstream::Pipeline::StateTransitionMessage::name_
std::string name_
Definition: pipeline.hpp:116
deepstream::Object
Base Object class.
Definition: object.hpp:44
deepstream::Pipeline::link
Pipeline & link(const std::string &arg1, const std::string arg2, const Args &...args)
Template function for linking elements in the simplest way.
Definition: pipeline.hpp:165
deepstream::Pipeline::StateTransitionMessage::new_state_
State new_state_
Definition: pipeline.hpp:115
deepstream::Pipeline::DynamicSourceMessage
Pipeline message on source add/remove.
Definition: pipeline.hpp:84
deepstream::Pipeline::DynamicSourceMessage::getUri
const std::string & getUri() const
Definition: pipeline.hpp:92
deepstream::Pipeline::DynamicSourceMessage::isSourceAdded
bool isSourceAdded() const
Definition: pipeline.hpp:88
deepstream::Pipeline::attach
Pipeline & attach(const std::string &element_name, const std::string &plugin_name, const std::string &object_name, const std::string tip, const Args &... args)
Template function for creating and attaching custom object with properties.
Definition: pipeline.hpp:206