NVIDIA DeepStream SDK API Reference

7.1 Release
deepstream_action.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2022 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 __DEEPSTREAM_ACTION_H__
14 #define __DEEPSTREAM_ACTION_H__
15 
16 #include <cuda_runtime_api.h>
17 
18 #include <fstream>
19 #include <functional>
20 #include <gst/gst.h>
21 #include <glib.h>
22 #include <inttypes.h>
23 #include <iostream>
24 #include <math.h>
25 #include <memory>
26 #include <queue>
27 #include <sys/time.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <string>
31 #include <string.h>
32 #include <unordered_map>
33 #include <map>
34 #include <vector>
35 
36 #include "gstnvdsmeta.h"
37 #include "nvdspreprocess_meta.h"
38 #include "gstnvdsinfer.h"
39 
40 #ifndef PLATFORM_TEGRA
41 #include "gst-nvmessage.h"
42 #endif
43 
44 /* Print log message*/
45 #define LOG(out, level, fmt, ...) \
46  fprintf(out, "[%s: DS_3DAR] " fmt "\n", #level, ##__VA_ARGS__)
47 
48 /* Print Debug message if ENABLE_DEBUG not zero */
49 #define LOG_DEBUG(fmt, ...) \
50  if (gActionConfig.debug >= kDebugEnable) { \
51  LOG(stdout, DEBUG, fmt, ##__VA_ARGS__); \
52  }
53 
54 /* Print Error message*/
55 #define LOG_ERROR(fmt, ...) \
56  LOG(stderr, ERROR, fmt, ##__VA_ARGS__)
57 
58 template<typename T>
59 class SafePtr: public std::unique_ptr<T, std::function<void(T*)>> {
60 public:
61  SafePtr(T* p, std::function<void(T*)> freefn):
62  std::unique_ptr<T, std::function<void(T*)>>(p, freefn)
63  {}
64 };
65 
66 enum DebugLevel {
70 };
71 
72 // FPS calculation for each source stream
74  struct FpsStats{
75  double startTime = 0;
76  uint64_t sumFrames = 0;
77  float curFps = 0;
78  float avgFps = 0;
79  };
80 public:
81  FpsCalculation(uint32_t interval)
82  : _max_frame_nums(interval)
83  {}
84  float updateFps(uint32_t source_id) {
85  struct timeval time_now;
86  gettimeofday(&time_now, nullptr);
87  double now = (double)time_now.tv_sec + time_now.tv_usec / (double)1000000; // second
88  float fps = -1.0f;
89  auto iSrc = _timestamps.find(source_id);
90  if (iSrc != _timestamps.end()) {
91  auto & tms = iSrc->second;
92  fps = tms.size() / (now - tms.front());
93  while (tms.size() >= _max_frame_nums) {
94  tms.pop();
95  }
96  auto & stats = _fpsStats[source_id];
97  stats.curFps = fps;
98  stats.avgFps = stats.sumFrames / (now - stats.startTime);
99  stats.sumFrames++;
100  } else {
101  iSrc = _timestamps.emplace(source_id, std::queue<double>()).first;
102  _fpsStats.emplace(source_id, FpsStats{now, 1, 0, 0});
103  }
104  iSrc->second.push(now);
105 
106  return fps;
107  }
108 
109  // get dataset of current fps and average fps
110  void getAllFps(std::vector<std::pair<float, float>>& fps) {
111  for (auto& s: _fpsStats) {
112  fps.emplace_back(std::make_pair(s.second.curFps, s.second.avgFps));
113  }
114  }
115 private:
116  std::unordered_map<uint32_t, std::queue<double>> _timestamps;
117  uint32_t _max_frame_nums = 50;
118  std::map<uint32_t, FpsStats> _fpsStats;
119 };
120 
122 {
123  // stream source list
124  std::vector<std::string> uri_list;
125 
126  // display sink settings
127  gboolean display_sync = true;
128 
129  // nvdspreprocess plugin config file path
130  std::string preprocess_config;
131  // nvinfer plugin config file path
132  std::string infer_config;
133  // nvinferserver(ds-triton) plugin config file path
134  std::string triton_infer_config;
135 
136  // nvstreammux settings
137  uint32_t muxer_height = 720;
138  uint32_t muxer_width = 1280;
139  // batched-push-timeout in usec, default value 40ms
140  int32_t muxer_batch_timeout = 40000;
141 
142  // tiler settings
143  uint32_t tiler_height = 720;
144  uint32_t tiler_width = 1280;
145 
146  // debug level, disabled by default
148 
149  // enable fps print on screen. enabled by default
150  gboolean enableFps = TRUE;
151 
152  // enable to use fakesink
153  gboolean useFakeSink = FALSE;
154 };
155 
156 // parse action recognition config into NvDsARConfig
157 bool parse_action_config(const char* action_config_path, NvDsARConfig& config);
158 
159 #endif
kDebugDisable
@ kDebugDisable
Definition: deepstream_action.h:67
FpsCalculation::updateFps
float updateFps(uint32_t source_id)
Definition: deepstream_action.h:84
kDebugEnable
@ kDebugEnable
Definition: deepstream_action.h:68
NvDsARConfig::infer_config
std::string infer_config
Definition: deepstream_action.h:132
FpsCalculation
Definition: deepstream_action.h:73
NvDsARConfig::uri_list
std::vector< std::string > uri_list
Definition: deepstream_action.h:124
gstnvdsinfer.h
FpsCalculation::getAllFps
void getAllFps(std::vector< std::pair< float, float >> &fps)
Definition: deepstream_action.h:110
NvDsARConfig::muxer_width
uint32_t muxer_width
Definition: deepstream_action.h:138
DebugLevel
DebugLevel
Definition: deepstream_action.h:66
NvDsARConfig::triton_infer_config
std::string triton_infer_config
Definition: deepstream_action.h:134
NvDsARConfig::tiler_height
uint32_t tiler_height
Definition: deepstream_action.h:143
NvDsARConfig::tiler_width
uint32_t tiler_width
Definition: deepstream_action.h:144
SafePtr::SafePtr
SafePtr(T *p, std::function< void(T *)> freefn)
Definition: deepstream_action.h:61
NvDsARConfig::muxer_height
uint32_t muxer_height
Definition: deepstream_action.h:137
NvDsARConfig::preprocess_config
std::string preprocess_config
Definition: deepstream_action.h:130
gstnvdsmeta.h
kDebugVerbose
@ kDebugVerbose
Definition: deepstream_action.h:69
NvDsARConfig::enableFps
gboolean enableFps
Definition: deepstream_action.h:150
NvDsARConfig::display_sync
gboolean display_sync
Definition: deepstream_action.h:127
SafePtr
Definition: deepstream_action.h:59
NvDsARConfig
Definition: deepstream_action.h:121
FpsCalculation::FpsCalculation
FpsCalculation(uint32_t interval)
Definition: deepstream_action.h:81
parse_action_config
bool parse_action_config(const char *action_config_path, NvDsARConfig &config)
NvDsARConfig::debug
DebugLevel debug
Definition: deepstream_action.h:147
gst-nvmessage.h
NvDsARConfig::muxer_batch_timeout
int32_t muxer_batch_timeout
Definition: deepstream_action.h:140
NvDsARConfig::useFakeSink
gboolean useFakeSink
Definition: deepstream_action.h:153