UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
ndi_common.h
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2022-2024 CESNET
7 * All rights reserved.
8 *
9 * Using sample code from NDI.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, is permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * 3. Neither the name of CESNET nor the names of its contributors may be
23 * used to endorse or promote products derived from this software without
24 * specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
28 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
30 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
31 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
37 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#ifndef NDI_COMMON_H_1A76D048_695C_4247_A24A_583C29010FC4
41#define NDI_COMMON_H_1A76D048_695C_4247_A24A_583C29010FC4
42
43#include <stdlib.h>
44#include <string.h>
45
46#define NDILIB_CPP_DEFAULT_CONSTRUCTORS 0
47#include <Processing.NDI.Lib.h>
48
49#include "compat/dlfunc.h"
50#include "debug.h"
51#include "utils/color_out.h"
52#include "utils/macros.h" // MAX, MERGE, TOSTRING
53
54#ifndef USE_NDI_VERSION
55#define USE_NDI_VERSION 6
56#endif
57
58#if USE_NDI_VERSION >= 6
59#define NDI_API_VERSION 5
60#else
61#define NDI_API_VERSION USE_NDI_VERSION
62#endif
63
64#ifdef __linux__
65#define FALLBACK_NDI_PATH "/usr/lib"
66#elif defined __APPLE__
67// redist NDI for Apple uses /usr/local/lib, which is tried prior to this path
68#define FALLBACK_NDI_PATH "/Library/NDI SDK for Apple/Lib/macOS"
69#else
70#define FALLBACK_NDI_PATH "C:\\Program Files\\NDI\\NDI " TOSTRING(USE_NDI_VERSION) " Runtime\\v" TOSTRING(USE_NDI_VERSION)
71#endif
72#define NDILIB_NDI_LOAD "NDIlib_v" TOSTRING(NDI_API_VERSION) "_load"
73#define MAKE_NDI_LIB_NAME(ver) MERGE(NDIlib_v,ver)
75typedef const NDIlib_t* NDIlib_load_f(void);
76
77static const NDIlib_t *NDIlib_load(LIB_HANDLE *lib) {
78#ifdef _WIN32
79 // We check whether the NDI run-time is installed
80 const char* p_ndi_runtime = getenv(NDILIB_REDIST_FOLDER);
81 if (!p_ndi_runtime) {
82 p_ndi_runtime = FALLBACK_NDI_PATH;
84 "[NDI] " NDILIB_REDIST_FOLDER " environment variable not defined. "
85 "Trying fallback folder: %s\n",
87 }
88
89 // We now load the DLL as it is installed
90 const size_t path_len = strlen(p_ndi_runtime) + 2 + strlen(NDILIB_LIBRARY_NAME) + 1;
91 char *ndi_path = (char *)alloca(path_len);
92 strncpy(ndi_path, p_ndi_runtime, path_len - 1);
93 strncat(ndi_path, "\\", path_len - strlen(ndi_path) - 1);
94 strncat(ndi_path, NDILIB_LIBRARY_NAME, path_len - strlen(ndi_path) - 1);
95
96 // Try to load the library
97 HMODULE hNDILib = LoadLibraryA(ndi_path);
98
99 // The main NDI entry point for dynamic loading if we got the library
100 const NDIlib_t* (*NDIlib_load)(void) = NULL;
101 if (hNDILib) {
102 *((FARPROC*)&NDIlib_load) = GetProcAddress(hNDILib, NDILIB_NDI_LOAD);
103 }
104
105 // If we failed to load the library then we tell people to re-install it
106 if (!NDIlib_load) { // Unload the DLL if we loaded it
107 // The NDI run-time is not installed correctly. Let the user know and take them to the download URL.
108 log_msg(LOG_LEVEL_ERROR, "[NDI] Failed to load " NDILIB_NDI_LOAD " from NDI: %s.\n"
109 "Please install the NewTek NDI Runtimes to use this module from " NDILIB_REDIST_URL ".\n", dlerror());
110 if (hNDILib) {
111 FreeLibrary(hNDILib);
112 }
113
114 return 0;
115 }
116#else
117 const char *lib_cand[3] = {getenv(NDILIB_REDIST_FOLDER) ? getenv(NDILIB_REDIST_FOLDER) : "",
118 "/usr/local/lib", FALLBACK_NDI_PATH};
119 void *hNDILib = NULL;
120 const char *last_err = "(none)";
121 for (unsigned int i = 0; i < sizeof lib_cand / sizeof lib_cand[0]; i++) {
122 if (i > 0) {
123 log_msg(LOG_LEVEL_INFO, "[NDI] Trying to load from fallback location: %s\n",
124 lib_cand[i]);
125 }
126 size_t path_len = strlen(lib_cand[i]) + 1 + strlen(NDILIB_LIBRARY_NAME) + 1;
127 char *ndi_path = (char *)alloca(path_len);
128 strncpy(ndi_path, lib_cand[i], path_len - 1);
129 if (strlen(ndi_path) > 0) {
130 strncat(ndi_path, "/", path_len - strlen(ndi_path) - 1);
131 }
132 strncat(ndi_path, NDILIB_LIBRARY_NAME, path_len - strlen(ndi_path) - 1);
133 // Try to load the library
134 hNDILib = dlopen(ndi_path, RTLD_LOCAL | RTLD_LAZY);
135 if (hNDILib) {
136 break;
137 }
138 last_err = dlerror();
140 "[NDI] Failed to open the library: %s\n", last_err);
141 }
142
143 // The main NDI entry point for dynamic loading if we got the library
144 const NDIlib_t* (*NDIlib_load)(void) = NULL;
145 if (hNDILib) {
146 *((void**)&NDIlib_load) = dlsym(hNDILib, NDILIB_NDI_LOAD);
147 }
148
149 // If we failed to load the library then we tell people to re-install it
150 if (!NDIlib_load) { // Unload the library if we loaded it
152 "[NDI] Failed to open the library: %s\n", last_err);
153
154 if (strlen(NDILIB_REDIST_URL) != 0) {
155 log_msg(LOG_LEVEL_ERROR, "[NDI] Please re-install the NewTek NDI Runtimes from " NDILIB_REDIST_URL " to use this application.\n");
156 } else { // NDILIB_REDIST_URL is set to "" in Linux
157 log_msg(LOG_LEVEL_ERROR, "[NDI] Please install " NDILIB_LIBRARY_NAME " from the NewTek NDI SDK either to system libraries' path or set "
158 NDILIB_REDIST_FOLDER " environment "
159 "variable to a path containing the libndi.so file (eg. \"export " NDILIB_REDIST_FOLDER "=<NDI_SDK_PATH>/lib/x86_64-linux-gnu\").\n");
160 }
161
162 if (hNDILib) {
163 dlclose(hNDILib);
164 }
165 return 0;
166 }
167#endif
168 const NDIlib_t *ret = NDIlib_load();
169 if (ret == NULL) {
170 dlclose(hNDILib);
171 } else {
172 *lib = hNDILib;
173 }
174 return ret;
175}
176
177static void close_ndi_library(LIB_HANDLE hNDILib) {
178 if (!hNDILib) {
179 return;
180 }
181 dlclose(hNDILib);
182}
183
184#define NDI_PRINT_COPYRIGHT \
185 color_printf( \
186 TERM_BOLD TERM_FG_BLUE u8"This application uses NDIĀ® available " \
187 u8"from https://ndi.video/\n" \
188 u8"NDIĀ® is a registered trademark of " \
189 u8"Vizrt NDI AB.\n\n" TERM_RESET); \
190 int not_defined_function
191
192#endif // defined NDI_COMMON_H_1A76D048_695C_4247_A24A_583C29010FC4
193
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_INFO
normal reporting
Definition debug.h:62
#define LOG_LEVEL_ERROR
general errors
Definition debug.h:59
#define LOG_LEVEL_WARNING
less severe errors
Definition debug.h:60
#define LIB_HANDLE
Definition dlfunc.h:51
#define FALLBACK_NDI_PATH
Definition ndi_common.h:70
static void close_ndi_library(LIB_HANDLE hNDILib)
Definition ndi_common.h:177
#define MAKE_NDI_LIB_NAME(ver)
Definition ndi_common.h:73
#define NDILIB_NDI_LOAD
Definition ndi_common.h:72
static const NDIlib_t * NDIlib_load(LIB_HANDLE *lib)
Definition ndi_common.h:77
const NDIlib_t * NDIlib_load_f(void)
Definition ndi_common.h:75
#define NDI_API_VERSION
Definition ndi_common.h:59