NVIDIA DeepStream SDK API Reference

7.0 Release
gstnvstreammux_impl.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: 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 
13 #ifndef __GST_NVSTREAMMUX_IMPL_H__
14 #define __GST_NVSTREAMMUX_IMPL_H__
15 
16 #include <gst/gst.h>
17 #include <gst/video/video.h>
18 #include "nvstreammux_pads.h"
19 #include "nvstreammux_batch.h"
20 #include "gstnvstreammux.h"
21 #include "gstnvstreampad.h"
22 #include "nvds_latency_meta.h"
24 #include "gstnvstreammuxdebug.h"
25 
26 extern "C"
27 {
28 static void
29 mem_buf_unref_callback (gpointer data);
30 }
31 
33 {
34 
35  public:
36  GstCommonBufferAPI(GstElement* a_mux) : mux(a_mux) {}
37 
38  void add_component_latency_metadata(std::shared_ptr<BufferWrapper> src_buffer, GstBuffer * gst_batch_buffer, NvDsBatchMeta* batch_meta, unsigned int source_id, unsigned int pad_index, unsigned int frame_number);
39  void update_component_latency_metadata(NvDsBatchMeta* dest_batch_meta);
40 
41  private:
42  GstElement* mux;
43 };
44 
46 {
47 
48  public :
49  GstBufferWrapper(void * buffer, ENTRY_TYPE et, BATCH_SEQUENCE_TYPE bt, GstClockTime a_ntp_ts, GstClockTime a_buf_pts, GstClockTime a_duration, unsigned int id=0) : BufferWrapper(buffer, et, bt, (uint64_t)a_buf_pts)
50  {
53  unwrap();
54  LOGD("GstBufferWrapper constructor %p gst_buffer refcount %d\n", this, ((GstBuffer *) (wrapped))->mini_object.refcount);
55  ntp_ts = a_ntp_ts;
56  buf_pts = a_buf_pts;
57  duration = a_duration;
58  stream_id = id;
61  {
63  }
64  is_nvmm = false;
65  }
66 
68  {
69  free();
70  }
71 
72  void * unwrap()
73  {
74  void * ret = NULL;
75 
76  GstMapInfo info = {0}; // TBD FIXME = GST_MAP_INFO_INIT;
77 
78  if (gst_buffer_map ((GstBuffer *)wrapped, &info, GST_MAP_READ))
79  {
80  raw = ret = info.data;
81  rawSize = info.size;
82  gst_buffer_unmap ((GstBuffer * )wrapped, &info);
83 
84  }
85  return ret;
86 
87  }
88  void free()
89  {
90  LOGD("GstBufferWrapper destructor %p raw %p gst_buf refcount %d\n", this, raw, ((GstBuffer *) (wrapped))->mini_object.refcount);
91  gst_buffer_unref(GST_BUFFER (wrapped));
92  }
93 
94  void SetAudioParams(NvBufAudioParams aAudioParams)
95  {
96  audioParams = aAudioParams;
97  }
98 
99  void SetMemTypeNVMM(bool isNVMM)
100  {
101  is_nvmm = isNVMM;
102  }
103 
105  {
106  return is_nvmm;
107  }
108 
110 
111  GstClockTime ntp_ts;
112  GstClockTime buf_pts;
113  GstClockTime duration;
114  unsigned int stream_id;
116  bool is_nvmm;
117 };
118 
128 {
129 
130  public :
131  GstBatchBufferWrapper(GstNvStreamMux * mux, unsigned int size, bool is_raw) : NvDsBatchBufferWrapper(size) , is_raw(is_raw), mux(mux), api((GstElement*)mux)
132  {
133  gst_buffer = nullptr;
134  batch = malloc(sizeof(NvBufSurface));
135  memset (batch, 0 , sizeof(NvBufSurface));
136  ((NvBufSurface *)batch)->surfaceList = (NvBufSurfaceParams *)malloc(sizeof(NvBufSurfaceParams) * size);
137  ((NvBufSurface *)batch)->numFilled = 0;
138  ((NvBufSurface *)batch)->batchSize = size;
139  ((NvBufSurface *)batch)->memType = NVBUF_MEM_DEFAULT;
140  if(!is_raw)
141  {
142  gst_buffer =
143  gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, (NvBufSurface *)batch, sizeof (NvBufSurface),
144  0, sizeof (NvBufSurface), (void *)this, mem_buf_unref_callback);
145  //gst_buffer_ref(gst_buffer);
146  }
147  else
148  {
149  raw_batch = gst_buffer_list_new_sized (size);
150  }
151  }
152 
153  void unref() override;
154  void reset_batch();
155  void copy_meta(unsigned int id, std::shared_ptr<BufferWrapper> src_buffer, unsigned int batch_id, unsigned int frame_number, unsigned int num_surfaces_per_frame, NvDsBatchMeta * dest_batch_meta, unsigned int source_id) override;
156  unsigned int copy_buf(std::shared_ptr<BufferWrapper> src, unsigned int pos) override;
157  bool push(SourcePad * src_pad, TimePoint current_play_start, NanoSecondsType accum_dur) override;
158  bool push(SourcePad * src_pad, unsigned long pts) override;
159  void unref_gst_bufs();
160 
161  GstBufferList *raw_batch;
163  bool is_raw;
165  std::vector<std::shared_ptr<GstBufferWrapper> > gst_in_bufs;
166 
167  private:
168  unsigned int copy_buf_impl(std::shared_ptr<BufferWrapper> buf, unsigned int pos);
169  GstCommonBufferAPI api;
170 };
171 
172 extern "C"
173 {
174 static void
175 mem_buf_unref_callback (gpointer data)
176 {
177 
178  if(data != NULL)
179  {
181  batch->unref_gst_bufs();
182  delete batch;
183  }
184 
185 }
186 }
187 
188 class GstSinkPad : public SinkPad
189 {
190  public :
191  GstSinkPad(GstNvStreamMux * elem, unsigned int id, GstPad * pad) : SinkPad(id, (void *) pad), element(elem)
192  {
193  new_add = true;
194  ntp_calc = nullptr;
195  cascaded_eos = false;
196  }
198  {
199  if(ntp_calc)
200  {
201  gst_nvds_ntp_calculator_free (ntp_calc);
202  }
203  }
204  GstVideoInfo vid_info;
205  void push_event(SourcePad * src_pad, QueueEntry *);
206  friend class GstSourcePad;
207 
209  GstClockTime frame_duration)
210  {
211  if(!ntp_calc)
212  {
213  ntp_calc =
214  gst_nvds_ntp_calculator_new (mode, frame_duration,
215  GST_ELEMENT (element), id);
216  }
217  return ntp_calc;
218  }
219 
220  private :
221  //GstPad * wrapped;
222  GstNvStreamMux * element;
223  bool new_add;
224  bool cascaded_eos;
225  GstNvDsNtpCalculator *ntp_calc;
226 };
227 
228 class GstSourcePad : public SourcePad
229 {
230  public :
231  GstSourcePad(GstNvStreamMux * elem, GstPad * pad, unsigned int id) : SourcePad(id, (void *) pad), element(elem)
232  {
233 
234  }
235  ~GstSourcePad();
236  GstVideoInfo vid_info;
237  friend class GstSinkPad;
238  private :
239  //GstPad * wrapped;
240  GstNvStreamMux * element;
241 };
242 #endif
BATCH_SEQUENCE_TYPE
BATCH_SEQUENCE_TYPE
Definition: nvstreammux_pads.h:51
GstBufferWrapper::GstBufferWrapper
GstBufferWrapper(void *buffer, ENTRY_TYPE et, BATCH_SEQUENCE_TYPE bt, GstClockTime a_ntp_ts, GstClockTime a_buf_pts, GstClockTime a_duration, unsigned int id=0)
Definition: gstnvstreammux_impl.h:49
_GstNvStreamMux
Definition: gstnvstreammux.h:42
GstBatchBufferWrapper::is_raw
bool is_raw
Definition: gstnvstreammux_impl.h:163
NvBufAudioParams
Definition: nvbufaudio.h:82
GstSinkPad
Definition: gstnvstreammux_impl.h:188
NvDsBatchBufferWrapper
Definition: nvstreammux_batch.h:108
GstCommonBufferAPI
Definition: gstnvstreammux_impl.h:32
GstBatchBufferWrapper::unref_gst_bufs
void unref_gst_bufs()
GstSinkPad::push_event
void push_event(SourcePad *src_pad, QueueEntry *)
mem_buf_unref_callback
static void mem_buf_unref_callback(gpointer data)
Definition: gstnvstreammux_impl.h:175
NvBufSurface
Holds information about batched buffers.
Definition: nvbufsurface.h:509
GstBufferWrapper::stream_id
unsigned int stream_id
Definition: gstnvstreammux_impl.h:114
ENTRY_TYPE
ENTRY_TYPE
Definition: nvstreammux_pads.h:43
GstBufferWrapper::SetMemTypeNVMM
void SetMemTypeNVMM(bool isNVMM)
Definition: gstnvstreammux_impl.h:99
SinkPad
Definition: nvstreammux_pads.h:167
GstBufferWrapper::SetAudioParams
void SetAudioParams(NvBufAudioParams aAudioParams)
Definition: gstnvstreammux_impl.h:94
GstBatchBufferWrapper::raw_batch
GstBufferList * raw_batch
Definition: gstnvstreammux_impl.h:161
BufferWrapper
Definition: nvstreammux_pads.h:92
GstBufferWrapper::~GstBufferWrapper
~GstBufferWrapper()
Definition: gstnvstreammux_impl.h:67
nvds_latency_meta.h
gstnvstreammuxdebug.h
nvds_latency_meta_internal.h
NVBUF_MEM_DEFAULT
@ NVBUF_MEM_DEFAULT
Specifies the default memory type, i.e.
Definition: nvbufsurface.h:311
GstSourcePad::GstSourcePad
GstSourcePad(GstNvStreamMux *elem, GstPad *pad, unsigned int id)
Definition: gstnvstreammux_impl.h:231
GstBatchBufferWrapper::gst_buffer
GstBuffer * gst_buffer
Definition: gstnvstreammux_impl.h:162
SourcePad
Definition: nvstreammux_pads.h:140
GstBufferWrapper::free
void free()
Definition: gstnvstreammux_impl.h:88
GstBatchBufferWrapper::gst_in_bufs
std::vector< std::shared_ptr< GstBufferWrapper > > gst_in_bufs
Definition: gstnvstreammux_impl.h:165
GstSinkPad::get_ntp_calc
GstNvDsNtpCalculator * get_ntp_calc(GstNvDsNtpCalculatorMode mode, GstClockTime frame_duration)
Definition: gstnvstreammux_impl.h:208
NvBufSurfaceParams
Hold the information of single buffer in the batch.
Definition: nvbufsurface.h:476
BufferWrapper::raw
void * raw
Definition: nvstreammux_pads.h:103
BatchBufferWrapper::batch
void * batch
Definition: nvstreammux_batch.h:102
GstBatchBufferWrapper::copy_buf
unsigned int copy_buf(std::shared_ptr< BufferWrapper > src, unsigned int pos) override
nvds_get_current_system_timestamp
gdouble nvds_get_current_system_timestamp(void)
BufferWrapper::rawSize
uint32_t rawSize
Definition: nvstreammux_pads.h:104
_NvDsBatchMeta
Holds information about a formed batch containing frames from different sources.
Definition: nvdsmeta.h:241
GstNvDsNtpCalculatorMode
GstNvDsNtpCalculatorMode
Definition: gstnvstreammux_ntp.h:25
GstBufferWrapper
Definition: gstnvstreammux_impl.h:45
GstBatchBufferWrapper::unref
void unref() override
GstBatchBufferWrapper::GstBatchBufferWrapper
GstBatchBufferWrapper(GstNvStreamMux *mux, unsigned int size, bool is_raw)
Definition: gstnvstreammux_impl.h:131
GstBufferWrapper::buffer_wrapper_creation_time
gdouble buffer_wrapper_creation_time
Definition: gstnvstreammux_impl.h:115
LOGD
#define LOGD(...)
Definition: gstnvstreammuxdebug.h:22
GstBufferWrapper::is_nvmm
bool is_nvmm
Definition: gstnvstreammux_impl.h:116
SinkPad::id
unsigned int id
Definition: nvstreammux_pads.h:312
GstCommonBufferAPI::update_component_latency_metadata
void update_component_latency_metadata(NvDsBatchMeta *dest_batch_meta)
gstnvstreampad.h
GstBatchBufferWrapper::mux
GstNvStreamMux * mux
Definition: gstnvstreammux_impl.h:164
GstBatchBufferWrapper::push
bool push(SourcePad *src_pad, TimePoint current_play_start, NanoSecondsType accum_dur) override
SourcePad::id
unsigned int id
Definition: nvstreammux_pads.h:162
GstBatchBufferWrapper::reset_batch
void reset_batch()
GstSinkPad::~GstSinkPad
~GstSinkPad()
Definition: gstnvstreammux_impl.h:197
GstBufferWrapper::IsMemTypeNVMM
bool IsMemTypeNVMM()
Definition: gstnvstreammux_impl.h:104
gst_nvds_ntp_calculator_new
GstNvDsNtpCalculator * gst_nvds_ntp_calculator_new(GstNvDsNtpCalculatorMode mode, GstClockTime frame_duration, GstElement *elem, guint source_id)
NanoSecondsType
std::chrono::duration< double, std::nano > NanoSecondsType
Definition: nvstreammux_pads.h:22
GstBufferWrapper::buf_pts
GstClockTime buf_pts
Definition: gstnvstreammux_impl.h:112
GstBatchBufferWrapper
TODO re-design GstBatchBufferWrapper to be video specific and NvDsBatchBufferWrapper generic ; Also r...
Definition: gstnvstreammux_impl.h:127
GstBatchBufferWrapper::copy_meta
void copy_meta(unsigned int id, std::shared_ptr< BufferWrapper > src_buffer, unsigned int batch_id, unsigned int frame_number, unsigned int num_surfaces_per_frame, NvDsBatchMeta *dest_batch_meta, unsigned int source_id) override
GstCommonBufferAPI::add_component_latency_metadata
void add_component_latency_metadata(std::shared_ptr< BufferWrapper > src_buffer, GstBuffer *gst_batch_buffer, NvDsBatchMeta *batch_meta, unsigned int source_id, unsigned int pad_index, unsigned int frame_number)
GstBufferWrapper::unwrap
void * unwrap()
Definition: gstnvstreammux_impl.h:72
GstBuffer
struct _GstBuffer GstBuffer
Definition: idatatype.h:19
QueueEntry
Definition: nvstreammux_pads.h:76
QueueEntry::wrapped
void * wrapped
Definition: nvstreammux_pads.h:84
GstSinkPad::vid_info
GstVideoInfo vid_info
Definition: gstnvstreammux_impl.h:204
GstSourcePad::vid_info
GstVideoInfo vid_info
Definition: gstnvstreammux_impl.h:236
nvstreammux_batch.h
GstBufferWrapper::duration
GstClockTime duration
Definition: gstnvstreammux_impl.h:113
GstSourcePad
Definition: gstnvstreammux_impl.h:228
GstNvDsNtpCalculator
struct _GstNvDsNtpCalculator GstNvDsNtpCalculator
Definition: gstnvstreammux_ntp.h:45
GstBufferWrapper::audioParams
NvBufAudioParams audioParams
Definition: gstnvstreammux_impl.h:109
GstBufferWrapper::ntp_ts
GstClockTime ntp_ts
Definition: gstnvstreammux_impl.h:111
gstnvstreammux.h
GstCommonBufferAPI::GstCommonBufferAPI
GstCommonBufferAPI(GstElement *a_mux)
Definition: gstnvstreammux_impl.h:36
nvstreammux_pads.h
GstSourcePad::~GstSourcePad
~GstSourcePad()
gst_nvds_ntp_calculator_free
void gst_nvds_ntp_calculator_free(GstNvDsNtpCalculator *calc)
nvds_enable_latency_measurement
#define nvds_enable_latency_measurement
Defines a pseudo-variable whose value is the return value of nvds_get_enable_latency_measurement().
Definition: nvds_latency_meta.h:172
TimePoint
std::chrono::time_point< std::chrono::steady_clock, NanoSecondsType > TimePoint
Definition: nvstreammux_batch.h:37
GstSinkPad::GstSinkPad
GstSinkPad(GstNvStreamMux *elem, unsigned int id, GstPad *pad)
Definition: gstnvstreammux_impl.h:191