NVIDIA DeepStream SDK API Reference

8.0 Release
nvmsgbroker_internal.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 /*
13  * This file defines the NvMsgBroker interface.
14  * The interfaces is used by applications to send and receive
15  * messages from remote entities and services to deliver events, allow
16  * configuration of settings etc.
17  */
18 
19 #ifndef __NV_MSGBROKER_INTERNAL_H__
20 #define __NV_MSGBROKER_INTERNAL_H__
21 
22 #ifdef __cplusplus
23 extern "C"
24 {
25 #endif
26 
27 #define DEFAULT_RETRY_INTERVAL 2
28 #define DEFAULT_MAX_RETRY_LIMIT 360
29 #define DEFAULT_WORK_INTERVAL 10000
30 
31 //config settings applicable for nvmsgbroker library
32 const char *nvmsgbrokerConfig = "/opt/nvidia/deepstream/deepstream/sources/libs/nvmsgbroker/cfg_nvmsgbroker.txt";
33 
34 bool auto_reconnect; //Flag to indicate autoreconnection
35 unsigned retry_interval; //connection retry interval in secs
36 unsigned max_retry_limit; //max retry limit in secs
37 unsigned work_interval; //interval at which to perform work, in microseconds
38 
39 typedef enum {
44 
45 //Structure used to store synchronization primitives
46 typedef struct {
47  pthread_t tid; //thread id
48  pthread_mutex_t lock; //lock
49  pthread_cond_t cv; //condition variable
51 
52 //Pointer to Adapter Api's
53 typedef struct proto_adapter{
54  string protocol; //Protocol name
55  void *so_handle; //Adapter shared library handle
56  int ref_count; //count num of lib open references
57  bool subscribe_capability; //Flag to indicate if the adapter lib supports topic subscription
58  bool signature_capability; //Indicate if adapter lib implements connection signature query api
59  NvDsMsgApiHandle (*nvds_msgapi_connect_ptr)(char *connection_str, nvds_msgapi_connect_cb_t connect_cb, char *config_path);
61  NvDsMsgApiErrorType (*nvds_msgapi_send_async_ptr)(NvDsMsgApiHandle conn, char *topic, const uint8_t *payload, size_t nbuf, nvds_msgapi_send_cb_t send_callback, void *user_ptr);
63  NvDsMsgApiErrorType (*nvds_msgapi_subscribe_ptr)(NvDsMsgApiHandle conn, char **topics, int num_topics, nvds_msgapi_subscribe_request_cb_t cb, void *user_ctx);
64  char* (*nvds_msgapi_getversion_ptr)(void);
65  char* (*nvds_msgapi_get_protocol_name_ptr)(void);
66  NvDsMsgApiErrorType (*nvds_msgapi_connection_signature_ptr)(char *connection_str, char *config_path, char *output_str, int max_len);
67 
69 }AdapterLib;
70 
71 //Structure to hold info used within a NvMsgBrokerHandle
72 typedef struct {
73  int ref_count; //count num of components calling connect
74  string broker_proto_so; //Adapter library so name
75  string broker_conn_str; //Broker connection string(hostname;port)
76  string broker_cfg_path; //Broker config file path
77  string broker_signature; //Broker connection string signature
78  NvDsMsgApiHandle adapter_h_ptr; //Adapter Connection handle
79  AdapterLib *adapter_lib; //Adapter shared library handle
80  nvds_msgapi_subscribe_request_cb_t subscribe_cb; //callback for consuming messages from adapter lib
81  unordered_set<nv_msgbroker_connect_cb_t> connect_cb_set; //saved list of connect cb() for the handle
82  unordered_set<nv_msgbroker_send_cb_t> send_cb_set; //saved list of send cb() for the handle
83  unordered_map<string, set<pair<nv_msgbroker_subscribe_cb_t, void *>>> subscribe_topic_map; //topic → {subscribe_cb(), user_ctx}
84  NvMsgBrokerThread_t do_work_thread; //Used for calling adapter do_work()
85  pthread_mutex_t subscribe_lock; //Used for calling adapter subscribe()
86  int pending_send_cnt; //num of msgs waiting to be sent
87  bool disconnect; //Flag used to notify dowork thread to quit
88  bool reconnecting; //Flag to notify if reconnection attempt is in progress
90 
91 //Structure to store user data during a send_async operation
92 typedef struct {
93  nv_msgbroker_send_cb_t send_cb; //User send callback func pointer
94  void *user_ptr; //Pointer to user context
95  NvMsgBrokerHandle *h_ptr; //Pointer to msgbroker connection handle
97 
98 //Containers used by nvmsgbroker library
99 map<pair<string, AdapterLib *>, NvMsgBrokerHandle *> conn_string_handle_map; //Map connection string --> connection handle
100 unordered_set<NvMsgBrokerHandle *> conn_Handle_list; //List of connection handles
101 unordered_map<NvDsMsgApiHandle, NvMsgBrokerHandle *> adapter_msgbroker_map; //Map Adapter handle --> msgbroker handle
102 unordered_map<void *, AdapterLib *> so_handle_map; //Map so_handle --> adapter lib
103 
104 //Lock to use msgbroker connection handle h_ptr
105 pthread_mutex_t h_ptr_lock;
106 
107 //Callbacks and functions used internally within nvmsgbroker lib
108 void *do_work_func(void *);
109 void adapter_connect_cb(NvDsMsgApiHandle *adapter_h_ptr, NvDsMsgApiEventType ds_evt);
110 void adapter_send_cb(void *user_ptr, NvDsMsgApiErrorType completion_flag);
111 void adapter_subscribe_cb(NvDsMsgApiErrorType flag, void *msg, int msglen, char *topic, void *user_ptr);
112 void handle_error(void *handle, pthread_mutex_t *lock, AdapterLib * lib, const char *log, const char *error);
113 bool fetch_adapter_api(void *so_handle, AdapterLib *LIB);
115 void __attribute__ ((constructor)) nvmsgbroker_init(void);
116 void __attribute__ ((destructor)) nvmsgbroker_deinit(void);
117 
118 #ifdef __cplusplus
119 }
120 #endif
121 
122 #endif
NvMsgBrokerHandle::disconnect
bool disconnect
Definition: nvmsgbroker_internal.h:87
SUCCESS
@ SUCCESS
Definition: nvmsgbroker_internal.h:40
proto_adapter::nvds_msgapi_subscribe_ptr
NvDsMsgApiErrorType(* nvds_msgapi_subscribe_ptr)(NvDsMsgApiHandle conn, char **topics, int num_topics, nvds_msgapi_subscribe_request_cb_t cb, void *user_ctx)
Definition: nvmsgbroker_internal.h:63
proto_adapter::nvds_msgapi_connection_signature_ptr
NvDsMsgApiErrorType(* nvds_msgapi_connection_signature_ptr)(char *connection_str, char *config_path, char *output_str, int max_len)
Definition: nvmsgbroker_internal.h:66
NvMsgBrokerThread_t::cv
pthread_cond_t cv
Definition: nvmsgbroker_internal.h:49
fetch_adapter_api
bool fetch_adapter_api(void *so_handle, AdapterLib *LIB)
work_interval
unsigned work_interval
Definition: nvmsgbroker_internal.h:37
nvds_msgapi_connect_cb_t
void(* nvds_msgapi_connect_cb_t)(NvDsMsgApiHandle h_ptr, NvDsMsgApiEventType ds_evt)
Type definition for a "handle" callback.
Definition: nvds_msgapi.h:98
proto_adapter::ref_count
int ref_count
Definition: nvmsgbroker_internal.h:56
ERROR
@ ERROR
Definition: nvmsgbroker_internal.h:42
__attribute__
void __attribute__((constructor)) nvmsgbroker_init(void)
NvMsgBrokerThread_t::lock
pthread_mutex_t lock
Definition: nvmsgbroker_internal.h:48
NvDsMsgApiErrorType
NvDsMsgApiErrorType
Defines completion codes for operations in the messaging API.
Definition: nvds_msgapi.h:57
proto_adapter::so_handle
void * so_handle
Definition: nvmsgbroker_internal.h:55
NvMsgBrokerHandle::broker_cfg_path
string broker_cfg_path
Definition: nvmsgbroker_internal.h:76
retry_interval
unsigned retry_interval
Definition: nvmsgbroker_internal.h:35
NvMsgBrokerSendCB_info::h_ptr
NvMsgBrokerHandle * h_ptr
Definition: nvmsgbroker_internal.h:95
NvMsgBrokerHandle::subscribe_cb
nvds_msgapi_subscribe_request_cb_t subscribe_cb
Definition: nvmsgbroker_internal.h:80
adapter_send_cb
void adapter_send_cb(void *user_ptr, NvDsMsgApiErrorType completion_flag)
NvMsgBrokerSendCB_info
Definition: nvmsgbroker_internal.h:92
nvmsgbrokerConfig
const char * nvmsgbrokerConfig
Definition: nvmsgbroker_internal.h:32
proto_adapter
Definition: nvmsgbroker_internal.h:53
adapter_msgbroker_map
unordered_map< NvDsMsgApiHandle, NvMsgBrokerHandle * > adapter_msgbroker_map
Definition: nvmsgbroker_internal.h:101
NvMsgBrokerHandle::connect_cb_set
unordered_set< nv_msgbroker_connect_cb_t > connect_cb_set
Definition: nvmsgbroker_internal.h:81
NvMsgBrokerHandle::broker_conn_str
string broker_conn_str
Definition: nvmsgbroker_internal.h:75
NvMsgBrokerSendCB_info::send_cb
nv_msgbroker_send_cb_t send_cb
Definition: nvmsgbroker_internal.h:93
NvMsgBrokerSendCB_info::user_ptr
void * user_ptr
Definition: nvmsgbroker_internal.h:94
proto_adapter::proto_adapter
proto_adapter()
Definition: nvmsgbroker_internal.h:68
NvMsgBrokerHandle::reconnecting
bool reconnecting
Definition: nvmsgbroker_internal.h:88
NvMsgBrokerHandle
Definition: nvmsgbroker_internal.h:72
NvDsMsgApiHandle
void * NvDsMsgApiHandle
Defines the handle used by messaging API functions.
Definition: nvds_msgapi.h:40
proto_adapter::signature_capability
bool signature_capability
Definition: nvmsgbroker_internal.h:58
NvMsgBrokerHandle::pending_send_cnt
int pending_send_cnt
Definition: nvmsgbroker_internal.h:86
NvDsMsgApiEventType
NvDsMsgApiEventType
Defines events associated with connections to remote entities.
Definition: nvds_msgapi.h:45
proto_adapter::nvds_msgapi_send_async_ptr
NvDsMsgApiErrorType(* nvds_msgapi_send_async_ptr)(NvDsMsgApiHandle conn, char *topic, const uint8_t *payload, size_t nbuf, nvds_msgapi_send_cb_t send_callback, void *user_ptr)
Definition: nvmsgbroker_internal.h:61
so_handle_map
unordered_map< void *, AdapterLib * > so_handle_map
Definition: nvmsgbroker_internal.h:102
conn_string_handle_map
map< pair< string, AdapterLib * >, NvMsgBrokerHandle * > conn_string_handle_map
Definition: nvmsgbroker_internal.h:99
NvMsgBrokerReturnVal
NvMsgBrokerReturnVal
Definition: nvmsgbroker_internal.h:39
NvMsgBrokerHandle::broker_proto_so
string broker_proto_so
Definition: nvmsgbroker_internal.h:74
reconnect
NvMsgBrokerReturnVal reconnect(NvMsgBrokerHandle *h_ptr)
AdapterLib
struct proto_adapter AdapterLib
adapter_subscribe_cb
void adapter_subscribe_cb(NvDsMsgApiErrorType flag, void *msg, int msglen, char *topic, void *user_ptr)
FAIL
@ FAIL
Definition: nvmsgbroker_internal.h:41
NvMsgBrokerHandle::adapter_h_ptr
NvDsMsgApiHandle adapter_h_ptr
Definition: nvmsgbroker_internal.h:78
NvMsgBrokerHandle::adapter_lib
AdapterLib * adapter_lib
Definition: nvmsgbroker_internal.h:79
NvMsgBrokerHandle::subscribe_lock
pthread_mutex_t subscribe_lock
Definition: nvmsgbroker_internal.h:85
h_ptr_lock
pthread_mutex_t h_ptr_lock
Definition: nvmsgbroker_internal.h:105
max_retry_limit
unsigned max_retry_limit
Definition: nvmsgbroker_internal.h:36
proto_adapter::nvds_msgapi_disconnect_ptr
NvDsMsgApiErrorType(* nvds_msgapi_disconnect_ptr)(NvDsMsgApiHandle h_ptr)
Definition: nvmsgbroker_internal.h:60
proto_adapter::subscribe_capability
bool subscribe_capability
Definition: nvmsgbroker_internal.h:57
NvMsgBrokerHandle::subscribe_topic_map
unordered_map< string, set< pair< nv_msgbroker_subscribe_cb_t, void * > > > subscribe_topic_map
Definition: nvmsgbroker_internal.h:83
NvMsgBrokerThread_t::tid
pthread_t tid
Definition: nvmsgbroker_internal.h:47
NvMsgBrokerHandle::ref_count
int ref_count
Definition: nvmsgbroker_internal.h:73
NvMsgBrokerHandle::do_work_thread
NvMsgBrokerThread_t do_work_thread
Definition: nvmsgbroker_internal.h:84
NvMsgBrokerHandle::broker_signature
string broker_signature
Definition: nvmsgbroker_internal.h:77
NvMsgBrokerHandle::send_cb_set
unordered_set< nv_msgbroker_send_cb_t > send_cb_set
Definition: nvmsgbroker_internal.h:82
handle_error
void handle_error(void *handle, pthread_mutex_t *lock, AdapterLib *lib, const char *log, const char *error)
proto_adapter::nvds_msgapi_do_work_ptr
void(* nvds_msgapi_do_work_ptr)(NvDsMsgApiHandle h_ptr)
Definition: nvmsgbroker_internal.h:62
do_work_func
void * do_work_func(void *)
proto_adapter::nvds_msgapi_connect_ptr
NvDsMsgApiHandle(* nvds_msgapi_connect_ptr)(char *connection_str, nvds_msgapi_connect_cb_t connect_cb, char *config_path)
Definition: nvmsgbroker_internal.h:59
nvds_msgapi_send_cb_t
void(* nvds_msgapi_send_cb_t)(void *user_ptr, NvDsMsgApiErrorType completion_flag)
Type definition for a "send" callback.
Definition: nvds_msgapi.h:71
proto_adapter::protocol
string protocol
Definition: nvmsgbroker_internal.h:54
auto_reconnect
bool auto_reconnect
Definition: nvmsgbroker_internal.h:34
conn_Handle_list
unordered_set< NvMsgBrokerHandle * > conn_Handle_list
Definition: nvmsgbroker_internal.h:100
adapter_connect_cb
void adapter_connect_cb(NvDsMsgApiHandle *adapter_h_ptr, NvDsMsgApiEventType ds_evt)
nvds_msgapi_subscribe_request_cb_t
void(* nvds_msgapi_subscribe_request_cb_t)(NvDsMsgApiErrorType flag, void *msg, int msg_len, char *topic, void *user_ptr)
Type definition for callback registered during subscribe.
Definition: nvds_msgapi.h:87
NvMsgBrokerThread_t
Definition: nvmsgbroker_internal.h:46
nv_msgbroker_send_cb_t
void(* nv_msgbroker_send_cb_t)(void *user_ptr, NvMsgBrokerErrorType status)
Send callback method registered during send_async.
Definition: nvmsgbroker.h:68