NVIDIA DeepStream SDK API Reference

9.2 Release
9.2/service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-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 #include <iostream>
19 #include <string>
20 #include <memory>
21 #include <vector>
22 #include <fstream>
23 #include <algorithm> // for std::transform
24 #include <cctype> // for std::tolower
25 
26 #include "buffer_probe.hpp"
27 #include "source_config.hpp"
28 
29 namespace deepstream {
30 
31  static std::vector<std::string> parse_labels(const std::string& filename) {
32  std::ifstream file(filename); // Open the file
33  std::vector<std::string> lines; // Vector to store lines
34 
35  if (file.is_open()) { // Check if the file is open
36  std::string line;
37  while (std::getline(file, line)) { // Read each line
38  std::string result=line;
39  std::transform(line.begin(), line.end(), result.begin(), [](unsigned char c){ return std::tolower(c); });
40  lines.push_back(result); // Store the line in the vector
41  }
42  file.close(); // Close the file
43  } else {
44  std::cerr << "Unable to open file: " << filename << std::endl;
45  }
46 
47  return lines;
48  }
49 
50 class MsgMetaGenerator : public BufferProbe::IBatchMetadataOperator {
51 public:
53  virtual ~MsgMetaGenerator() {}
54 
56  // Initialize only once
57  if (!initialized_) {
58  probe.getProperty("frame-interval", frame_interval);
59  // frames % frame_interval below is undefined for 0 (SIGFPE) and matches
60  // every frame for -1, so a bad config would either kill the pipeline or
61  // silently emit on every frame. Fall back to the default instead.
62  if (frame_interval <= 0) {
63  std::cerr << "add_message_meta_probe: frame-interval must be > 0, got "
64  << frame_interval << "; using 1" << std::endl;
65  frame_interval = 1;
66  }
67  std::string source_config;
68  probe.getProperty("source-config", source_config);
69  std::string label_file;
70  probe.getProperty("label-file", label_file);
71 
72  if (!source_config.empty()) {
73  SourceConfig source_config_obj(source_config);
74  for (size_t i = 0; i < source_config_obj.nSources(); i++) {
75  // multiple sources added
76  std::string src_name = "src_";
77  src_name += std::to_string(i);
78  std::string uri = source_config_obj.getSensorInfo(i).uri;
79  std::string sensor_id = source_config_obj.getSensorInfo(i).sensor_id;
80  sensor_map_[i] = SensorInfo{uri, sensor_id, src_name};
81  }
82  }
83 
84  if (!label_file.empty()) {
85  labels_ = parse_labels(label_file);
86  }
87 
88  initialized_ = true;
89  }
90 
91  FrameMetadata::Iterator frame_itr;
92  for (data.initiateIterator(frame_itr); !frame_itr->done(); frame_itr->next())
93  {
94  auto source_id = (*frame_itr)->sourceId();
95  int frames = frames_[source_id];
97  for ((*frame_itr)->initiateIterator(obj_itr); !obj_itr->done(); obj_itr->next()) {
98  if (frames % frame_interval == 0)
99  {
100  EventMessageUserMetadata event_user_meta;
101  if (data.acquire(event_user_meta)) {
102  if(sensor_map_.empty()) {
103  event_user_meta.generate(**obj_itr, **frame_itr, "N/A", "N/A", labels_);
104  (*frame_itr)->append(event_user_meta);
105  } else {
106  auto itr = sensor_map_.find(source_id);
107  if (itr != sensor_map_.end()) {
108  const std::string sensor = itr->second.sensor_id;
109  const std::string uri = itr->second.uri;
110  event_user_meta.generate(**obj_itr, **frame_itr, sensor, uri, labels_);
111  (*frame_itr)->append(event_user_meta);
112  }
113  }
114  }
115  }
116  }
117  frames_[source_id] = frames + 1;
118  }
119 
120  return probeReturn::Probe_Ok;
121  }
122 
123  protected:
124  // Per source: a single shared counter would advance once per frame in the
125  // batch, so frame_interval would apply to the batch rather than to each
126  // source. With N sources and an interval that divides N, a source would then
127  // emit either on every frame or never.
128  std::map<uint32_t, int> frames_; // source id -> frames seen from that source
129  int frame_interval = 1;
130  std::map<uint32_t, SensorInfo> sensor_map_;
131  std::vector<std::string> labels_;
132  bool initialized_;
133 };
134 
135 }
deepstream::probeReturn::Probe_Ok
@ Probe_Ok
Nothing abnormal, the buffer will be treated as usual.
deepstream::ObjectMetadata::Iterator
std::unique_ptr< AbstractIterator< ObjectMetadata > > Iterator
Definition: service-maker/includes/metadata.hpp:208
deepstream::SensorInfo::uri
std::string uri
Definition: service-maker/includes/source_config.hpp:36
deepstream::SourceConfig::getSensorInfo
SensorInfo getSensorInfo(uint32_t index) const
Get sensor information for a specific source.
deepstream::BufferProbe
Represent a custom object for the purpose of probing output buffers.
Definition: service-maker/includes/buffer_probe.hpp:63
deepstream::MsgMetaGenerator::~MsgMetaGenerator
virtual ~MsgMetaGenerator()
Definition: 9.2/service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:53
deepstream::MsgMetaGenerator::frame_interval
int frame_interval
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:129
deepstream::parse_labels
static std::vector< std::string > parse_labels(const std::string &filename)
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:31
deepstream::MsgMetaGenerator::labels_
std::vector< std::string > labels_
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:131
deepstream::EventMessageUserMetadata
User metadata for event message.
Definition: service-maker/includes/metadata.hpp:277
deepstream::probeReturn
probeReturn
Return values from user implemented probe interfaces.
Definition: service-maker/includes/buffer_probe.hpp:47
deepstream::MsgMetaGenerator::MsgMetaGenerator
MsgMetaGenerator()
Definition: 9.2/service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:52
deepstream::MsgMetaGenerator::handleData
virtual probeReturn handleData(BufferProbe &probe, BatchMetadata &data)
Definition: 9.2/service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:55
deepstream
Definition: service-maker/includes/buffer.hpp:38
deepstream::BatchMetadata
Holds information about a formed batch containingframes from different sources.
Definition: service-maker/includes/metadata.hpp:841
deepstream::MsgMetaGenerator::frames_
std::map< uint32_t, int > frames_
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:128
deepstream::MsgMetaGenerator::initialized_
bool initialized_
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:132
deepstream::SourceConfig::nSources
uint32_t nSources() const
Get the number of the sources.
deepstream::FrameMetadata::Iterator
std::unique_ptr< AbstractIterator< FrameMetadata > > Iterator
Definition: service-maker/includes/metadata.hpp:480
deepstream::SensorInfo::sensor_id
std::string sensor_id
Definition: service-maker/includes/source_config.hpp:37
deepstream::EventMessageUserMetadata::generate
void generate(const ObjectMetadata &, const FrameMetadata &, const std::string sensor="N/A", const std::string uri="N/A", const std::vector< std::string >labels=std::vector< std::string >())
generate an event by attaching an object
deepstream::SensorInfo
Definition: service-maker/includes/source_config.hpp:34
data
JSAMPIMAGE data
Definition: sources/libs/nvll_batch_jpeg_encode/oss/jpeglib.h:967
deepstream::SourceConfig
Definition: service-maker/includes/source_config.hpp:61
deepstream::Object::getProperty
Object & getProperty(const std::string &name, T &value, Args &... args)
Template for getting multiple properties.
Definition: service-maker/includes/object.hpp:177
deepstream::MsgMetaGenerator::sensor_map_
std::map< uint32_t, SensorInfo > sensor_map_
Definition: service-maker/sources/modules/add_message_meta_probe/add_message_meta_probe.hpp:130