NVIDIA DeepStream SDK API Reference

6.4 Release
nvstreammux.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef __NVSTREAMMUX_H__
25 #define __NVSTREAMMUX_H__
26 
27 #include <chrono>
28 #include <unordered_map>
29 #include <map>
30 #include <list>
31 #include "nvbufsurface.h"
32 #include "nvstreammux_batch.h"
33 #include "nvstreammux_debug.h"
34 
35 //G_BEGIN_DECLS
36 
39 
40 /*
41  * @brief low level helper for core logic, this is agnostic of gstreamer
42  * or any other wrapper on higher level
43  */
44 
46 {
47 
48 
49  public :
50 
51  NvStreamMux(SourcePad * srcpad, INvStreammuxDebug* a_debug_iface=nullptr) : inputs(), src_pad(srcpad), cv(), cv_throttle_wait(), mutex(), mutex_throttle_wait()
52  {
53  batch = NULL;
54 
55  stop_task = false;
56  num_queues_empty = 0;
57  num_sources_idle = 0;
58  pads_got_eos_and_empty_in_q = 0;
59  num_pads_eos = 0;
60  cur_frame_pts = 0;
61  state = SOURCE_STATE_IDLE;
62  num_surfaces_per_frame = 1;
63  all_pads_eos = false;
64  extra_throttle_wait_time = std::chrono::microseconds(0);
65  got_first_buffer = false;
66  synch_buffer = nullptr;
67  debug_iface = a_debug_iface;
68  }
69 
70  /*
71  * @brief functions to maintain internal variables based on config
72  */
74  {
75  batch_policy = BatchPolicy(cfg, &inputs, debug_iface);
76  batch_policy.total_buf_available = 0;
78  }
79  void set_frame_num_reset_on_eos(bool reset)
80  {
81  frame_num_reset_on_eos = reset;
82  }
84  {
85  frame_num_reset_on_stream_reset = reset;
86  }
87 
88  /*
89  * @brief acquire lock on mux
90  */
91  virtual void lock()
92  {
93  mutex.lock();
94  };
95 
96  /*
97  * @brief release lock on mux
98  */
99  virtual void unlock()
100  {
101  mutex.unlock();
102  };
103 
104  /*
105  * @brief release lock on mux
106  */
107  virtual void wait(){};
108 
109  /*
110  * @brief release lock on mux
111  */
112  virtual void notify_all();
113 
114  /*
115  * @brief get_pad for specified pad id
116  * @param pad_id, id of pad to lookup
117  * @return pointer to SinkPad object for the pad_id
118  */
119  SinkPad * get_pad(unsigned int pad_id);
120 
121  /*
122  * @brief add_pad for specified pad id
123  * @param pad_id, id of pad to lookup
124  * @return pointer to SinkPad object for the pad_id
125  */
126  void add_pad(unsigned int id, SinkPad * pad);
127 
128  /*
129  * @brief
130  * @param pad_id, id of pad to lookup
131  * @param mime_type [IN]
132  * @return NA
133  */
134  void update_pad_mimetype(unsigned int id, PAD_MIME_TYPE mime_type);
135 
136  /*
137  * @brief
138  * @param id [IN] id of pad to lookup
139  * @return The configured mime type for the sinkpad at pad_id==id
140  */
141  PAD_MIME_TYPE get_pad_mimetype(unsigned int id);
142 
143  /*
144  * @brief get_pad for specified pad id
145  * @param pad_id, id of pad to lookup
146  * @return pointer to SinkPad object for the pad_id
147  */
148  void remove_pad(unsigned int id);
149 
150  /*
151  * @brief Construct the NvStreamMux from config, this would typically
152  * come from a high level config file
153  */
154  //virtual update_source(SINK_EVENT event, unsigned long source_id);
155 
156 
157  /*
158  * @brief handle the eos event on pad
159  * @param event [IN] sink event
160  * @param source_id[IN] id of source, in deepstream app pad_id
161  * of gstreamer
162  * source pad is same as this id
163  * @param EventWrapper [IN] event coming on sink pad
164  *
165  */
166  virtual bool handle_eos(SINK_EVENT et, unsigned int source_id, EventWrapper * event);
167 
168  /*
169  * @brief handle the eos event on pad for cascaded muxers
170  * @param event [IN] sink event
171  * @param source_id[IN] id of source, in deepstream app pad_id
172  * of gstreamer
173  * source pad is same as this id
174  * @param EventWrapper [IN] event coming on sink pad
175  *
176  */
177  virtual bool handle_eos_cascaded(SINK_EVENT et, unsigned int source_id, EventWrapper * event);
178  /*
179  * @brief handle the segment event on pad
180  * @param event [IN] sink event
181  * @param source_id[IN] id of source, in deepstream app pad_id
182  * of gstreamer
183  * source pad is same as this id
184  * @param EventWrapper [IN] event coming on sink pad
185  *
186  */
187  virtual bool handle_segment(SINK_EVENT et, unsigned int source_id, EventWrapper * event);
188 
189  /*
190  * @brief handle the GST_NVEVENT_STREAM_RESET event on pad
191  * @param event [IN] sink event
192  * @param source_id[IN] id of source, in deepstream app pad_id
193  * of gstreamer
194  * source pad is same as this id
195  * @param EventWrapper [IN] event coming on sink pad
196  *
197  */
198  virtual bool handle_stream_reset(SINK_EVENT et, unsigned int source_id, EventWrapper * event);
199 
200  /*
201  * @brief handle the FLUSH_STOP event on pad
202  * @param event [IN] sink event
203  * @param source_id[IN] id of source, in deepstream app pad_id
204  * of gstreamer
205  * source pad is same as this id
206  * @param EventWrapper [IN] event coming on sink pad
207  *
208  */
209  virtual bool handle_flush_stop(SINK_EVENT et, unsigned int source_id, EventWrapper * event);
210 
211  /*
212  * @brief add sink pad
213  * @param pad [IN] the pad to be added
214  */
215  virtual void add_sink(SinkPad * pad){};
216 
217  /*
218  * @brief remove sink pad
219  * @param pad [IN] the pad to be added
220  */
221  virtual void remove_sink(SinkPad pad){};
222 
223  /*
224  * @brief add buffer to sink pad
225  * @param pad [IN] the pad to be added
226  * @param buffer [IN] the buffer to be added
227  */
228  //virtual void add_buffer(SinkPad * pad, BufferWrapper * buffer);
229  virtual void add_buffer(unsigned int pad_id, BufferWrapper * buffer);
230 
231  /*
232  * @brief task for processing queued buffers and events,
233  * according to batching configuration
234  * @param pad [IN] the pad to be added
235  * @param buffer [IN] the buffer to be added
236  */
238 
239  /*
240  * @brief function to push the buffer out of mux to designated SourcePad
241  * @param out_buf [IN] the batch to be pushed
242  * @param src_pad [IN] the to push to
243  * @return true on successfully pushing the batch buffer on src_pad;
244  * false otherwise
245  */
246  bool push_batch(NvDsBatchBufferWrapper * out_buf, SourcePad * src_pad);
247 
248  /*
249  * @brief function to push events out of mux
250  */
251 
252  void push_events();
253 
254 
255  /*
256  * @brief function to copy the individual buffers and metadata to batch
257  * @param out_buf [IN] the batch buffer
258  * @param buffer [IN] the metadata
259  * @return the total number of buffers copied
260  */
261  unsigned int copy_batch(NvDsBatchBufferWrapper * out_buf, NvDsBatchMeta *);
262 
263  /*
264  * @brief functions to calculate play duration, called while processing
265  * relevant gstreamer events
266  * @return void
267  */
268  void handle_pause_play();
269  void handle_play_pause();
270  void handle_ready_pause();
271 
272  /*
273  * @brief functions called to process gstreamer event
274  * GST_STATE_CHANGE_READY_TO_PAUSED
275  * @return void
276  */
277  void handle_stop();
278  void reset_stop();
279 
280 
281  /*
282  * @brief function to set value of frame_duration_nsec
283  * @param offset [IN] value to be set
284  */
285  void set_frame_duration(unsigned long);
286 
287  /*
288  * @brief function to set value of cur_frame_pts
289  * @param offset [IN] value to be set
290  */
291  void set_pts_offset(gulong offset);
292 
293  /*
294  * @brief function to get current state of PAD
295  * @param sinkPad [IN] SinkPad Pointer
296  * @param SOURCE_STATE [OUT] State of sinkpad
297  */
299 
304  void set_batch_size(unsigned int size);
305 
310  void set_batch_push_timeout(unsigned int timeout);
311 
316  unsigned int get_batch_size();
317 
322  unsigned int get_config_batch_size();
323 
329  void set_num_surfaces_per_frame(unsigned int num);
330 
335  unsigned int get_num_surfaces_per_frame();
336 
341  void set_all_pads_eos(bool eos);
342 
347  void reset_pad(SinkPad* pad);
348 
353  bool get_all_pads_eos();
354 
360  void set_synch_buffer_iface(ISynchronizeBuffer* synch_buffer_iface);
361 
367 
368  unsigned int get_source_id_with_earliest_buffer();
369 
376 
384  void apply_throttle(bool stop_when_input_buffer=false);
385 
386  private :
387  std::unordered_map<unsigned int, SinkPad *> inputs;
388  SourcePad * src_pad;
389  SOURCE_STATE state;
390  unsigned int num_queues_empty;
391  unsigned int num_sources_idle;
392  unsigned int pads_got_eos_and_empty_in_q;
393  unsigned int num_pads_eos;
394  unsigned int num_surfaces;
395  std::condition_variable cv;
396  std::condition_variable cv_throttle_wait;
399  std::mutex mutex;
400  std::mutex mutex_throttle_wait;
401  Batch * batch;
402  /*
403  * @brief map of sources for a priority
404  */
405  std::multimap<int, int> sources;
406  TimePoint current_play_start_time;
407  TimePoint last_pause_time;
408  NanoSecondsType accum_play_dur;
409  unsigned long frame_duration_nsec;
410  unsigned long cur_frame_pts;
411  bool stop_task;
412  NanoSecondsType extra_throttle_wait_time;
413 
414  BatchPolicy batch_policy;
415  unsigned int num_surfaces_per_frame;
417  bool all_pads_eos;
418  bool got_first_buffer;
419  bool frame_num_reset_on_eos = false;
420  bool frame_num_reset_on_stream_reset = false;
421  INvStreammuxDebug* debug_iface;
422 
423  public:
425 };
426 
427 //G_END_DECLS
428 #endif
NvStreamMux::set_batch_size
void set_batch_size(unsigned int size)
set batch_policy.set_batch_size()
NvStreamMux::push_loop
bool push_loop(NvDsBatchBufferWrapper *out_buf, NvDsBatchMeta *)
_GstNvStreamMux
Definition: gstnvstreammux.h:53
NvStreamMux::get_config_batch_size
unsigned int get_config_batch_size()
return batch_policy.get_config_batch_size()
NvDsBatchBufferWrapper
Definition: nvstreammux_batch.h:119
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: nvstreammux_batch.h:272
NvStreamMux::lock
virtual void lock()
Definition: nvstreammux.h:91
EventWrapper
Definition: nvstreammux_pads.h:138
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: nvstreammux_pads.h:178
NvStreamMux::unlock
virtual void unlock()
Definition: nvstreammux.h:99
NvStreamMux::reset_pad
void reset_pad(SinkPad *pad)
Reset a pad for streaming.
SINK_EVENT
SINK_EVENT
Definition: nvstreammux_pads.h:35
Batch
Definition: nvstreammux_batch.h:139
BufferWrapper
Definition: nvstreammux_pads.h:103
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: nvstreammux_pads.h:46
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: nvstreammux.h:73
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: nvstreammux.h:83
NvStreamMux::update_pad_mimetype
void update_pad_mimetype(unsigned int id, PAD_MIME_TYPE mime_type)
SourcePad
Definition: nvstreammux_pads.h:151
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: nvstreammux.h:107
NvStreamMux::add_sink
virtual void add_sink(SinkPad *pad)
Definition: nvstreammux.h:215
NvStreamMux::add_buffer
virtual void add_buffer(unsigned int pad_id, BufferWrapper *buffer)
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: nvstreammux.h:79
_NvDsBatchMeta
Holds information about a formed batch containing frames from different sources.
Definition: nvdsmeta.h:240
NvStreamMux::handle_ready_pause
void handle_ready_pause()
NvStreamMux::handle_stop
void handle_stop()
nvstreammux_debug.h
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: nvstreammux.h:45
NvStreamMux::get_source_id_with_earliest_buffer
unsigned int get_source_id_with_earliest_buffer()
NvStreamMux::synch_buffer
ISynchronizeBuffer * synch_buffer
Definition: nvstreammux.h:424
NvStreamMux::push_events
void push_events()
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)
NanoSecondsType
std::chrono::duration< double, std::nano > NanoSecondsType
Definition: nvstreammux_pads.h:33
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: nvstreammux.h:221
_GstNvStreamMuxClass
Definition: gstnvstreammux.h:142
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: nvstreammux_pads.h:353
nvstreammux_batch.h
PAD_MIME_TYPE
PAD_MIME_TYPE
Definition: nvstreammux_pads.h:70
nvbufsurface.h
BatchPolicy
Definition: nvstreammux_batch.h:194
BatchPolicyConfig
Definition: nvstreammux_batch.h:67
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()
SOURCE_STATE_IDLE
@ SOURCE_STATE_IDLE
Definition: nvstreammux_pads.h:48
NvStreamMux::NvStreamMux
NvStreamMux(SourcePad *srcpad, INvStreammuxDebug *a_debug_iface=nullptr)
Definition: nvstreammux.h:51
INvStreammuxDebug
Definition: nvstreammux_debug.h:27
TimePoint
std::chrono::time_point< std::chrono::steady_clock, NanoSecondsType > TimePoint
Definition: nvstreammux_batch.h:48