NVIDIA DeepStream SDK API Reference

9.1 Release
9.1/sources/libs/nvstreammux/include/nvstreammux.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-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 
18 #ifndef __NVSTREAMMUX_H__
19 #define __NVSTREAMMUX_H__
20 
21 #include <chrono>
22 #include <unordered_map>
23 #include <map>
24 #include <list>
25 #include "nvbufsurface.h"
26 #include "nvstreammux_batch.h"
27 #include "nvstreammux_debug.h"
28 
29 //G_BEGIN_DECLS
30 
33 
34 /*
35  * @brief low level helper for core logic, this is agnostic of gstreamer
36  * or any other wrapper on higher level
37  */
38 
39 class NvStreamMux
40 {
41 
42 
43  public :
44 
45  NvStreamMux(SourcePad * srcpad, INvStreammuxDebug* a_debug_iface=nullptr) : inputs(), src_pad(srcpad), cv(), cv_throttle_wait(), mutex(), mutex_throttle_wait()
46  {
47  batch = NULL;
48 
49  stop_task = false;
50  num_queues_empty = 0;
51  num_sources_idle = 0;
52  pads_got_eos_and_empty_in_q = 0;
53  num_pads_eos = 0;
54  cur_frame_pts = 0;
55  state = SOURCE_STATE_IDLE;
56  num_surfaces_per_frame = 1;
57  all_pads_eos = false;
58  extra_throttle_wait_time = std::chrono::microseconds(0);
59  got_first_buffer = false;
60  synch_buffer = nullptr;
61  debug_iface = a_debug_iface;
62  }
63 
64  /*
65  * @brief functions to maintain internal variables based on config
66  */
68  {
69  batch_policy = BatchPolicy(cfg, &inputs, debug_iface);
70  batch_policy.total_buf_available = 0;
72  }
73  void set_frame_num_reset_on_eos(bool reset)
74  {
75  frame_num_reset_on_eos = reset;
76  }
78  {
79  frame_num_reset_on_stream_reset = reset;
80  }
81 
82  /*
83  * @brief acquire lock on mux
84  */
85  virtual void lock()
86  {
87  mutex.lock();
88  };
89 
90  /*
91  * @brief release lock on mux
92  */
93  virtual void unlock()
94  {
95  mutex.unlock();
96  };
97 
98  /*
99  * @brief release lock on mux
100  */
101  virtual void wait(){};
102 
103  /*
104  * @brief release lock on mux
105  */
106  virtual void notify_all();
107 
108  /*
109  * @brief get_pad for specified pad id
110  * @param pad_id, id of pad to lookup
111  * @return pointer to SinkPad object for the pad_id
112  */
113  SinkPad * get_pad(unsigned int pad_id);
114 
115  /*
116  * @brief add_pad for specified pad id
117  * @param pad_id, id of pad to lookup
118  * @return pointer to SinkPad object for the pad_id
119  */
120  void add_pad(unsigned int id, SinkPad * pad);
121 
122  /*
123  * @brief
124  * @param pad_id, id of pad to lookup
125  * @param mime_type [IN]
126  * @return NA
127  */
128  void update_pad_mimetype(unsigned int id, PAD_MIME_TYPE mime_type);
129 
130  /*
131  * @brief
132  * @param id [IN] id of pad to lookup
133  * @return The configured mime type for the sinkpad at pad_id==id
134  */
135  PAD_MIME_TYPE get_pad_mimetype(unsigned int id);
136 
137  /*
138  * @brief get_pad for specified pad id
139  * @param pad_id, id of pad to lookup
140  * @return pointer to SinkPad object for the pad_id
141  */
142  void remove_pad(unsigned int id);
143 
144  /*
145  * @brief Construct the NvStreamMux from config, this would typically
146  * come from a high level config file
147  */
148  //virtual update_source(SINK_EVENT event, unsigned long source_id);
149 
150 
151  /*
152  * @brief handle the eos event on pad
153  * @param event [IN] sink event
154  * @param source_id[IN] id of source, in deepstream app pad_id
155  * of gstreamer
156  * source pad is same as this id
157  * @param EventWrapper [IN] event coming on sink pad
158  *
159  */
160  virtual bool handle_eos(SINK_EVENT et, unsigned int source_id, EventWrapper * event);
161 
162  /*
163  * @brief handle the eos event on pad for cascaded muxers
164  * @param event [IN] sink event
165  * @param source_id[IN] id of source, in deepstream app pad_id
166  * of gstreamer
167  * source pad is same as this id
168  * @param EventWrapper [IN] event coming on sink pad
169  *
170  */
171  virtual bool handle_eos_cascaded(SINK_EVENT et, unsigned int source_id, EventWrapper * event);
172  /*
173  * @brief handle the segment event on pad
174  * @param event [IN] sink event
175  * @param source_id[IN] id of source, in deepstream app pad_id
176  * of gstreamer
177  * source pad is same as this id
178  * @param EventWrapper [IN] event coming on sink pad
179  *
180  */
181  virtual bool handle_segment(SINK_EVENT et, unsigned int source_id, EventWrapper * event);
182 
183  /*
184  * @brief handle the GST_NVEVENT_STREAM_RESET event on pad
185  * @param event [IN] sink event
186  * @param source_id[IN] id of source, in deepstream app pad_id
187  * of gstreamer
188  * source pad is same as this id
189  * @param EventWrapper [IN] event coming on sink pad
190  *
191  */
192  virtual bool handle_stream_reset(SINK_EVENT et, unsigned int source_id, EventWrapper * event);
193 
194  /*
195  * @brief handle the FLUSH_STOP event on pad
196  * @param event [IN] sink event
197  * @param source_id[IN] id of source, in deepstream app pad_id
198  * of gstreamer
199  * source pad is same as this id
200  * @param EventWrapper [IN] event coming on sink pad
201  *
202  */
203  virtual bool handle_flush_stop(SINK_EVENT et, unsigned int source_id, EventWrapper * event);
204 
205  /*
206  * @brief add sink pad
207  * @param pad [IN] the pad to be added
208  */
209  virtual void add_sink(SinkPad * pad){};
210 
211  /*
212  * @brief remove sink pad
213  * @param pad [IN] the pad to be added
214  */
215  virtual void remove_sink(SinkPad pad){};
216 
217  /*
218  * @brief add buffer to sink pad
219  * @param pad [IN] the pad to be added
220  * @param buffer [IN] the buffer to be added
221  */
222  //virtual void add_buffer(SinkPad * pad, BufferWrapper * buffer);
223  virtual void add_buffer(unsigned int pad_id, BufferWrapper * buffer);
224 
225  /*
226  * @brief task for processing queued buffers and events,
227  * according to batching configuration
228  * @param pad [IN] the pad to be added
229  * @param buffer [IN] the buffer to be added
230  */
232 
233  /*
234  * @brief function to push the buffer out of mux to designated SourcePad
235  * @param out_buf [IN] the batch to be pushed
236  * @param src_pad [IN] the to push to
237  * @return true on successfully pushing the batch buffer on src_pad;
238  * false otherwise
239  */
240  bool push_batch(NvDsBatchBufferWrapper * out_buf, SourcePad * src_pad);
241 
242  /*
243  * @brief function to push events out of mux
244  */
245 
246  void push_events();
247 
248 
249  /*
250  * @brief function to copy the individual buffers and metadata to batch
251  * @param out_buf [IN] the batch buffer
252  * @param buffer [IN] the metadata
253  * @return the total number of buffers copied
254  */
255  unsigned int copy_batch(NvDsBatchBufferWrapper * out_buf, NvDsBatchMeta *);
256 
257  /*
258  * @brief functions to calculate play duration, called while processing
259  * relevant gstreamer events
260  * @return void
261  */
262  void handle_pause_play();
263  void handle_play_pause();
264  void handle_ready_pause();
265 
266  /*
267  * @brief functions called to process gstreamer event
268  * GST_STATE_CHANGE_READY_TO_PAUSED
269  * @return void
270  */
271  void handle_stop();
272  void reset_stop();
273 
274 
275  /*
276  * @brief function to set value of frame_duration_nsec
277  * @param offset [IN] value to be set
278  */
279  void set_frame_duration(unsigned long);
280 
281  /*
282  * @brief function to set value of cur_frame_pts
283  * @param offset [IN] value to be set
284  */
285  void set_pts_offset(gulong offset);
286 
287  /*
288  * @brief function to get current state of PAD
289  * @param sinkPad [IN] SinkPad Pointer
290  * @param SOURCE_STATE [OUT] State of sinkpad
291  */
293 
298  void set_batch_size(unsigned int size);
299 
304  void set_batch_push_timeout(unsigned int timeout);
305 
310  unsigned int get_batch_size();
311 
316  unsigned int get_config_batch_size();
317 
323  void set_num_surfaces_per_frame(unsigned int num);
324 
329  unsigned int get_num_surfaces_per_frame();
330 
335  void set_all_pads_eos(bool eos);
336 
341  void reset_pad(SinkPad* pad);
342 
347  bool get_all_pads_eos();
348 
354  void set_synch_buffer_iface(ISynchronizeBuffer* synch_buffer_iface);
355 
361 
362  unsigned int get_source_id_with_earliest_buffer();
363 
370 
378  void apply_throttle(bool stop_when_input_buffer=false);
379 
380  private :
381  std::unordered_map<unsigned int, SinkPad *> inputs;
382  SourcePad * src_pad;
383  SOURCE_STATE state;
384  unsigned int num_queues_empty;
385  unsigned int num_sources_idle;
386  unsigned int pads_got_eos_and_empty_in_q;
387  unsigned int num_pads_eos;
388  unsigned int num_surfaces;
389  std::condition_variable cv;
390  std::condition_variable cv_throttle_wait;
393  std::mutex mutex;
394  std::mutex mutex_throttle_wait;
395  Batch * batch;
396  /*
397  * @brief map of sources for a priority
398  */
399  std::multimap<int, int> sources;
400  TimePoint current_play_start_time;
401  TimePoint last_pause_time;
402  NanoSecondsType accum_play_dur;
403  unsigned long frame_duration_nsec;
404  unsigned long cur_frame_pts;
405  bool stop_task;
406  NanoSecondsType extra_throttle_wait_time;
407 
408  BatchPolicy batch_policy;
409  unsigned int num_surfaces_per_frame;
411  bool all_pads_eos;
412  bool got_first_buffer;
413  bool frame_num_reset_on_eos = false;
414  bool frame_num_reset_on_stream_reset = false;
415  INvStreammuxDebug* debug_iface;
416 
417  public:
419 };
420 
421 //G_END_DECLS
422 #endif
NvStreamMux::set_batch_size
void set_batch_size(unsigned int size)
set batch_policy.set_batch_size()
NvStreamMux::synch_buffer
ISynchronizeBuffer * synch_buffer
Definition: sources/libs/nvstreammux/include/nvstreammux.h:418
NvStreamMux::push_loop
bool push_loop(NvDsBatchBufferWrapper *out_buf, NvDsBatchMeta *)
_GstNvStreamMux
Definition: sources/gst-plugins/gst-nvmultistream2/gstnvstreammux.h:47
SINK_EVENT
SINK_EVENT
Definition: sources/libs/nvstreammux/include/nvstreammux_pads.h:29
NvStreamMux::get_config_batch_size
unsigned int get_config_batch_size()
return batch_policy.get_config_batch_size()
NvDsBatchBufferWrapper
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:113
NvStreamMux::get_num_surfaces_per_frame
unsigned int get_num_surfaces_per_frame()
Get the class var num_surfaces_per_frame.
BatchPolicy::total_buf_available
unsigned int total_buf_available
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:266
NvStreamMux::lock
virtual void lock()
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux.h:85
TimePoint
std::chrono::time_point< std::chrono::steady_clock, NanoSecondsType > TimePoint
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:42
EventWrapper
Definition: sources/libs/nvstreammux/include/nvstreammux_pads.h:132
NvStreamMux::get_remaining_unbatched_buffers_from_all_sources
unsigned int get_remaining_unbatched_buffers_from_all_sources()
Get the remaining unbatched ready buffers from all sources.
NvStreamMux::reset_stop
void reset_stop()
SinkPad
Definition: sources/libs/nvstreammux/include/nvstreammux_pads.h:172
NvStreamMux::unlock
virtual void unlock()
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux.h:93
SOURCE_STATE_IDLE
@ SOURCE_STATE_IDLE
Definition: sources/libs/nvstreammux/include/nvstreammux_pads.h:42
nvstreammux_batch.h
NvStreamMux::reset_pad
void reset_pad(SinkPad *pad)
Reset a pad for streaming.
Batch
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:133
BufferWrapper
Definition: sources/libs/nvstreammux/include/nvstreammux_pads.h:97
NvStreamMux::remove_pad
void remove_pad(unsigned int id)
NvStreamMux::get_min_fps_duration
NanoSecondsType get_min_fps_duration()
Get the Batch Policy minimum fps duration calculated from min overall fps config.
SOURCE_STATE
SOURCE_STATE
Definition: sources/libs/nvstreammux/include/nvstreammux_pads.h:40
NvStreamMux::apply_throttle
void apply_throttle(bool stop_when_input_buffer=false)
Apply the throttle delay and wait for the time (max-fps cfg) or until a new buffer come in.
NvStreamMux::set_policy
void set_policy(BatchPolicyConfig cfg)
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux.h:67
NvStreamMux::push_batch
bool push_batch(NvDsBatchBufferWrapper *out_buf, SourcePad *src_pad)
NvStreamMux::set_frame_num_reset_on_stream_reset
void set_frame_num_reset_on_stream_reset(bool reset)
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux.h:77
nvstreammux_debug.h
NvStreamMux::update_pad_mimetype
void update_pad_mimetype(unsigned int id, PAD_MIME_TYPE mime_type)
SourcePad
Definition: sources/libs/nvstreammux/include/nvstreammux_pads.h:145
NvStreamMux::set_frame_duration
void set_frame_duration(unsigned long)
NvStreamMux::get_pad_state
SOURCE_STATE get_pad_state(SinkPad *sinkPad)
NvStreamMux::get_all_pads_eos
bool get_all_pads_eos()
Get all_pads_eos.
NvStreamMux::wait
virtual void wait()
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux.h:101
NvStreamMux::add_sink
virtual void add_sink(SinkPad *pad)
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux.h:209
NvStreamMux::add_buffer
virtual void add_buffer(unsigned int pad_id, BufferWrapper *buffer)
PAD_MIME_TYPE
PAD_MIME_TYPE
Definition: sources/libs/nvstreammux/include/nvstreammux_pads.h:64
NvStreamMux::handle_eos
virtual bool handle_eos(SINK_EVENT et, unsigned int source_id, EventWrapper *event)
NvStreamMux::set_num_surfaces_per_frame
void set_num_surfaces_per_frame(unsigned int num)
Set the class var num_surfaces_per_frame and batch_policy.set_num_surfaces()
NvStreamMux::handle_pause_play
void handle_pause_play()
NvStreamMux::set_frame_num_reset_on_eos
void set_frame_num_reset_on_eos(bool reset)
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux.h:73
_NvDsBatchMeta
Holds information about a formed batch containing frames from different sources.
Definition: sources/includes/nvdsmeta.h:262
NvStreamMux::handle_ready_pause
void handle_ready_pause()
NvStreamMux::handle_stop
void handle_stop()
NvStreamMux::handle_flush_stop
virtual bool handle_flush_stop(SINK_EVENT et, unsigned int source_id, EventWrapper *event)
NvStreamMux::get_pad_mimetype
PAD_MIME_TYPE get_pad_mimetype(unsigned int id)
NvStreamMux
Definition: sources/libs/nvstreammux/include/nvstreammux.h:39
NvStreamMux::get_source_id_with_earliest_buffer
unsigned int get_source_id_with_earliest_buffer()
NvStreamMux::push_events
void push_events()
NanoSecondsType
std::chrono::duration< double, std::nano > NanoSecondsType
Definition: sources/libs/nvstreammux/include/nvstreammux_pads.h:27
NvStreamMux::set_synch_buffer_iface
void set_synch_buffer_iface(ISynchronizeBuffer *synch_buffer_iface)
Set the user interface for buffer synchronization Note: This API shall be called before set_policy()
NvStreamMux::copy_batch
unsigned int copy_batch(NvDsBatchBufferWrapper *out_buf, NvDsBatchMeta *)
NvStreamMux::notify_all
virtual void notify_all()
NvStreamMux::handle_eos_cascaded
virtual bool handle_eos_cascaded(SINK_EVENT et, unsigned int source_id, EventWrapper *event)
NvStreamMux::set_pts_offset
void set_pts_offset(gulong offset)
BatchPolicy::set_synch_buffer_iface
void set_synch_buffer_iface(ISynchronizeBuffer *synch_buffer_iface)
NvStreamMux::handle_stream_reset
virtual bool handle_stream_reset(SINK_EVENT et, unsigned int source_id, EventWrapper *event)
NvStreamMux::remove_sink
virtual void remove_sink(SinkPad pad)
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux.h:215
_GstNvStreamMuxClass
Definition: sources/gst-plugins/gst-nvmultistream2/gstnvstreammux.h:136
NvStreamMux::set_batch_push_timeout
void set_batch_push_timeout(unsigned int timeout)
set batch_policy.set_batch_push_timeout()
NvStreamMux::handle_play_pause
void handle_play_pause()
NvStreamMux::handle_segment
virtual bool handle_segment(SINK_EVENT et, unsigned int source_id, EventWrapper *event)
NvStreamMux::add_pad
void add_pad(unsigned int id, SinkPad *pad)
NvStreamMux::set_all_pads_eos
void set_all_pads_eos(bool eos)
Set all_pads_eos.
ISynchronizeBuffer
Definition: sources/libs/nvstreammux/include/nvstreammux_pads.h:347
BatchPolicy
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:188
BatchPolicyConfig
Definition: sources/libs/nvstreammux/include/nvstreammux_batch.h:61
NvStreamMux::get_pad
SinkPad * get_pad(unsigned int pad_id)
NvStreamMux::get_batch_size
unsigned int get_batch_size()
return batch_policy.get_batch_size()
NvStreamMux::NvStreamMux
NvStreamMux(SourcePad *srcpad, INvStreammuxDebug *a_debug_iface=nullptr)
Definition: 9.1/sources/libs/nvstreammux/include/nvstreammux.h:45
INvStreammuxDebug
Definition: sources/libs/nvstreammux/include/nvstreammux_debug.h:21