NVIDIA DeepStream SDK API Reference

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