NVIDIA DeepStream SDK API Reference

9.1 Release
9.1/service-maker/includes/pipeline.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-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 
27 #ifndef PIPELINE_HPP
28 #define PIPELINE_HPP
29 
30 #include <string>
31 #include <memory>
32 #include <thread>
33 #include <unordered_map>
34 
35 #include "object.hpp"
36 #include "element.hpp"
37 #include "buffer_probe.hpp"
38 #include "signal_handler.hpp"
39 #include "metadata.hpp"
40 
41 namespace deepstream
42 {
43 
44 
51 struct RecordingInfo {
52  RecordingInfo(void* data=nullptr);
53  virtual ~RecordingInfo();
54 
56  unsigned int getSessionId() const;
58  std::string getFileName() const;
60  std::string getFileDirectory() const;
62  unsigned int getDuration() const;
64  std::string getContainerType() const;
66  unsigned int getWidth() const;
68  unsigned int getHeight() const;
70  bool containsVideo() const;
72  bool containsAudio() const;
74  unsigned int getChannels() const;
76  unsigned int getSamplingRate() const;
77 
78  operator bool() const {
79  return data_ != nullptr;
80  }
81 
82  protected:
83  void * data_;
84 
85 };
86 
96 class Pipeline : public Object {
97  public:
98 
99  typedef enum {
100  INVALID,
101  EMPTY,
102  READY,
103  PAUSED,
104  PLAYING
105  } State;
106 
113  class Message {
114  public:
115  virtual ~Message() {}
116  uint32_t type() { return type_; }
117  protected:
118  uint32_t type_;
119 
120  Message(uint32_t type): type_(type) {}
121  };
122 
126  class EOSMessage : public Message {
127  public:
128  EOSMessage(void*);
129  };
130 
134  class DynamicSourceMessage : public Message {
135  public:
136  DynamicSourceMessage(void*);
137  DynamicSourceMessage(uint32_t source_id, bool source_added);
138 
139  inline bool isSourceAdded() const { return source_added_; }
140  inline uint32_t getSourceId() const { return source_id_; }
141  inline const std::string& getSensorId() const { return sensor_id_; }
142  inline const std::string& getSensorName() const { return sensor_name_; }
143  inline const std::string& getUri() const { return uri_; }
144 
145  protected:
146  bool source_added_ = false;
147  uint32_t source_id_ = 0;
148  std::string sensor_id_;
149  std::string sensor_name_;
150  std::string uri_;
151  };
152 
156  class StateTransitionMessage : public Message {
157  public:
158  StateTransitionMessage(void*);
159  inline const std::string& getName() const { return name_; };
160  inline void getState(State& old_state, State& new_state) const {
161  old_state = old_state_;
162  new_state = new_state_;
163  }
164  protected:
167  std::string name_;
168  };
169 
170  Pipeline(const char* name);
171 
173  Pipeline(const char* name, const std::string& config_file);
174 
176  virtual ~Pipeline();
177 
179  template<typename... Args>
180  Pipeline& add(const std::string& element_type, const std::string& element_name, const Args&... args) {
181  Element element = Element(element_type, element_name);
182  if constexpr (sizeof...(args) > 0) {
183  element.set(args...);
184  }
185  return this->add(element);
186  }
187 
189  Pipeline& add(Element element);
190 
192  Element* find(const::std::string& name);
193 
195  Element& operator[](const std::string& name) {
196  Element* e = this->find(name);
197  if (e == nullptr) {
198  throw std::runtime_error(name + " Not found");
199  }
200  return *e;
201  }
202 
209  Pipeline& link(
210  std::pair<std::string, std::string> route,
211  std::pair<std::string, std::string> tips
212  );
213 
215  template <typename... Args>
216  Pipeline &link(const std::string &arg1, const std::string arg2, const Args &...args)
217  {
218  link(std::make_pair(arg1, arg2), std::make_pair("", ""));
219  if constexpr (sizeof...(args) > 0) {
220  return this->link(arg2, args...);
221  } else {
222  return *this;
223  }
224  }
225 
236  Pipeline& attach(const std::string& elmenent_name, CustomObject* object, const std::string tip="");
237 
249  Pipeline& attach(
250  const std::string& element_name,
251  const std::string& plugin_name,
252  const std::string& object_name,
253  const std::string tip="");
254 
256  template<typename... Args>
258  const std::string& element_name,
259  const std::string& plugin_name,
260  const std::string& object_name,
261  const std::string tip,
262  const Args&... args) {
263  attach(element_name, plugin_name, object_name, tip);
264  if (sizeof...(args) > 0) {
265  auto& element = (*this)[element_name];
266  if (auto handler = element.getSignalHandler(object_name)) {
267  handler->set(args...);
268  } else if (auto probe = element.getProbe(object_name)) {
269  probe->set(args...);
270  }
271  }
272  return *this;
273  }
274 
276  Pipeline& install(std::function<void(Pipeline&, int key)> keyboard_listener) {
277  keyboard_listener_ = keyboard_listener;
278  return *this;
279  }
280 
282  int prepare();
284  int prepare(std::function<void(Pipeline &, const Message &)> listener);
285 
287  Pipeline& start();
289  Pipeline& start(std::function<void(Pipeline&, const Message&)> listener);
291  Pipeline &activate();
293  Pipeline& wait();
294 
296  Pipeline& stop();
297 
298  bool isRunning() const;
299 
301  Pipeline& pause();
303  Pipeline& resume();
305  Pipeline& seek(uint64_t timestamp);
306 
308  uint32_t startRTSP(uint16_t rtsp_port, uint16_t udp_port, uint32_t buffer_size=0);
309 
318  uint32_t startRecording(const std::string& element_name,
319  uint32_t startTime = 0,
320  uint32_t duration = 0,
321  std::function<void(const RecordingInfo &)> callback = [](const RecordingInfo &info) {
322  printf("\nSource recorded at %s/%s. Duration %.2f sec\n",
323  info.getFileDirectory().c_str(), info.getFileName().c_str(),
324  info.getDuration() / 1000.0);
325  },
326  void* userData = nullptr);
327 
333  bool stopRecording(const std::string& element_name);
334 
340  bool stopRecording(uint32_t sessionId);
341 
342  void handleKey(int key);
343 
344 protected:
345  int run();
346  int run_after_prepare();
347 
348  // This is GMainLoop*
349  void *loop_ = NULL;
350  // gstreamer bus watch id
351  uint bus_watch_id_ = 0;
352  // gstreamer bus data
353  void *bus_data_ = NULL;
354  std::function<void(Pipeline&, int key)> keyboard_listener_;
355 
356  // Thread
357  std::thread thread_;
358  // Hold all the element references here
359  std::map<std::string, Element> elements_;
360  // Hold the signal emitters for action control
361  std::map<std::string, std::unique_ptr<SignalEmitter>> action_owners_;
362  // start timestamp for the whole pipeline
363  uint64_t start_pts_ = 0;
364  // Smart Record session id per element name
365  std::unordered_map<std::string, uint32_t> element_to_session_id;
366  std::unordered_map<uint32_t, std::string> session_id_to_element;
367 };
368 
369 } // namespace deepstream
370 
371 #endif
deepstream::Object::Element
friend class Element
Definition: service-maker/includes/object.hpp:210
deepstream::RecordingInfo::containsVideo
bool containsVideo() const
Get the recording file contains video.
deepstream::Pipeline::session_id_to_element
std::unordered_map< uint32_t, std::string > session_id_to_element
Definition: service-maker/includes/pipeline.hpp:366
deepstream::Pipeline::bus_data_
void * bus_data_
Definition: service-maker/includes/pipeline.hpp:353
deepstream::Pipeline::PAUSED
@ PAUSED
Definition: service-maker/includes/pipeline.hpp:103
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:361
deepstream::CustomObject
Base class for all the custom objects.
Definition: service-maker/includes/custom_object.hpp:39
deepstream::Pipeline::StateTransitionMessage::getName
const std::string & getName() const
Definition: 9.1/service-maker/includes/pipeline.hpp:159
deepstream::Pipeline::bus_watch_id_
uint bus_watch_id_
Definition: service-maker/includes/pipeline.hpp:351
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:357
deepstream::Element
Element class definition.
Definition: service-maker/includes/element.hpp:54
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.1/service-maker/includes/pipeline.hpp:180
deepstream::Pipeline::StateTransitionMessage::old_state_
State old_state_
Definition: service-maker/includes/pipeline.hpp:165
deepstream::Pipeline::elements_
std::map< std::string, Element > elements_
Definition: service-maker/includes/pipeline.hpp:359
deepstream::Pipeline::StateTransitionMessage::StateTransitionMessage
StateTransitionMessage(void *)
deepstream::Pipeline::DynamicSourceMessage::getSensorName
const std::string & getSensorName() const
Definition: 9.1/service-maker/includes/pipeline.hpp:142
deepstream::RecordingInfo::getDuration
unsigned int getDuration() const
Get the recording file duration.
deepstream::RecordingInfo::getHeight
unsigned int getHeight() const
Get the recording file height.
buffer_probe.hpp
deepstream::Pipeline
Pipeline class definition.
Definition: service-maker/includes/pipeline.hpp:96
deepstream::Pipeline::Message::type
uint32_t type()
Definition: 9.1/service-maker/includes/pipeline.hpp:116
deepstream::Pipeline::install
Pipeline & install(std::function< void(Pipeline &, int key)> keyboard_listener)
install a callback to capture keyboard events
Definition: 9.1/service-maker/includes/pipeline.hpp:276
deepstream::RecordingInfo::data_
void * data_
Definition: service-maker/includes/pipeline.hpp:83
deepstream::Pipeline::DynamicSourceMessage::getSensorId
const std::string & getSensorId() const
Definition: 9.1/service-maker/includes/pipeline.hpp:141
deepstream::Pipeline::DynamicSourceMessage::sensor_id_
std::string sensor_id_
Definition: service-maker/includes/pipeline.hpp:148
deepstream::Pipeline::StateTransitionMessage::getState
void getState(State &old_state, State &new_state) const
Definition: 9.1/service-maker/includes/pipeline.hpp:160
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:365
deepstream::Pipeline::State
State
Definition: service-maker/includes/pipeline.hpp:99
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:363
deepstream::Pipeline::keyboard_listener_
std::function< void(Pipeline &, int key)> keyboard_listener_
Definition: service-maker/includes/pipeline.hpp:354
deepstream::Pipeline::DynamicSourceMessage::getSourceId
uint32_t getSourceId() const
Definition: 9.1/service-maker/includes/pipeline.hpp:140
deepstream::Pipeline::DynamicSourceMessage::uri_
std::string uri_
Definition: service-maker/includes/pipeline.hpp:150
deepstream::Pipeline::DynamicSourceMessage::source_added_
bool source_added_
Definition: service-maker/includes/pipeline.hpp:146
deepstream::Pipeline::operator[]
Element & operator[](const std::string &name)
Operator for accessing elements in a pipeline.
Definition: 9.1/service-maker/includes/pipeline.hpp:195
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:147
deepstream::RecordingInfo::getWidth
unsigned int getWidth() const
Get the recording file width.
deepstream::Pipeline::PLAYING
@ PLAYING
Definition: service-maker/includes/pipeline.hpp:104
deepstream
Definition: service-maker/includes/buffer.hpp:38
deepstream::Pipeline::run_after_prepare
int run_after_prepare()
deepstream::Pipeline::Message::type_
uint32_t type_
Definition: service-maker/includes/pipeline.hpp:118
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:149
deepstream::Pipeline::~Pipeline
virtual ~Pipeline()
Destructor.
metadata.hpp
deepstream::RecordingInfo
Holds information about a recording session.
Definition: service-maker/includes/pipeline.hpp:51
deepstream::Object::Pipeline
friend class Pipeline
Definition: service-maker/includes/object.hpp:209
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.
signal_handler.hpp
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.1/service-maker/includes/pipeline.hpp:120
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.1/service-maker/includes/pipeline.hpp:115
deepstream::Pipeline::DynamicSourceMessage::DynamicSourceMessage
DynamicSourceMessage(void *)
deepstream::Pipeline::EOSMessage::EOSMessage
EOSMessage(void *)
deepstream::Pipeline::EMPTY
@ EMPTY
Definition: service-maker/includes/pipeline.hpp:101
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:100
deepstream::Pipeline::stop
Pipeline & stop()
Stop the pipeline.
deepstream::Pipeline::READY
@ READY
Definition: service-maker/includes/pipeline.hpp:102
deepstream::Pipeline::StateTransitionMessage::name_
std::string name_
Definition: service-maker/includes/pipeline.hpp:167
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.1/service-maker/includes/pipeline.hpp:216
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:166
element.hpp
deepstream::Pipeline::DynamicSourceMessage::getUri
const std::string & getUri() const
Definition: 9.1/service-maker/includes/pipeline.hpp:143
deepstream::Pipeline::loop_
void * loop_
Definition: service-maker/includes/pipeline.hpp:349
deepstream::Pipeline::DynamicSourceMessage::isSourceAdded
bool isSourceAdded() const
Definition: 9.1/service-maker/includes/pipeline.hpp:139
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.1/service-maker/includes/pipeline.hpp:257