NVIDIA DeepStream SDK API Reference

9.2 Release
service-maker/sources/python/src/bindings/probe.h
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 "probedoc.h"
19 
20 class BufferObserver_ : public BufferProbe::IBufferObserver {
21 public:
22  probeReturn handleBuffer(BufferProbe& probe, const Buffer& buffer) override {
23  this->handle_buffer(buffer);
24  return probeReturn::Probe_Ok;
25  }
26 
27  virtual void handle_buffer(const Buffer& buffer) = 0;
28 };
29 
31  virtual void handle_buffer(const Buffer& buffer) {
32  PYBIND11_OVERRIDE_PURE_NAME(
33  void,
35  "handle_buffer",
36  handle_buffer,
37  buffer
38  );
39  }
40 };
41 
42 class BufferOperator_ : public BufferProbe::IBufferOperator {
43 public:
44  probeReturn handleBuffer(BufferProbe& probe, Buffer& buffer) override {
45  auto ret = this->handle_buffer(buffer);
46  return ret ? probeReturn::Probe_Ok:probeReturn::Probe_Drop;
47  }
48 
49  virtual bool handle_buffer(Buffer buffer) = 0;
50 };
51 
53  virtual bool handle_buffer(Buffer buffer) {
54  PYBIND11_OVERRIDE_PURE_NAME(
55  bool,
57  "handle_buffer",
58  handle_buffer,
59  buffer
60  );
61  }
62 };
63 
64 class BatchMetadataOperator_ : public BufferProbe::IBatchMetadataOperator {
65 public:
66  probeReturn handleData(BufferProbe& probe, BatchMetadata& metadata) override {
67  probe_ = &probe;
68  this->handle_metadata(metadata);
69  probe_ = nullptr;
70  return probeReturn::Probe_Ok;
71  }
72 
73  virtual void handle_metadata(BatchMetadata& metadata) = 0;
74 
75  Object::Value get_property(const std::string& name) {
76  Object::Value v;
77  if (probe_) probe_->getProperty(name, v);
78  return v;
79  }
80 
81 protected:
82  BufferProbe* probe_ = nullptr;
83 };
84 
86  virtual void handle_metadata(BatchMetadata& metadata) {
87  PYBIND11_OVERRIDE_PURE_NAME(
88  void,
90  "handle_metadata",
91  handle_metadata,
92  metadata
93  );
94  }
95 
96 };
97 
98 class Probe : public BufferProbe {
99 public:
100  Probe(const string& name, BufferObserver_* handler)
101  : BufferProbe(name, handler)
102  {}
103 
104  Probe(const string& name, const char* factory, BufferObserver_* handler)
105  : BufferProbe(name, factory, handler)
106  {}
107 
108  Probe(const string& name, BufferOperator_* handler)
109  : BufferProbe(name, handler)
110  {}
111 
112  Probe(const string& name, const char* factory, BufferOperator_* handler)
113  : BufferProbe(name, factory, handler)
114  {}
115 
116  Probe(const string& name, BatchMetadataOperator_* handler)
117  : BufferProbe(name, handler)
118  {}
119 
120  Probe(const string& name, const char* factory, BatchMetadataOperator_* handler)
121  : BufferProbe(name, factory, handler)
122  {}
123 
124  virtual ~Probe() {
126  metadata_handler_.release();
127  }
128 
129  static unique_ptr<Probe> create(const string& plugin, const string& name) {
130  auto obj = CommonFactory::getInstance().createObject(plugin, name).release();
131  Probe* probe = dynamic_cast<Probe*>(obj);
132  return unique_ptr<Probe>(probe);
133  }
134 };
135 
143 class __attribute__((visibility("hidden"))) PythonProbeFactory : public CustomFactory {
144  py::object py_class_;
145  string param_spec_;
146  string factory_name_;
147 public:
148  PythonProbeFactory(const string& name, py::object py_class, string param_spec)
149  : CustomFactory(name, 0) // 0 → Object() skips g_object_new, no gst_custom_factory_init
150  , py_class_(py_class), param_spec_(param_spec), factory_name_(name) {}
151 
152  CustomObject* createObject(const string& instance_name) override {
153  py::gil_scoped_acquire gil;
154  py::object py_instance = py_class_();
155  Probe* probe = nullptr;
156  const char* factory_name = factory_name_.c_str();
157  if (py::isinstance<BufferObserver_>(py_instance)) {
158  auto* obs = py_instance.cast<BufferObserver_*>();
159  py_instance.release();
160  probe = new Probe(instance_name, factory_name, obs);
161  } else {
162  auto* handler = py_instance.cast<BatchMetadataOperator_*>();
163  py_instance.release();
164  probe = new Probe(instance_name, factory_name, handler);
165  }
166  return probe;
167  }
168 
169 protected:
170  // Serve our stored param spec so CustomObject::CustomObject populates
171  // properties_ with the right defaults, without touching the null object_.
172  Value get_(const std::string& property_name) override {
173  if (property_name == "param-spec") {
174  return Value(param_spec_);
175  }
176  return Value();
177  }
178 };
179 
180 void module_probe_bind(py::module &m);
181 
182 void module_probe_bind(py::module &m) {
183  py::class_<Probe>(m, "Probe", pydeepstreamdoc::probe::ProbeDoc::descr)
184  .def(py::init<const string&, BufferOperator_*>(), py::keep_alive<1, 3>(), pydeepstreamdoc::probe::ProbeDoc::init_buffer_operator)
185  .def(py::init<const string&, BatchMetadataOperator_*>(), py::keep_alive<1, 3>(), pydeepstreamdoc::probe::ProbeDoc::init_batch_metadata_operator)
187  .def("set", [](Probe& self, py::dict args) {
188  for (auto item : args) {
189  string key = item.first.cast<string>();
190  Object::Value value = item.second.cast<Object::Value>();
191  self.set(key, value);
192  }
193  });
194  py::class_<BufferObserver_, PyBufferObserver>(m, "BufferObserver", pydeepstreamdoc::probe::BufferObserverDoc::descr)
195  .def(py::init<>())
197  py::class_<BufferOperator_, PyBufferOperator>(m, "BufferOperator", pydeepstreamdoc::probe::BufferOperatorDoc::descr)
198  .def(py::init<>())
200  py::class_<BatchMetadataOperator_, PyBatchMetadataOperator>(m, "BatchMetadataOperator", pydeepstreamdoc::probe::BatchMetadataOperatorDoc::descr)
201  .def(py::init<>())
203  .def("get_property", &BatchMetadataOperator_::get_property, py::arg("name"));
204 
205  m.def("_register_probe",
206  [](const string& name, py::object cls, const string& param_spec) {
207  CommonFactory::getInstance().addCustomFactory(
208  new PythonProbeFactory(name, cls, param_spec), name.c_str());
209  },
210  py::arg("name"), py::arg("cls"), py::arg("param_spec") = string(""));
211 }
pydeepstreamdoc::probe::ProbeDoc::init_buffer_operator
constexpr const char * init_buffer_operator
Definition: service-maker/sources/python/src/bindings/docstrings/probedoc.h:27
PyBufferOperator
Definition: service-maker/sources/python/src/bindings/probe.h:52
pydeepstreamdoc::probe::BatchMetadataOperatorDoc::descr
constexpr const char * descr
Definition: service-maker/sources/python/src/bindings/docstrings/probedoc.h:46
Probe::Probe
Probe(const string &name, BufferOperator_ *handler)
Definition: service-maker/sources/python/src/bindings/probe.h:108
pydeepstreamdoc::probe::ProbeDoc::descr
constexpr const char * descr
Definition: service-maker/sources/python/src/bindings/docstrings/probedoc.h:26
BufferOperator_::handle_buffer
virtual bool handle_buffer(Buffer buffer)=0
Probe::create
static unique_ptr< Probe > create(const string &plugin, const string &name)
Definition: service-maker/sources/python/src/bindings/probe.h:129
Probe
Definition: service-maker/sources/python/src/bindings/probe.h:98
Probe::Probe
Probe(const string &name, const char *factory, BatchMetadataOperator_ *handler)
Definition: service-maker/sources/python/src/bindings/probe.h:120
pydeepstreamdoc::probe::BufferObserverDoc::descr
constexpr const char * descr
Definition: service-maker/sources/python/src/bindings/docstrings/probedoc.h:34
BatchMetadataOperator_::probe_
BufferProbe * probe_
Definition: service-maker/sources/python/src/bindings/probe.h:82
BufferObserver_::handle_buffer
virtual void handle_buffer(const Buffer &buffer)=0
Probe::Probe
Probe(const string &name, const char *factory, BufferObserver_ *handler)
Definition: service-maker/sources/python/src/bindings/probe.h:104
PyBatchMetadataOperator
Definition: service-maker/sources/python/src/bindings/probe.h:85
BufferOperator_::handleBuffer
probeReturn handleBuffer(BufferProbe &probe, Buffer &buffer) override
Definition: service-maker/sources/python/src/bindings/probe.h:44
__attribute__
void __attribute__((constructor)) nvmsgbroker_init(void)
Probe::~Probe
virtual ~Probe()
Definition: service-maker/sources/python/src/bindings/probe.h:124
PyBufferObserver
Definition: service-maker/sources/python/src/bindings/probe.h:30
pydeepstreamdoc::probe::BufferOperatorDoc::handle_buffer
constexpr const char * handle_buffer
Definition: service-maker/sources/python/src/bindings/docstrings/probedoc.h:41
BufferObserver_
Definition: service-maker/sources/python/src/bindings/probe.h:20
BatchMetadataOperator_::handleData
probeReturn handleData(BufferProbe &probe, BatchMetadata &metadata) override
Definition: service-maker/sources/python/src/bindings/probe.h:66
deepstream::probeReturn
probeReturn
Return values from user implemented probe interfaces.
Definition: service-maker/includes/buffer_probe.hpp:47
pydeepstreamdoc::probe::ProbeDoc::create
constexpr const char * create
Definition: service-maker/sources/python/src/bindings/docstrings/probedoc.h:29
plugin
Definition: includes/opentelemetry/plugin/dynamic_load.h:19
Probe::Probe
Probe(const string &name, const char *factory, BufferOperator_ *handler)
Definition: service-maker/sources/python/src/bindings/probe.h:112
BatchMetadataOperator_
Definition: service-maker/sources/python/src/bindings/probe.h:64
module_probe_bind
class __attribute__((visibility("hidden"))) PythonProbeFactory void module_probe_bind(py::module &m)
CustomFactory backed by a Python class.
Definition: service-maker/sources/python/src/bindings/probe.h:182
pydeepstreamdoc::probe::ProbeDoc::init_batch_metadata_operator
constexpr const char * init_batch_metadata_operator
Definition: service-maker/sources/python/src/bindings/docstrings/probedoc.h:28
pydeepstreamdoc::probe::BatchMetadataOperatorDoc::handle_metadata
constexpr const char * handle_metadata
Definition: service-maker/sources/python/src/bindings/docstrings/probedoc.h:47
BufferObserver_::handleBuffer
probeReturn handleBuffer(BufferProbe &probe, const Buffer &buffer) override
Definition: service-maker/sources/python/src/bindings/probe.h:22
BatchMetadataOperator_::handle_metadata
virtual void handle_metadata(BatchMetadata &metadata)=0
Probe::Probe
Probe(const string &name, BufferObserver_ *handler)
Definition: service-maker/sources/python/src/bindings/probe.h:100
pydeepstreamdoc::probe::BufferOperatorDoc::descr
constexpr const char * descr
Definition: service-maker/sources/python/src/bindings/docstrings/probedoc.h:40
Probe::Probe
Probe(const string &name, BatchMetadataOperator_ *handler)
Definition: service-maker/sources/python/src/bindings/probe.h:116
pydeepstreamdoc::probe::BufferObserverDoc::handle_buffer
constexpr const char * handle_buffer
Definition: service-maker/sources/python/src/bindings/docstrings/probedoc.h:35
BatchMetadataOperator_::get_property
Object::Value get_property(const std::string &name)
Definition: service-maker/sources/python/src/bindings/probe.h:75
BufferOperator_
Definition: service-maker/sources/python/src/bindings/probe.h:42