NVIDIA DeepStream SDK API Reference

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