NVIDIA DeepStream SDK API Reference

6.4 Release
gstnvinferserver_impl.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-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 
22 #ifndef __GSTNVINFERSERVER_IMPL_H__
23 #define __GSTNVINFERSERVER_IMPL_H__
24 
25 #include <glib.h>
26 #include <gst/gst.h>
27 #include <string.h>
28 #include <sys/time.h>
29 
30 #include <condition_variable>
31 #include <list>
32 #include <memory>
33 #include <mutex>
34 #include <thread>
35 #include <vector>
36 
37 #include <nvbufsurface.h>
38 #include <nvbufsurftransform.h>
39 
40 #include "infer_icontext.h"
41 #include "infer_utils.h"
42 
43 #include "nvdsmeta.h"
44 #include "nvtx3/nvToolsExt.h"
45 
47 #include "infer_datatypes.h"
48 #include "infer_post_datatypes.h"
49 #include "nvdsinferserver_config.pb.h"
50 #include "nvdsinferserver_plugin.pb.h"
51 
56 void gst_nvinfer_server_logger(uint32_t unique_id, NvDsInferLogLevel log_level,
57  const char* log_message, void* user_ctx);
58 
59 namespace dsis = nvdsinferserver;
60 namespace ic = nvdsinferserver::config;
61 
62 using SharedInferContext = std::shared_ptr<dsis::IInferContext>;
63 
64 namespace nvdsinferserver {
65 class BatchSurfaceBuffer;
66 }
67 
68 namespace gstnvinferserver {
69 
76  bool under_inference = FALSE;
87  InferClassificationOutput cached_info;
88 };
89 
90 using SharedObjHistory = std::shared_ptr<GstNvInferServerObjectHistory>;
91 using WeakObjHistory = std::weak_ptr<GstNvInferServerObjectHistory>;
92 
96 typedef std::unordered_map<uint64_t, SharedObjHistory>
98 
102 typedef struct {
106  gulong last_cleanup_frame_num = 0;
108  gulong last_seen_frame_num = 0;
110 
115  std::string configPath;
116  uint32_t maxBatchSize = 0;
117  int32_t interval = -1;
118  uint32_t uniqueId = 0;
119  uint32_t processMode = 0;
120  int32_t inferOnGieId = -1;
121  bool inputTensorFromMeta = false;
122  std::vector<int32_t> operateOnClassIds;
123  std::string classifierType;
124 };
125 
134 using RawOutputCallback = std::function<void(void *, NvDsInferNetworkInfo&,
135  NvDsInferLayerInfo*, uint32_t, uint32_t)>;
136 
137 struct RequestBuffer;
138 
143 private:
149  class ObjTrackingData {
150  public:
154  ObjTrackingData() {}
155 
161  bool initSource(uint32_t id) {
162  m_SourceInfo.emplace(id, GstNvInferServerSourceInfo{});
163  return true;
164  }
165 
171  void removeSource(uint32_t id) { m_SourceInfo.erase(id); }
172 
180  GstNvInferServerSourceInfo* findSource(int id);
181 
189  bool hasSource(uint32_t id) const {
190  return m_SourceInfo.find(id) != m_SourceInfo.end();
191  }
192 
201  SharedObjHistory findObjHistory(
202  uint32_t sourceId, uint64_t objId);
203 
212  SharedObjHistory newHistory(
213  uint32_t sourceId, uint64_t objId);
214 
219  void clear() { m_SourceInfo.clear(); }
220 
231  void clearUpHistory(uint64_t seqId);
232 
233  private:
236  std::unordered_map<uint32_t, GstNvInferServerSourceInfo> m_SourceInfo;
237 
240  gulong m_LastMapCleanupSeqId = 0;
241  };
242 
243  using FuncItem = std::function<void()>;
244 
245 public:
253 
258 
265  m_RawoutputCb = cb;
266  }
267 
282 
291 
305  NvBufSurface* inSurf, uint64_t seqId, void *gstBuf);
306 
312 
317  NvDsInferStatus queueOperation(FuncItem func);
318 
319 
329  bool addTrackingSource(uint32_t sourceId);
330 
340  void eraseTrackingSource(uint32_t sourceId);
341 
345  void resetIntervalCounter();
346 
347  // helper functions
348  uint32_t uniqueId() const { return m_GstProperties.uniqueId; }
349  const std::string& classifierType() const { return m_GstProperties.classifierType; }
350  uint32_t maxBatchSize() const;
351  nvtxDomainHandle_t nvtxDomain() {
352  return m_NvtxDomain.get();
353  }
354  bool isAsyncMode() const;
355  bool canSupportGpu(int gpuId) const;
356  NvDsInferStatus lastError() const;
357  const ic::PluginControl& config() const { return m_PluginConfig; }
358  void updateInterval(guint interval) {m_PluginConfig.mutable_input_control()->set_interval(interval);}
359 private:
373  NvDsInferStatus processFullFrame(NvDsBatchMeta* batchMeta,
374  NvBufSurface* inSurf, uint64_t seqId, void *gstBuf);
375 
393  NvDsInferStatus processObjects(NvDsBatchMeta* batchMeta,
394  NvBufSurface* inSurf, uint64_t seqId, void *gstBuf);
395 
408  NvDsInferStatus processInputTensor(
409  NvDsBatchMeta* batchMeta, NvBufSurface* inSurf, uint64_t seqId, void* gstBuf);
410 
423  NvDsInferStatus batchInference(
424  std::shared_ptr<RequestBuffer> reqBuf, std::vector<dsis::SharedBatchBuf> batchBuf);
425 
437  void InferenceDone(std::shared_ptr<RequestBuffer> req);
438 
444  NvDsInferStatus attachBatchDetection(
445  RequestBuffer* req, dsis::SharedIBatchBuffer output);
446 
452  NvDsInferStatus attachBatchClassification(
453  RequestBuffer* req, dsis::SharedIBatchBuffer output);
454 
460  NvDsInferStatus attachBatchSegmentation(
461  RequestBuffer* req, dsis::SharedIBatchBuffer output);
462 
473  NvDsInferStatus handleOutputTensors(RequestBuffer* req);
474 
475  void updateLastError(NvDsInferStatus s);
476 
481  ic::PluginControl& mutableConfig() { return m_PluginConfig; }
482  bool isClassify() const;
483  bool isDetection() const;
484  bool isSegmentation() const;
485  bool isOtherNetowrk() const;
486  bool isFullFrame() const;
487  bool maintainAspectRatio() const;
488  bool needOutputTensorMeta() const;
489  bool hasCustomProcess() const;
490  uint32_t inferInterval() const { return m_PluginConfig.input_control().interval(); }
491  bool inputTensorFromMeta() const
492  {
493  return m_PluginConfig.infer_config().has_input_tensor_from_meta();
494  }
505  bool validatePluginConfig(
506  ic::PluginControl& config, const std::string& path,
507  const GstNvInferServerProperties& update);
508 
533  bool shouldInferObject(NvDsObjectMeta* obj_meta, uint32_t frameNum,
534  GstNvInferServerObjectHistory* history);
535 
545  bool outputLoop(FuncItem func);
546 
547 public:
549 
550 private:
551  using OutputThread = dsis::QueueThread<std::list<FuncItem>>;
552 
554  SharedInferContext m_InferCtx;
556  GstNvInferServer* m_GstPlugin = nullptr;
558  mutable std::mutex m_ProcessMutex;
560  ic::PluginControl m_PluginConfig;
562  ObjTrackingData m_ObjTrackingData;
563 
565  std::unique_ptr<OutputThread> m_OutputThread;
566 
568  std::unique_ptr<nvtxDomainRegistration, std::function<void(nvtxDomainRegistration*)>>
569  m_NvtxDomain;
572  bool m_Stopped = false;
575  NvDsInferStatus m_LastInferError = NVDSINFER_SUCCESS;
576 
578  RawOutputCallback m_RawoutputCb = nullptr;
579 
581  uint32_t m_IntervalCounter = 0;
583  uint64_t m_UntrackedObjectWarnPts = UINT64_C(-1);
586  bool m_1stInferDone = false;
587 };
588 
589 } // namespace gstnvinferserver
590 
591 #endif
gstnvinferserver::GstNvInferServerSourceInfo::object_history_map
GstNvInferServerObjectHistoryMap object_history_map
Map of object tracking IDs and inference history of the object.
Definition: gstnvinferserver_impl.h:104
gstnvinferserver
Definition: gstnvinferserver_impl.h:68
nvdsinferserver
Copyright (c) 2021, NVIDIA CORPORATION.
Definition: infer_custom_process.h:28
_NvOSD_RectParams
Holds the box parameters of the box to be overlayed.
Definition: nvll_osd_struct.h:139
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: gstnvinferserver_impl.h:81
gstnvinferserver::GstNvInferServerProperties::inferOnGieId
int32_t inferOnGieId
Definition: gstnvinferserver_impl.h:120
gstnvinferserver::GstNvInferServerProperties::operateOnClassIds
std::vector< int32_t > operateOnClassIds
Definition: gstnvinferserver_impl.h:122
nvdsinferserver::SharedIBatchBuffer
std::shared_ptr< IBatchBuffer > SharedIBatchBuffer
Definition: infer_datatypes.h:204
gstnvinferserver::GstNvInferServerImpl::lastError
NvDsInferStatus lastError() const
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: gstnvinferserver_impl.h:85
infer_datatypes.h
Header file for the data types used in the inference processing.
gstnvinferserver::GstNvInferServerProperties::interval
int32_t interval
Definition: gstnvinferserver_impl.h:117
NvBufSurface
Holds information about batched buffers.
Definition: nvbufsurface.h:503
gstnvinferserver::GstNvInferServerProperties::classifierType
std::string classifierType
Definition: gstnvinferserver_impl.h:123
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: gstnvinferserver_impl.h:358
gstnvinferserver::GstNvInferServerImpl::classifierType
const std::string & classifierType() const
Definition: gstnvinferserver_impl.h:349
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::GstNvInferServerImpl
Class of the nvinferserver element implementation.
Definition: gstnvinferserver_impl.h:142
NVDSINFER_SUCCESS
@ NVDSINFER_SUCCESS
NvDsInferContext operation succeeded.
Definition: nvdsinfer.h:220
gstnvinferserver::GstNvInferServerImpl::eraseTrackingSource
void eraseTrackingSource(uint32_t sourceId)
Removes a source from the object history structure.
gstnvinferserver::GstNvInferServerProperties::processMode
uint32_t processMode
Definition: gstnvinferserver_impl.h:119
gstnvinferserver::GstNvInferServerProperties::inputTensorFromMeta
bool inputTensorFromMeta
Definition: gstnvinferserver_impl.h:121
GstNvInferServer
typedefG_BEGIN_DECLS struct _GstNvInferServer GstNvInferServer
Definition: gstnvinferserver.h:47
NvDsInferLogLevel
NvDsInferLogLevel
Enum for the log levels of NvDsInferContext.
Definition: nvdsinfer.h:249
infer_utils.h
Header file containing utility functions and classes used by the nvinferserver low level library.
gstnvinferserver::GstNvInferServerImpl::GstNvInferServerImpl
GstNvInferServerImpl(GstNvInferServer *infer)
Constructor, registers the handle of the parent GStreamer element.
gstnvinferserver_meta_utils.h
nvinferserver metadata utilities header file.
gstnvinferserver::GstNvInferServerImpl::resetIntervalCounter
void resetIntervalCounter()
Resets the inference interval used in frame process mode to 0.
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: gstnvinferserver_impl.h:97
gstnvinferserver::GstNvInferServerImpl::m_GstProperties
GstNvInferServerProperties m_GstProperties
Definition: gstnvinferserver_impl.h:548
NvDsInferNetworkInfo
Holds information about the model network.
Definition: nvdsinfer.h:110
gstnvinferserver::GstNvInferServerProperties::uniqueId
uint32_t uniqueId
Definition: gstnvinferserver_impl.h:118
NvDsInferLayerInfo
Holds information about one layer in the model.
Definition: nvdsinfer.h:87
gstnvinferserver::GstNvInferServerImpl::maxBatchSize
uint32_t maxBatchSize() const
_NvDsBatchMeta
Holds information about a formed batch containing frames from different sources.
Definition: nvdsmeta.h:240
gstnvinferserver::GstNvInferServerImpl::stop
NvDsInferStatus stop()
Deletes the inference context.
infer_icontext.h
Inference context library interface header file.
SharedInferContext
std::shared_ptr< dsis::IInferContext > SharedInferContext
Definition: gstnvinferserver_impl.h:62
gstnvinferserver::GstNvInferServerImpl::nvtxDomain
nvtxDomainHandle_t nvtxDomain()
Definition: gstnvinferserver_impl.h:351
gstnvinferserver::GstNvInferServerImpl::processBatchMeta
NvDsInferStatus processBatchMeta(NvDsBatchMeta *batchMeta, NvBufSurface *inSurf, uint64_t seqId, void *gstBuf)
Submits the input batch for inference.
gstnvinferserver::WeakObjHistory
std::weak_ptr< GstNvInferServerObjectHistory > WeakObjHistory
Definition: gstnvinferserver_impl.h:91
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: gstnvinferserver_impl.h:264
gstnvinferserver::GstNvInferServerObjectHistory::under_inference
bool under_inference
Boolean indicating if the object is already being inferred on.
Definition: gstnvinferserver_impl.h:76
gstnvinferserver::GstNvInferServerSourceInfo
Holds information about the detected objects in the specific input source.
Definition: gstnvinferserver_impl.h:102
gstnvinferserver::GstNvInferServerImpl::~GstNvInferServerImpl
~GstNvInferServerImpl()
Constructor, do nothing.
infer_post_datatypes.h
gstnvinferserver::GstNvInferServerImpl::uniqueId
uint32_t uniqueId() const
Definition: gstnvinferserver_impl.h:348
gstnvinferserver::GstNvInferServerProperties::maxBatchSize
uint32_t maxBatchSize
Definition: gstnvinferserver_impl.h:116
gstnvinferserver::GstNvInferServerImpl::sync
NvDsInferStatus sync()
Waits for the output thread to finish processing queued operations.
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: gstnvinferserver_impl.h:74
gstnvinferserver::GstNvInferServerImpl::config
const ic::PluginControl & config() const
Definition: gstnvinferserver_impl.h:357
gstnvinferserver::GstNvInferServerObjectHistory::last_inferred_coords
NvOSD_RectParams last_inferred_coords
Bounding box co-ordinates of the object when it was last inferred on.
Definition: gstnvinferserver_impl.h:78
gstnvinferserver::SharedObjHistory
std::shared_ptr< GstNvInferServerObjectHistory > SharedObjHistory
Definition: gstnvinferserver_impl.h:90
gstnvinferserver::RawOutputCallback
std::function< void(void *, NvDsInferNetworkInfo &, NvDsInferLayerInfo *, uint32_t, uint32_t)> RawOutputCallback
Function for raw output callback .
Definition: gstnvinferserver_impl.h:135
gstnvinferserver::GstNvInferServerObjectHistory::cached_info
InferClassificationOutput cached_info
Cached object information.
Definition: gstnvinferserver_impl.h:87
gstnvinferserver::GstNvInferServerProperties
Holds the configuration information from the nvinferserver element properties.
Definition: gstnvinferserver_impl.h:114
nvdsmeta.h
nvbufsurftransform.h
gstnvinferserver::GstNvInferServerProperties::configPath
std::string configPath
Definition: gstnvinferserver_impl.h:115
nvbufsurface.h
gstnvinferserver::GstNvInferServerImpl::isAsyncMode
bool isAsyncMode() const
NvDsInferStatus
NvDsInferStatus
Enum for the status codes returned by NvDsInferContext.
Definition: nvdsinfer.h:218
_NvDsObjectMeta
Holds metadata for an object in the frame.
Definition: nvdsmeta.h:342