NVIDIA DeepStream SDK API Reference

7.1 Release
pipeline.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024 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 DynamicSourceMessage : public Message {
77  public:
78  DynamicSourceMessage(void*);
79 
80  inline bool isSourceAdded() const { return source_added_; }
81  inline uint32_t getSourceId() const { return source_id_; }
82  inline const std::string& getSensorId() const { return sensor_id_; }
83  inline const std::string& getSensorName() const { return sensor_name_; }
84  inline const std::string& getUri() const { return uri_; }
85 
86  protected:
87  bool source_added_ = false;
88  uint32_t source_id_ = 0;
89  std::string sensor_id_;
90  std::string sensor_name_;
91  std::string uri_;
92  };
93 
98  public:
100  inline const std::string& getName() const { return name_; };
101  inline void getState(State& old_state, State& new_state) const {
102  old_state = old_state_;
103  new_state = new_state_;
104  }
105  protected:
108  std::string name_;
109  };
110 
111  Pipeline(const char* name);
112 
114  Pipeline(const char* name, const std::string& config_file);
115 
117  virtual ~Pipeline();
118 
120  template<typename... Args>
121  Pipeline& add(const std::string& element_type, const std::string& element_name, const Args&... args) {
122  Element element = Element(element_type, element_name);
123  if constexpr (sizeof...(args) > 0) {
124  element.set(args...);
125  }
126  return this->add(element);
127  }
128 
130  Pipeline& add(Element element);
131 
133  Element* find(const::std::string& name);
134 
136  Element& operator[](const std::string& name) {
137  Element* e = this->find(name);
138  if (e == nullptr) {
139  throw std::runtime_error(name + " Not found");
140  }
141  return *e;
142  }
143 
150  Pipeline& link(
151  std::pair<std::string, std::string> route,
152  std::pair<std::string, std::string> tips
153  );
154 
156  template <typename... Args>
157  Pipeline &link(const std::string &arg1, const std::string arg2, const Args &...args)
158  {
159  link(std::make_pair(arg1, arg2), std::make_pair("", ""));
160  if constexpr (sizeof...(args) > 0) {
161  return this->link(arg2, args...);
162  } else {
163  return *this;
164  }
165  }
166 
177  Pipeline& attach(const std::string& elmenent_name, CustomObject* object, const std::string tip="");
178 
190  Pipeline& attach(
191  const std::string& element_name,
192  const std::string& plugin_name,
193  const std::string& object_name,
194  const std::string tip="");
195 
197  template<typename... Args>
199  const std::string& element_name,
200  const std::string& plugin_name,
201  const std::string& object_name,
202  const std::string tip,
203  const Args&... args) {
204  attach(element_name, plugin_name, object_name, tip);
205  if (sizeof...(args) > 0) {
206  auto& element = (*this)[element_name];
207  if (auto handler = element.getSignalHandler(object_name)) {
208  handler->set(args...);
209  } else if (auto probe = element.getProbe(object_name)) {
210  probe->set(args...);
211  }
212  }
213  return *this;
214  }
215 
217  Pipeline& install(std::function<void(Pipeline&, int key)> keyboard_listener) {
218  keyboard_listener_ = keyboard_listener;
219  return *this;
220  }
221 
223  int prepare();
225  int prepare(std::function<void(Pipeline &, const Message &)> listener);
226 
228  Pipeline& start();
230  Pipeline& start(std::function<void(Pipeline&, const Message&)> listener);
232  Pipeline &activate();
234  Pipeline& wait();
235 
237  Pipeline& stop();
238 
239  bool isRunning() const;
240 
242  Pipeline& pause();
244  Pipeline& resume();
246  Pipeline& seek(uint64_t timestamp);
247 
249  uint32_t startRTSP(uint16_t rtsp_port, uint16_t udp_port, uint32_t buffer_size=0);
250 
251  void handleKey(int key);
252 
253 protected:
254  int run();
255  int run_after_prepare();
256 
257  // This is GMainLoop*
258  void *loop_ = NULL;
259  // gstreamer bus watch id
260  uint bus_watch_id_ = 0;
261  // gstreamer bus data
262  void *bus_data_ = NULL;
263  std::function<void(Pipeline&, int key)> keyboard_listener_;
264 
265  // Thread
266  std::thread thread_;
267  // Hold all the element references here
268  std::map<std::string, Element> elements_;
269  // Hold the signal emitters for action control
270  std::map<std::string, std::unique_ptr<SignalEmitter>> action_owners_;
271  // start timestamp for the whole pipeline
272  uint64_t start_pts_ = 0;
273 };
274 
275 } // namespace deepstream
276 
277 #endif
buffer_probe.hpp
deepstream::Pipeline::StateTransitionMessage
Pipeline message on state transition.
Definition: pipeline.hpp:97
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:100
deepstream::Pipeline::bus_watch_id_
uint bus_watch_id_
Definition: pipeline.hpp:260
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:266
deepstream::Element
Element class definition.
Definition: element.hpp:49
deepstream::Pipeline::keyboard_listener_
std::function< void(Pipeline &, int key)> keyboard_listener_
Definition: pipeline.hpp:263
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:121
deepstream::Pipeline::StateTransitionMessage::old_state_
State old_state_
Definition: pipeline.hpp:106
deepstream::Pipeline::StateTransitionMessage::StateTransitionMessage
StateTransitionMessage(void *)
deepstream::Pipeline::DynamicSourceMessage::getSensorName
const std::string & getSensorName() const
Definition: pipeline.hpp:83
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:217
deepstream::Pipeline::PLAYING
@ PLAYING
Definition: pipeline.hpp:54
deepstream::Pipeline::DynamicSourceMessage::getSensorId
const std::string & getSensorId() const
Definition: pipeline.hpp:82
deepstream::Pipeline::action_owners_
std::map< std::string, std::unique_ptr< SignalEmitter > > action_owners_
Definition: pipeline.hpp:270
deepstream::Pipeline::DynamicSourceMessage::sensor_id_
std::string sensor_id_
Definition: pipeline.hpp:89
deepstream::Pipeline::StateTransitionMessage::getState
void getState(State &old_state, State &new_state) const
Definition: pipeline.hpp:101
deepstream::Pipeline::loop_
void * loop_
Definition: pipeline.hpp:258
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:262
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:272
deepstream::Pipeline::DynamicSourceMessage::getSourceId
uint32_t getSourceId() const
Definition: pipeline.hpp:81
deepstream::Pipeline::DynamicSourceMessage::uri_
std::string uri_
Definition: pipeline.hpp:91
deepstream::Pipeline::DynamicSourceMessage::source_added_
bool source_added_
Definition: pipeline.hpp:87
deepstream::Pipeline::operator[]
Element & operator[](const std::string &name)
Operator for accessing elements in a pipeline.
Definition: pipeline.hpp:136
deepstream::Pipeline::DynamicSourceMessage::source_id_
uint32_t source_id_
Definition: pipeline.hpp:88
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:90
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:268
deepstream::Pipeline::Message::~Message
virtual ~Message()
Definition: pipeline.hpp:65
deepstream::Pipeline::DynamicSourceMessage::DynamicSourceMessage
DynamicSourceMessage(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:108
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:157
deepstream::Pipeline::StateTransitionMessage::new_state_
State new_state_
Definition: pipeline.hpp:107
deepstream::Pipeline::DynamicSourceMessage
Pipeline message on source add/remove.
Definition: pipeline.hpp:76
deepstream::Pipeline::DynamicSourceMessage::getUri
const std::string & getUri() const
Definition: pipeline.hpp:84
deepstream::Pipeline::DynamicSourceMessage::isSourceAdded
bool isSourceAdded() const
Definition: pipeline.hpp:80
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:198