NVIDIA DeepStream SDK API Reference

9.1 Release
sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-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 
27 #ifndef __GSTNVINFERSERVER_IMPL_H__
28 #define __GSTNVINFERSERVER_IMPL_H__
29 
30 #include <glib.h>
31 #include <gst/gst.h>
32 #include <string.h>
33 #include <sys/time.h>
34 
35 #include <condition_variable>
36 #include <list>
37 #include <memory>
38 #include <mutex>
39 #include <thread>
40 #include <vector>
41 
42 #include <nvbufsurface.h>
43 #include <nvbufsurftransform.h>
44 
45 #include "infer_icontext.h"
46 #include "infer_utils.h"
47 
48 #include "nvdsmeta.h"
49 #include "nvtx3/nvToolsExt.h"
50 
52 #include "infer_datatypes.h"
53 #include "infer_post_datatypes.h"
54 #include "nvdsinferserver_config.pb.h"
55 #include "nvdsinferserver_plugin.pb.h"
56 
61 void gst_nvinfer_server_logger(uint32_t unique_id, NvDsInferLogLevel log_level,
62  const char* log_message, void* user_ctx);
63 
64 namespace dsis = nvdsinferserver;
65 namespace ic = nvdsinferserver::config;
66 
67 using SharedInferContext = std::shared_ptr<dsis::IInferContext>;
68 
69 namespace nvdsinferserver {
70 class BatchSurfaceBuffer;
71 }
72 
73 namespace gstnvinferserver {
74 
81  bool under_inference = FALSE;
92  InferClassificationOutput cached_info;
93 };
94 
95 using SharedObjHistory = std::shared_ptr<GstNvInferServerObjectHistory>;
96 using WeakObjHistory = std::weak_ptr<GstNvInferServerObjectHistory>;
97 
101 typedef std::unordered_map<uint64_t, SharedObjHistory>
103 
107 typedef struct {
111  gulong last_cleanup_frame_num = 0;
113  gulong last_seen_frame_num = 0;
115 
120  std::string configPath;
121  uint32_t maxBatchSize = 0;
122  int32_t interval = -1;
123  uint32_t uniqueId = 0;
124  uint32_t processMode = 0;
125  int32_t inferOnGieId = -1;
126  bool inputTensorFromMeta = false;
127  std::vector<int32_t> operateOnClassIds;
128  std::string classifierType;
129 };
130 
139 using RawOutputCallback = std::function<void(void *, NvDsInferNetworkInfo&,
140  NvDsInferLayerInfo*, uint32_t, uint32_t)>;
141 
142 struct RequestBuffer;
143 
148 private:
154  class ObjTrackingData {
155  public:
159  ObjTrackingData() {}
160 
166  bool initSource(uint32_t id) {
167  m_SourceInfo.emplace(id, GstNvInferServerSourceInfo{});
168  return true;
169  }
170 
176  void removeSource(uint32_t id) { m_SourceInfo.erase(id); }
177 
185  GstNvInferServerSourceInfo* findSource(int id);
186 
194  bool hasSource(uint32_t id) const {
195  return m_SourceInfo.find(id) != m_SourceInfo.end();
196  }
197 
206  SharedObjHistory findObjHistory(
207  uint32_t sourceId, uint64_t objId);
208 
217  SharedObjHistory newHistory(
218  uint32_t sourceId, uint64_t objId);
219 
224  void clear() { m_SourceInfo.clear(); }
225 
236  void clearUpHistory(uint64_t seqId);
237 
238  private:
241  std::unordered_map<uint32_t, GstNvInferServerSourceInfo> m_SourceInfo;
242 
245  gulong m_LastMapCleanupSeqId = 0;
246  };
247 
248  using FuncItem = std::function<void()>;
249 
250 public:
258 
263 
270  m_RawoutputCb = cb;
271  }
272 
287 
296 
310  NvBufSurface* inSurf, uint64_t seqId, void *gstBuf);
311 
317 
322  NvDsInferStatus queueOperation(FuncItem func);
323 
324 
334  bool addTrackingSource(uint32_t sourceId);
335 
345  void eraseTrackingSource(uint32_t sourceId);
346 
350  void resetIntervalCounter();
351 
352  // helper functions
353  uint32_t uniqueId() const { return m_GstProperties.uniqueId; }
354  const std::string& classifierType() const { return m_GstProperties.classifierType; }
355  uint32_t maxBatchSize() const;
356  nvtxDomainHandle_t nvtxDomain() {
357  return m_NvtxDomain.get();
358  }
359  bool isAsyncMode() const;
360  bool canSupportGpu(int gpuId) const;
361  NvDsInferStatus lastError() const;
362  const ic::PluginControl& config() const { return m_PluginConfig; }
363  void updateInterval(guint interval) {m_PluginConfig.mutable_input_control()->set_interval(interval);}
364 private:
378  NvDsInferStatus processFullFrame(NvDsBatchMeta* batchMeta,
379  NvBufSurface* inSurf, uint64_t seqId, void *gstBuf);
380 
398  NvDsInferStatus processObjects(NvDsBatchMeta* batchMeta,
399  NvBufSurface* inSurf, uint64_t seqId, void *gstBuf);
400 
413  NvDsInferStatus processInputTensor(
414  NvDsBatchMeta* batchMeta, NvBufSurface* inSurf, uint64_t seqId, void* gstBuf);
415 
428  NvDsInferStatus batchInference(
429  std::shared_ptr<RequestBuffer> reqBuf, std::vector<dsis::SharedBatchBuf> batchBuf);
430 
442  void InferenceDone(std::shared_ptr<RequestBuffer> req);
443 
449  NvDsInferStatus attachBatchDetection(
450  RequestBuffer* req, dsis::SharedIBatchBuffer output);
451 
457  NvDsInferStatus attachBatchClassification(
458  RequestBuffer* req, dsis::SharedIBatchBuffer output);
459 
465  NvDsInferStatus attachBatchSegmentation(
466  RequestBuffer* req, dsis::SharedIBatchBuffer output);
467 
478  NvDsInferStatus handleOutputTensors(RequestBuffer* req);
479 
480  void updateLastError(NvDsInferStatus s);
481 
486  ic::PluginControl& mutableConfig() { return m_PluginConfig; }
487  bool isClassify() const;
488  bool isDetection() const;
489  bool isSegmentation() const;
490  bool isOtherNetowrk() const;
491  bool isFullFrame() const;
492  bool maintainAspectRatio() const;
493  bool symmetricPadding() const;
494  bool needOutputTensorMeta() const;
495  bool hasCustomProcess() const;
496  uint32_t inferInterval() const { return m_PluginConfig.input_control().interval(); }
497  bool inputTensorFromMeta() const
498  {
499  return m_PluginConfig.infer_config().has_input_tensor_from_meta();
500  }
511  bool validatePluginConfig(
512  ic::PluginControl& config, const std::string& path,
513  const GstNvInferServerProperties& update);
514 
539  bool shouldInferObject(NvDsObjectMeta* obj_meta, uint32_t frameNum,
540  GstNvInferServerObjectHistory* history);
541 
551  bool outputLoop(FuncItem func);
552 
553 public:
555 
556 private:
557  using OutputThread = dsis::QueueThread<std::list<FuncItem>>;
558 
560  SharedInferContext m_InferCtx;
562  GstNvInferServer* m_GstPlugin = nullptr;
564  mutable std::mutex m_ProcessMutex;
566  ic::PluginControl m_PluginConfig;
568  ObjTrackingData m_ObjTrackingData;
569 
571  std::unique_ptr<OutputThread> m_OutputThread;
572 
574  std::unique_ptr<nvtxDomainRegistration, std::function<void(nvtxDomainRegistration*)>>
575  m_NvtxDomain;
578  bool m_Stopped = false;
581  NvDsInferStatus m_LastInferError = NVDSINFER_SUCCESS;
582 
584  RawOutputCallback m_RawoutputCb = nullptr;
585 
587  uint32_t m_IntervalCounter = 0;
589  uint64_t m_UntrackedObjectWarnPts = UINT64_C(-1);
592  bool m_1stInferDone = false;
593 };
594 
595 } // namespace gstnvinferserver
596 
597 #endif
gstnvinferserver::GstNvInferServerSourceInfo::object_history_map
GstNvInferServerObjectHistoryMap object_history_map
Map of object tracking IDs and inference history of the object.
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:109
gstnvinferserver
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver.h:45
nvdsinferserver
This is a header file for pre-processing cuda kernels with normalization and mean subtraction require...
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:69
_NvOSD_RectParams
Holds the box parameters of the box to be overlayed.
Definition: sources/includes/nvll_osd_struct.h:145
gstnvinferserver::GstNvInferServerObjectHistory::last_inferred_frame_num
uint64_t last_inferred_frame_num
Number of the frame in the stream when the object was last inferred on.
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:86
gstnvinferserver::GstNvInferServerProperties::inferOnGieId
int32_t inferOnGieId
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:125
gstnvinferserver::GstNvInferServerImpl::lastError
NvDsInferStatus lastError() const
GstNvInferServer
typedefG_BEGIN_DECLS struct _GstNvInferServer GstNvInferServer
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver.h:52
gstnvinferserver::GstNvInferServerImpl::start
NvDsInferStatus start()
Reads the configuration file and sets up processing context.
gstnvinferserver::GstNvInferServerObjectHistory::last_accessed_frame_num
uint64_t last_accessed_frame_num
Number of the frame in the stream when the object was last accessed.
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:90
gstnvinferserver::SharedObjHistory
std::shared_ptr< GstNvInferServerObjectHistory > SharedObjHistory
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:95
gstnvinferserver::GstNvInferServerProperties::interval
int32_t interval
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:122
NvBufSurface
Holds information about batched buffers.
Definition: sources/includes/nvbufsurface.h:597
gstnvinferserver::GstNvInferServerProperties::classifierType
std::string classifierType
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:128
gstnvinferserver::GstNvInferServerImpl::addTrackingSource
bool addTrackingSource(uint32_t sourceId)
Add a new source to the object history structure.
gstnvinferserver::GstNvInferServerImpl::updateInterval
void updateInterval(guint interval)
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:363
gstnvinferserver::GstNvInferServerImpl::classifierType
const std::string & classifierType() const
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:354
gstnvinferserver::GstNvInferServerImpl
Class of the nvinferserver element implementation.
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:147
NVDSINFER_SUCCESS
@ NVDSINFER_SUCCESS
NvDsInferContext operation succeeded.
Definition: sources/includes/nvdsinfer.h:233
gstnvinferserver::GstNvInferServerImpl::eraseTrackingSource
void eraseTrackingSource(uint32_t sourceId)
Removes a source from the object history structure.
gstnvinferserver::GstNvInferServerProperties::processMode
uint32_t processMode
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:124
gstnvinferserver::RawOutputCallback
std::function< void(void *, NvDsInferNetworkInfo &, NvDsInferLayerInfo *, uint32_t, uint32_t)> RawOutputCallback
Function for raw output callback .
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:140
gstnvinferserver::GstNvInferServerProperties::inputTensorFromMeta
bool inputTensorFromMeta
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:126
NvDsInferLogLevel
NvDsInferLogLevel
Enum for the log levels of NvDsInferContext.
Definition: sources/includes/nvdsinfer.h:262
gstnvinferserver::GstNvInferServerImpl::GstNvInferServerImpl
GstNvInferServerImpl(GstNvInferServer *infer)
Constructor, registers the handle of the parent GStreamer element.
gstnvinferserver::GstNvInferServerImpl::resetIntervalCounter
void resetIntervalCounter()
Resets the inference interval used in frame process mode to 0.
gst_nvinfer_server_logger
void gst_nvinfer_server_logger(uint32_t unique_id, NvDsInferLogLevel log_level, const char *log_message, void *user_ctx)
Function to output the logs using the GStreamer's debugging subsystem.
gstnvinferserver_meta_utils.h
nvinferserver metadata utilities header file.
gstnvinferserver::GstNvInferServerImpl::m_GstProperties
GstNvInferServerProperties m_GstProperties
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:554
NvDsInferNetworkInfo
Holds information about the model network.
Definition: sources/includes/nvdsinfer.h:119
gstnvinferserver::GstNvInferServerProperties::uniqueId
uint32_t uniqueId
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:123
NvDsInferLayerInfo
Holds information about one layer in the model.
Definition: sources/includes/nvdsinfer.h:96
gstnvinferserver::GstNvInferServerImpl::maxBatchSize
uint32_t maxBatchSize() const
_NvDsBatchMeta
Holds information about a formed batch containing frames from different sources.
Definition: sources/includes/nvdsmeta.h:262
gstnvinferserver::GstNvInferServerImpl::stop
NvDsInferStatus stop()
Deletes the inference context.
gstnvinferserver::GstNvInferServerImpl::nvtxDomain
nvtxDomainHandle_t nvtxDomain()
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:356
gstnvinferserver::GstNvInferServerImpl::processBatchMeta
NvDsInferStatus processBatchMeta(NvDsBatchMeta *batchMeta, NvBufSurface *inSurf, uint64_t seqId, void *gstBuf)
Submits the input batch for inference.
gstnvinferserver::GstNvInferServerImpl::canSupportGpu
bool canSupportGpu(int gpuId) const
gstnvinferserver::GstNvInferServerImpl::setRawoutputCb
void setRawoutputCb(RawOutputCallback cb)
Saves the callback function pointer for the raw tensor output.
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:269
gstnvinferserver::GstNvInferServerObjectHistory::under_inference
bool under_inference
Boolean indicating if the object is already being inferred on.
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:81
gstnvinferserver::GstNvInferServerSourceInfo
Holds information about the detected objects in the specific input source.
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:107
gstnvinferserver::GstNvInferServerImpl::~GstNvInferServerImpl
~GstNvInferServerImpl()
Constructor, do nothing.
gstnvinferserver::GstNvInferServerImpl::uniqueId
uint32_t uniqueId() const
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:353
gstnvinferserver::GstNvInferServerProperties::maxBatchSize
uint32_t maxBatchSize
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:121
gstnvinferserver::GstNvInferServerImpl::sync
NvDsInferStatus sync()
Waits for the output thread to finish processing queued operations.
gstnvinferserver::GstNvInferServerProperties::operateOnClassIds
std::vector< int32_t > operateOnClassIds
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:127
gstnvinferserver::GstNvInferServerImpl::queueOperation
NvDsInferStatus queueOperation(FuncItem func)
Queues the inference done operation for the request to the output thread.
gstnvinferserver::GstNvInferServerObjectHistory
Holds the inference information/history for one object based on its tracking ID.
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:79
gstnvinferserver::GstNvInferServerImpl::config
const ic::PluginControl & config() const
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:362
nvdsinferserver::SharedIBatchBuffer
std::shared_ptr< IBatchBuffer > SharedIBatchBuffer
Definition: sources/includes/nvdsinferserver/infer_datatypes.h:209
gstnvinferserver::GstNvInferServerObjectHistory::last_inferred_coords
NvOSD_RectParams last_inferred_coords
Bounding box co-ordinates of the object when it was last inferred on.
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:83
gstnvinferserver::GstNvInferServerObjectHistory::cached_info
InferClassificationOutput cached_info
Cached object information.
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:92
gstnvinferserver::GstNvInferServerProperties
Holds the configuration information from the nvinferserver element properties.
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:119
SharedInferContext
std::shared_ptr< dsis::IInferContext > SharedInferContext
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:67
gstnvinferserver::GstNvInferServerProperties::configPath
std::string configPath
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:120
nvdsinferserver::BatchSurfaceBuffer
Definition: sources/libs/nvdsinferserver/infer_surface_bufs.h:117
gstnvinferserver::WeakObjHistory
std::weak_ptr< GstNvInferServerObjectHistory > WeakObjHistory
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:96
gstnvinferserver::GstNvInferServerImpl::isAsyncMode
bool isAsyncMode() const
NvDsInferStatus
NvDsInferStatus
Enum for the status codes returned by NvDsInferContext.
Definition: sources/includes/nvdsinfer.h:231
gstnvinferserver::GstNvInferServerObjectHistoryMap
std::unordered_map< uint64_t, SharedObjHistory > GstNvInferServerObjectHistoryMap
Map for maintaining inference history for objects based on their tracking ids: Object ID : Object His...
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:102
_NvDsObjectMeta
Holds metadata for an object in the frame.
Definition: sources/includes/nvdsmeta.h:365