NVIDIA DeepStream SDK API Reference

7.1 Release
gst-nvdsaudiotemplate/common/nvdscustomlib_factory.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef __NVDSCUSTOMLIB_FACTORY_HPP__
25 #define __NVDSCUSTOMLIB_FACTORY_HPP__
26 
27 #include <dlfcn.h>
28 #include <errno.h>
29 
30 #include <iostream>
31 #include <functional>
32 
34 
35 template<class T>
36 T* dlsym_ptr(void* handle, char const* name) {
37  return reinterpret_cast<T*>(dlsym(handle, name));
38 }
39 
41 {
42 public:
44  {
45  }
46 
48  {
49  if (m_libHandle)
50  {
51  dlclose(m_libHandle);
52  m_libHandle = NULL;
53  m_libName.clear();
54  }
55  }
56 
57  IDSCustomLibrary *CreateCustomAlgoCtx(std::string libName, GObject* object)
58  {
59  m_libName.assign(libName);
60  m_libHandle = dlopen(m_libName.c_str(), RTLD_NOW);
61  std::function<IDSCustomLibrary*(GObject*)> createAlgoCtx = nullptr;
62  if (m_libHandle)
63  {
64  createAlgoCtx = dlsym_ptr<IDSCustomLibrary*(GObject*)>(m_libHandle, "CreateCustomAlgoCtx");
65  if (!createAlgoCtx)
66  {
67  throw std::runtime_error("createCustomAlgoCtx function not found in library");
68  }
69  }
70  else
71  {
72  throw std::runtime_error(dlerror());
73  }
74 
75  return createAlgoCtx ? createAlgoCtx(object) : nullptr;
76  }
77 
78 public:
79  void *m_libHandle;
80  std::string m_libName;
81 };
82 
83 #endif
DSCustomLibrary_Factory::DSCustomLibrary_Factory
DSCustomLibrary_Factory()
Definition: gst-nvdsaudiotemplate/common/nvdscustomlib_factory.hpp:43
DSCustomLibrary_Factory
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_factory.hpp:29
nvdscustomlib_interface.hpp
DSCustomLibrary_Factory::CreateCustomAlgoCtx
IDSCustomLibrary * CreateCustomAlgoCtx(std::string libName, GObject *object)
Definition: gst-nvdsaudiotemplate/common/nvdscustomlib_factory.hpp:57
IDSCustomLibrary
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_interface.hpp:43
dlsym_ptr
T * dlsym_ptr(void *handle, char const *name)
Definition: gst-nvdsaudiotemplate/common/nvdscustomlib_factory.hpp:36
DSCustomLibrary_Factory::~DSCustomLibrary_Factory
~DSCustomLibrary_Factory()
Definition: gst-nvdsaudiotemplate/common/nvdscustomlib_factory.hpp:47
DSCustomLibrary_Factory::m_libHandle
void * m_libHandle
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_factory.hpp:71
DSCustomLibrary_Factory::m_libName
std::string m_libName
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_factory.hpp:72