forked from NVIDIA-AI-IOT/deepstream_python_apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
162 lines (151 loc) · 6.89 KB
/
Copy pathutils.cpp
File metadata and controls
162 lines (151 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* SPDX-FileCopyrightText: Copyright (c) 2019-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gst/gst.h>
#include <glib.h>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <sys/timeb.h>
#include <functional>
#include <iostream>
#include "gstnvdsmeta.h"
#include "pyds.hpp"
#include "nvdsmeta_schema.h"
#include "utils.hpp"
#include "nvds_obj_encode.h"
#include "bind_string_property_definitions.h"
#include "../../docstrings/utilsdoc.h"
/**
* Specifies the type of function to copy meta data.
* It is passed the pointer to meta data and user specific data.
* It allocates the required memory, copies the content from src metadata
* and returns the pointer to newly allocated memory.
*
* user_data is for internal purpose only. User should set it to NULL.
*/
using COPYFUNC_SIG = gpointer(gpointer, gpointer);
/**
* Specifies the type of function to free meta data.
* It is passed the pointer to meta data and user specific data.
* It should free any resource allocated for meta data.
*
* user_data is for internal purpose only. User should set it to NULL.
*/
using RELEASEFUNC_SIG = void(gpointer, gpointer);
namespace pydeepstream {
pybind11::arg operator ""_a(const char *str, size_t len) {
return pybind11::arg(str);
}
}
// Utils
namespace pydeepstream {
void bindutils(py::module &m) {
py::class_<NvDsObjEncOutParams>(m, "NvDsObjEncOutParams",
pydsdoc::utilsdoc::NvDsObjEncOutParamsDoc::descr)
.def(py::init<>())
.def("outBuffer", [](const NvDsObjEncOutParams &self) {
// Use this if the C++ buffer should NOT be deallocated
// once Python no longer has a reference to it
py::capsule buffer_handle([](){});
return py::array(self.outLen, self.outBuffer, buffer_handle);
})
.def("cast", [](size_t data) {
return (NvDsObjEncOutParams *) data;
},
py::return_value_policy::reference,
pydsdoc::utilsdoc::NvDsObjEncOutParamsDoc::cast)
.def("cast", [](void *data) {
return (NvDsObjEncOutParams *) data;
},
py::return_value_policy::reference,
pydsdoc::utilsdoc::NvDsObjEncOutParamsDoc::cast);
py::class_<NvDsObjEncUsrArgs>(m, "NvDsObjEncUsrArgs",
pydsdoc::utilsdoc::NvDsObjEncUsrArgsDoc::descr)
.def(py::init<>())
.def_readwrite("saveImg", &NvDsObjEncUsrArgs::saveImg)
.def_readwrite("attachUsrMeta", &NvDsObjEncUsrArgs::attachUsrMeta)
.def_readwrite("scaleImg", &NvDsObjEncUsrArgs::scaleImg)
.def_readwrite("scaledWidth", &NvDsObjEncUsrArgs::scaledWidth)
.def_readwrite("scaledHeight", &NvDsObjEncUsrArgs::scaledHeight)
.def_property("fileNameImg", STRING_CHAR_ARRAY(NvDsObjEncUsrArgs, fileNameImg))
.def_readwrite("objNum", &NvDsObjEncUsrArgs::objNum)
.def_readwrite("quality", &NvDsObjEncUsrArgs::quality)
.def_readwrite("isFrame", &NvDsObjEncUsrArgs::isFrame)
.def_readwrite("calcEncodeTime", &NvDsObjEncUsrArgs::calcEncodeTime)
.def("cast",
[](size_t data) {
return (NvDsObjEncUsrArgs *) data;
},
py::return_value_policy::reference,
pydsdoc::utilsdoc::NvDsObjEncUsrArgsDoc::cast)
.def("cast",
[](void *data) {
return (NvDsObjEncUsrArgs *) data;
},
py::return_value_policy::reference,
pydsdoc::utilsdoc::NvDsObjEncUsrArgsDoc::cast);
}
}
namespace pydeepstream::utils {
std::unordered_map<std::string, std::shared_ptr<char>> font_name_memory;
static const char copyfuncname[] = "copy_func"; // must be unique
static const char freefuncname[] = "free_func"; // must be unique
/// The returned pointer from get_fn_ptr_from_std_function is the same for all
/// instance templated on 'copy_func' string literal. Therefore it must only be
/// stored at one place. In case of multiple registration the latest stored
/// std::function will be the one called. The std::function 'func' is stored
/// statically.
void
set_copyfunc(NvDsUserMeta *meta,
std::function<COPYFUNC_SIG> const &func) {
// must be stored only at 1 place
meta->base_meta.copy_func = get_fn_ptr_from_std_function<copyfuncname>(func);
}
/// same as set_copyfunc
void set_freefunc(NvDsUserMeta *meta,
std::function<RELEASEFUNC_SIG> const &func) {
// must be stored only at 1 place
meta->base_meta.release_func = get_fn_ptr_from_std_function<freefuncname>(func);
}
void
__attribute__((optimize("O0")))
release_all_func() {
// these dummy functions are only used for their type to match the appropriate template
const auto dummy_copy_func = std::function<COPYFUNC_SIG>();
const auto dummy_free_func = std::function<RELEASEFUNC_SIG>();
// Here the functions must be freed because otherwise the destructor will be stuck with GIL
// this is related to pybind11 behavior.
// As far as I understand pybind11 does not expect us to store functions in a static storage
// it tries to acquire a lock inside its custom destructor of these functions, but the lock was
// destroyed before, thus creating a deadlock at program exit...
free_fn_ptr_from_std_function<copyfuncname>(dummy_copy_func);
free_fn_ptr_from_std_function<freefuncname>(dummy_free_func);
}
void generate_ts_rfc3339(char *buf, int buf_size) {
time_t tloc;
struct tm tm_log{};
struct timespec ts{};
char strmsec[6]; //.nnnZ\0
clock_gettime(CLOCK_REALTIME, &ts);
memcpy(&tloc, (void *) (&ts.tv_sec), sizeof(time_t));
gmtime_r(&tloc, &tm_log);
strftime(buf, buf_size, "%Y-%m-%dT%H:%M:%S", &tm_log);
int ms = ts.tv_nsec / 1000000;
g_snprintf(strmsec, sizeof(strmsec), ".%.3dZ", ms);
strncat(buf, strmsec, buf_size);
}
}