NVIDIA DeepStream SDK API Reference

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