NVIDIA DeepStream SDK API Reference

9.1 Release
sources/gst-plugins/gst-nvdsvideotemplate/includes/nvdscustomlib_factory.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-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 #ifndef __NVDSCUSTOMLIB_FACTORY_HPP__
19 #define __NVDSCUSTOMLIB_FACTORY_HPP__
20 
21 #include <dlfcn.h>
22 #include <errno.h>
23 
24 #include <iostream>
25 #include <functional>
26 
28 
29 template<class T>
30 T* dlsym_ptr(void* handle, char const* name) {
31  return reinterpret_cast<T*>(dlsym(handle, name));
32 }
33 
35 {
36 public:
38  {
39  }
40 
42  {
43  if (m_libHandle)
44  {
45  dlclose(m_libHandle);
46  m_libHandle = NULL;
47  m_libName.clear();
48  }
49  }
50 
51  IDSCustomLibrary *CreateCustomAlgoCtx(std::string libName, GObject* object)
52  {
53  m_libName.assign(libName);
54 
55  m_libHandle = dlopen(m_libName.c_str(), RTLD_NOW);
56  std::function<IDSCustomLibrary*(GObject*)> createAlgoCtx = nullptr;
57  if (m_libHandle)
58  {
59  //std::cout << "Library Opened Successfully" << std::endl;
60 
61  createAlgoCtx = dlsym_ptr<IDSCustomLibrary*(GObject*)>(m_libHandle, "CreateCustomAlgoCtx");
62  if (!createAlgoCtx)
63  {
64  throw std::runtime_error("createCustomAlgoCtx function not found in library");
65  }
66  }
67  else
68  {
69  throw std::runtime_error(dlerror());
70  }
71 
72  return createAlgoCtx ? createAlgoCtx(object) : nullptr;
73  }
74 
76  {
77  m_libHandle = nullptr;
78  m_libName.clear();
79  return nullptr;
80  }
81 
82 public:
83  void *m_libHandle;
84  std::string m_libName;
85 };
86 
87 #endif
DSCustomLibrary_Factory::DSCustomLibrary_Factory
DSCustomLibrary_Factory()
Definition: sources/gst-plugins/gst-nvdsvideotemplate/includes/nvdscustomlib_factory.hpp:37
DSCustomLibrary_Factory
Definition: sources/gst-plugins/gst-nvdsvideotemplate/includes/nvdscustomlib_factory.hpp:34
DSCustomLibrary_Factory::Initialize
IDSCustomLibrary * Initialize()
Definition: sources/gst-plugins/gst-nvdsvideotemplate/includes/nvdscustomlib_factory.hpp:75
DSCustomLibrary_Factory::CreateCustomAlgoCtx
IDSCustomLibrary * CreateCustomAlgoCtx(std::string libName, GObject *object)
Definition: sources/gst-plugins/gst-nvdsvideotemplate/includes/nvdscustomlib_factory.hpp:51
IDSCustomLibrary
Definition: sources/gst-plugins/gst-nvdsvideotemplate/includes/nvdscustomlib_interface.hpp:51
nvdscustomlib_interface.hpp
DSCustomLibrary_Factory::~DSCustomLibrary_Factory
~DSCustomLibrary_Factory()
Definition: sources/gst-plugins/gst-nvdsvideotemplate/includes/nvdscustomlib_factory.hpp:41
dlsym_ptr
T * dlsym_ptr(void *handle, char const *name)
Definition: sources/gst-plugins/gst-nvdsvideotemplate/includes/nvdscustomlib_factory.hpp:30
DSCustomLibrary_Factory::m_libHandle
void * m_libHandle
Definition: sources/gst-plugins/gst-nvdsvideotemplate/includes/nvdscustomlib_factory.hpp:83
DSCustomLibrary_Factory::m_libName
std::string m_libName
Definition: sources/gst-plugins/gst-nvdsvideotemplate/includes/nvdscustomlib_factory.hpp:84