NVIDIA DeepStream SDK API Reference

8.0 Release
9.0/service-maker/includes/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 #include <unordered_map>
29 
30 #include "object.hpp"
31 #include "element.hpp"
32 #include "buffer_probe.hpp"
33 #include "signal_handler.hpp"
34 #include "metadata.hpp"
35 
36 namespace deepstream
37 {
38 
39 
46 struct RecordingInfo {
47  RecordingInfo(void* data=nullptr);
48  virtual ~RecordingInfo();
49 
51  unsigned int getSessionId() const;
53  std::string getFileName() const;
55  std::string getFileDirectory() const;
57  unsigned int getDuration() const;
59  std::string getContainerType() const;
61  unsigned int getWidth() const;
63  unsigned int getHeight() const;
65  bool containsVideo() const;
67  bool containsAudio() const;
69  unsigned int getChannels() const;
71  unsigned int getSamplingRate() const;
72 
73  operator bool() const {
74  return data_ != nullptr;
75  }
76 
77  protected:
78  void * data_;
79 
80 };
81 
91 class Pipeline : public Object {
92  public:
93 
94  typedef enum {
95  INVALID,
96  EMPTY,
97  READY,
98  PAUSED,
99  PLAYING
100  } State;
101 
108  class Message {
109  public:
110  virtual ~Message() {}
111  uint32_t type() { return type_; }
112  protected:
113  uint32_t type_;
114 
115  Message(uint32_t type): type_(type) {}
116  };
117 
121  class EOSMessage : public Message {
122  public:
123  EOSMessage(void*);
124  };
125 
129  class DynamicSourceMessage : public Message {
130  public:
131  DynamicSourceMessage(void*);
132 
133  inline bool isSourceAdded() const { return source_added_; }
134  inline uint32_t getSourceId() const { return source_id_; }
135  inline const std::string& getSensorId() const { return sensor_id_; }
136  inline const std::string& getSensorName() const { return sensor_name_; }
137  inline const std::string& getUri() const { return uri_; }
138 
139  protected:
140  bool source_added_ = false;
141  uint32_t source_id_ = 0;
142  std::string sensor_id_;
143  std::string sensor_name_;
144  std::string uri_;
145  };
146 
150  class StateTransitionMessage : public Message {
151  public:
152  StateTransitionMessage(void*);
153  inline const std::string& getName() const { return name_; };
154  inline void getState(State& old_state, State& new_state) const {
155  old_state = old_state_;
156  new_state = new_state_;
157  }
158  protected:
161  std::string name_;
162  };
163 
164  Pipeline(const char* name);
165 
167  Pipeline(const char* name, const std::string& config_file);
168 
170  virtual ~Pipeline();
171 
173  template<typename... Args>
174  Pipeline& add(const std::string& element_type, const std::string& element_name, const Args&... args) {
175  Element element = Element(element_type, element_name);
176  if constexpr (sizeof...(args) > 0) {
177  element.set(args...);
178  }
179  return this->add(element);
180  }
181 
183  Pipeline& add(Element element);
184 
186  Element* find(const::std::string& name);
187 
189  Element& operator[](const std::string& name) {
190  Element* e = this->find(name);
191  if (e == nullptr) {
192  throw std::runtime_error(name + " Not found");
193  }
194  return *e;
195  }
196 
203  Pipeline& link(
204  std::pair<std::string, std::string> route,
205  std::pair<std::string, std::string> tips
206  );
207 
209  template <typename... Args>
210  Pipeline &link(const std::string &arg1, const std::string arg2, const Args &...args)
211  {
212  link(std::make_pair(arg1, arg2), std::make_pair("", ""));
213  if constexpr (sizeof...(args) > 0) {
214  return this->link(arg2, args...);
215  } else {
216  return *this;
217  }
218  }
219 
230  Pipeline& attach(const std::string& elmenent_name, CustomObject* object, const std::string tip="");
231 
243  Pipeline& attach(
244  const std::string& element_name,
245  const std::string& plugin_name,
246  const std::string& object_name,
247  const std::string tip="");
248 
250  template<typename... Args>
252  const std::string& element_name,
253  const std::string& plugin_name,
254  const std::string& object_name,
255  const std::string tip,
256  const Args&... args) {
257  attach(element_name, plugin_name, object_name, tip);
258  if (sizeof...(args) > 0) {
259  auto& element = (*this)[element_name];
260  if (auto handler = element.getSignalHandler(object_name)) {
261  handler->set(args...);
262  } else if (auto probe = element.getProbe(object_name)) {
263  probe->set(args...);
264  }
265  }
266  return *this;
267  }
268 
270  Pipeline& install(std::function<void(Pipeline&, int key)> keyboard_listener) {
271  keyboard_listener_ = keyboard_listener;
272  return *this;
273  }
274 
276  int prepare();
278  int prepare(std::function<void(Pipeline &, const Message &)> listener);
279 
281  Pipeline& start();
283  Pipeline& start(std::function<void(Pipeline&, const Message&)> listener);
285  Pipeline &activate();
287  Pipeline& wait();
288 
290  Pipeline& stop();
291 
292  bool isRunning() const;
293 
295  Pipeline& pause();
297  Pipeline& resume();
299  Pipeline& seek(uint64_t timestamp);
300 
302  uint32_t startRTSP(uint16_t rtsp_port, uint16_t udp_port, uint32_t buffer_size=0);
303 
312  uint32_t startRecording(const std::string& element_name,
313  uint32_t startTime = 0,
314  uint32_t duration = 0,
315  std::function<void(const RecordingInfo &)> callback = [](const RecordingInfo &info) {
316  printf("\nSource recorded at %s/%s. Duration %.2f sec\n",
317  info.getFileDirectory().c_str(), info.getFileName().c_str(),
318  info.getDuration() / 1000.0);
319  },
320  void* userData = nullptr);
321 
327  bool stopRecording(const std::string& element_name);
328 
334  bool stopRecording(uint32_t sessionId);
335 
336  void handleKey(int key);
337 
338 protected:
339  int run();
340  int run_after_prepare();
341 
342  // This is GMainLoop*
343  void *loop_ = NULL;
344  // gstreamer bus watch id
345  uint bus_watch_id_ = 0;
346  // gstreamer bus data
347  void *bus_data_ = NULL;
348  std::function<void(Pipeline&, int key)> keyboard_listener_;
349 
350  // Thread
351  std::thread thread_;
352  // Hold all the element references here
353  std::map<std::string, Element> elements_;
354  // Hold the signal emitters for action control
355  std::map<std::string, std::unique_ptr<SignalEmitter>> action_owners_;
356  // start timestamp for the whole pipeline
357  uint64_t start_pts_ = 0;
358  // Smart Record session id per element name
359  std::unordered_map<std::string, uint32_t> element_to_session_id;
360  std::unordered_map<uint32_t, std::string> session_id_to_element;
361 };
362 
363 } // namespace deepstream
364 
365 #endif
deepstream::Object::Element
friend class Element
Definition: service-maker/includes/object.hpp:205
deepstream::RecordingInfo::containsVideo
bool containsVideo() const
Get the recording file contains video.
element.hpp
signal_handler.hpp
metadata.hpp
deepstream::Pipeline::session_id_to_element
std::unordered_map< uint32_t, std::string > session_id_to_element
Definition: service-maker/includes/pipeline.hpp:360
deepstream::Pipeline::bus_data_
void * bus_data_
Definition: service-maker/includes/pipeline.hpp:347
deepstream::Pipeline::PAUSED
@ PAUSED
Definition: service-maker/includes/pipeline.hpp:98
deepstream::Pipeline::isRunning
bool isRunning() const
deepstream::RecordingInfo::~RecordingInfo
virtual ~RecordingInfo()
object.hpp
deepstream::RecordingInfo::getContainerType
std::string getContainerType() const
Get the recording file container type.
deepstream::Pipeline::action_owners_
std::map< std::string, std::unique_ptr< SignalEmitter > > action_owners_
Definition: service-maker/includes/pipeline.hpp:355
deepstream::CustomObject
Base class for all the custom objects.
Definition: service-maker/includes/custom_object.hpp:34
deepstream::Pipeline::StateTransitionMessage::getName
const std::string & getName() const
Definition: 9.0/service-maker/includes/pipeline.hpp:153
deepstream::Pipeline::bus_watch_id_
uint bus_watch_id_
Definition: service-maker/includes/pipeline.hpp:345
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: service-maker/includes/pipeline.hpp:351
deepstream::Element
Element class definition.
Definition: service-maker/includes/element.hpp:49
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: 9.0/service-maker/includes/pipeline.hpp:174
deepstream::Pipeline::StateTransitionMessage::old_state_
State old_state_
Definition: service-maker/includes/pipeline.hpp:159
deepstream::Pipeline::elements_
std::map< std::string, Element > elements_
Definition: service-maker/includes/pipeline.hpp:353
deepstream::Pipeline::StateTransitionMessage::StateTransitionMessage
StateTransitionMessage(void *)
deepstream::Pipeline::DynamicSourceMessage::getSensorName
const std::string & getSensorName() const
Definition: 9.0/service-maker/includes/pipeline.hpp:136
deepstream::RecordingInfo::getDuration
unsigned int getDuration() const
Get the recording file duration.
deepstream::RecordingInfo::getHeight
unsigned int getHeight() const
Get the recording file height.
deepstream::Pipeline
Pipeline class definition.
Definition: service-maker/includes/pipeline.hpp:91
deepstream::Pipeline::Message::type
uint32_t type()
Definition: 9.0/service-maker/includes/pipeline.hpp:111
buffer_probe.hpp
deepstream::Pipeline::install
Pipeline & install(std::function< void(Pipeline &, int key)> keyboard_listener)
install a callback to capture keyboard events
Definition: 9.0/service-maker/includes/pipeline.hpp:270
deepstream::RecordingInfo::data_
void * data_
Definition: service-maker/includes/pipeline.hpp:78
deepstream::Pipeline::DynamicSourceMessage::getSensorId
const std::string & getSensorId() const
Definition: 9.0/service-maker/includes/pipeline.hpp:135
deepstream::Pipeline::DynamicSourceMessage::sensor_id_
std::string sensor_id_
Definition: service-maker/includes/pipeline.hpp:142
deepstream::Pipeline::StateTransitionMessage::getState
void getState(State &old_state, State &new_state) const
Definition: 9.0/service-maker/includes/pipeline.hpp:154
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::element_to_session_id
std::unordered_map< std::string, uint32_t > element_to_session_id
Definition: service-maker/includes/pipeline.hpp:359
deepstream::Pipeline::State
State
Definition: service-maker/includes/pipeline.hpp:94
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: service-maker/includes/pipeline.hpp:357
deepstream::Pipeline::keyboard_listener_
std::function< void(Pipeline &, int key)> keyboard_listener_
Definition: service-maker/includes/pipeline.hpp:348
deepstream::Pipeline::DynamicSourceMessage::getSourceId
uint32_t getSourceId() const
Definition: 9.0/service-maker/includes/pipeline.hpp:134
deepstream::Pipeline::DynamicSourceMessage::uri_
std::string uri_
Definition: service-maker/includes/pipeline.hpp:144
deepstream::Pipeline::DynamicSourceMessage::source_added_
bool source_added_
Definition: service-maker/includes/pipeline.hpp:140
deepstream::Pipeline::operator[]
Element & operator[](const std::string &name)
Operator for accessing elements in a pipeline.
Definition: 9.0/service-maker/includes/pipeline.hpp:189
deepstream::RecordingInfo::getSessionId
unsigned int getSessionId() const
Get the recording session id.
deepstream::Pipeline::DynamicSourceMessage::source_id_
uint32_t source_id_
Definition: service-maker/includes/pipeline.hpp:141
deepstream::RecordingInfo::getWidth
unsigned int getWidth() const
Get the recording file width.
deepstream::Pipeline::PLAYING
@ PLAYING
Definition: service-maker/includes/pipeline.hpp:99
deepstream
Definition: service-maker/includes/buffer.hpp:33
deepstream::Pipeline::run_after_prepare
int run_after_prepare()
deepstream::Pipeline::Message::type_
uint32_t type_
Definition: service-maker/includes/pipeline.hpp:113
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::Pipeline::startRecording
uint32_t startRecording(const std::string &element_name, uint32_t startTime=0, uint32_t duration=0, std::function< void(const RecordingInfo &)> callback=[](const RecordingInfo &info) { printf("\nSource recorded at %s/%s. Duration %.2f sec\n", info.getFileDirectory().c_str(), info.getFileName().c_str(), info.getDuration()/1000.0);}, void *userData=nullptr)
Send Smart Record start signal to an nvurisrcbin element.
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: service-maker/includes/pipeline.hpp:143
deepstream::Pipeline::~Pipeline
virtual ~Pipeline()
Destructor.
deepstream::RecordingInfo
Holds information about a recording session.
Definition: service-maker/includes/pipeline.hpp:46
deepstream::Object::Pipeline
friend class Pipeline
Definition: service-maker/includes/object.hpp:204
deepstream::Pipeline::handleKey
void handleKey(int key)
deepstream::Pipeline::stopRecording
bool stopRecording(const std::string &element_name)
Send Smart Record stop signal to an nvurisrcbin element.
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: 9.0/service-maker/includes/pipeline.hpp:115
deepstream::Pipeline::run
int run()
deepstream::RecordingInfo::RecordingInfo
RecordingInfo(void *data=nullptr)
deepstream::Pipeline::pause
Pipeline & pause()
Pause the pipeline.
deepstream::RecordingInfo::getFileName
std::string getFileName() const
Get the recording file name.
deepstream::RecordingInfo::containsAudio
bool containsAudio() const
Get the recording file contains audio.
deepstream::Pipeline::Message::~Message
virtual ~Message()
Definition: 9.0/service-maker/includes/pipeline.hpp:110
deepstream::Pipeline::DynamicSourceMessage::DynamicSourceMessage
DynamicSourceMessage(void *)
deepstream::Pipeline::EOSMessage::EOSMessage
EOSMessage(void *)
deepstream::Pipeline::EMPTY
@ EMPTY
Definition: service-maker/includes/pipeline.hpp:96
deepstream::RecordingInfo::getFileDirectory
std::string getFileDirectory() const
Get the recording file directory.
deepstream::RecordingInfo::getSamplingRate
unsigned int getSamplingRate() const
Get the recording file sampling rate.
deepstream::Pipeline::seek
Pipeline & seek(uint64_t timestamp)
Seek to a specified position for processing data within the pipeline.
deepstream::Pipeline::INVALID
@ INVALID
Definition: service-maker/includes/pipeline.hpp:95
deepstream::Pipeline::stop
Pipeline & stop()
Stop the pipeline.
deepstream::Pipeline::READY
@ READY
Definition: service-maker/includes/pipeline.hpp:97
deepstream::Pipeline::StateTransitionMessage::name_
std::string name_
Definition: service-maker/includes/pipeline.hpp:161
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: 9.0/service-maker/includes/pipeline.hpp:210
deepstream::RecordingInfo::getChannels
unsigned int getChannels() const
Get the recording file channels.
deepstream::Pipeline::StateTransitionMessage::new_state_
State new_state_
Definition: service-maker/includes/pipeline.hpp:160
deepstream::Pipeline::DynamicSourceMessage::getUri
const std::string & getUri() const
Definition: 9.0/service-maker/includes/pipeline.hpp:137
deepstream::Pipeline::loop_
void * loop_
Definition: service-maker/includes/pipeline.hpp:343
deepstream::Pipeline::DynamicSourceMessage::isSourceAdded
bool isSourceAdded() const
Definition: 9.0/service-maker/includes/pipeline.hpp:133
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: 9.0/service-maker/includes/pipeline.hpp:251