grandi 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+ /* Copyright 2018 Streampunk Media Ltd.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
14
+ */
15
+
16
+ #ifndef GRANDI_SEND_H
17
+ #define GRANDI_SEND_H
18
+
19
+ #include <string>
20
+ #include "node_api.h"
21
+ #include "grandi_util.h"
22
+
23
+ napi_value send(napi_env env, napi_callback_info info);
24
+
25
+ struct sendCarrier : carrier {
26
+ char *name = nullptr;
27
+ char *groups = nullptr;
28
+ bool clockVideo = false;
29
+ bool clockAudio = false;
30
+ NDIlib_send_instance_t send;
31
+ ~sendCarrier() {
32
+ free(name);
33
+ free(groups);
34
+ }
35
+ };
36
+
37
+ struct sendDataCarrier : carrier {
38
+ NDIlib_send_instance_t send;
39
+ NDIlib_video_frame_v2_t videoFrame;
40
+ NDIlib_audio_frame_v3_t audioFrame;
41
+ NDIlib_metadata_frame_t metadataFrame;
42
+ std::string frameMetadata;
43
+ };
44
+
45
+ #endif /* GRANDI_SEND_H */
@@ -0,0 +1,323 @@
1
+ /* Copyright 2018 Streampunk Media Ltd.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
14
+ */
15
+
16
+ #include <assert.h>
17
+ #include <stdio.h>
18
+ #include <stdlib.h>
19
+ #include <chrono>
20
+ #include <string>
21
+ #include <algorithm>
22
+ #include <Processing.NDI.Lib.h>
23
+ #include "grandi_util.h"
24
+ #include "node_api.h"
25
+ using namespace std;
26
+
27
+ // Implementation of itoa()
28
+ char *custom_itoa(int num, char *str, int base) {
29
+ int i = 0;
30
+ bool isNegative = false;
31
+
32
+ /* Handle 0 explicitely, otherwise empty string is printed for 0 */
33
+ if (num == 0) {
34
+ str[i++] = '0';
35
+ str[i] = '\0';
36
+ return str;
37
+ }
38
+
39
+ // In standard itoa(), negative numbers are handled only with
40
+ // base 10. Otherwise numbers are considered unsigned.
41
+ if (num < 0 && base == 10) {
42
+ isNegative = true;
43
+ num = -num;
44
+ }
45
+
46
+ // Process individual digits
47
+ while (num != 0) {
48
+ int rem = num % base;
49
+ str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
50
+ num = num / base;
51
+ }
52
+
53
+ // If number is negative, append '-'
54
+ if (isNegative)
55
+ str[i++] = '-';
56
+
57
+ str[i] = '\0'; // Append string terminator
58
+
59
+ // Reverse the string
60
+ std::reverse(std::string(str).begin(), std::string(str).end());
61
+
62
+ return str;
63
+ }
64
+
65
+ napi_status checkStatus(napi_env env, napi_status status, const char *file,
66
+ uint32_t line) {
67
+
68
+ napi_status infoStatus, throwStatus;
69
+ const napi_extended_error_info *errorInfo;
70
+
71
+ if (status == napi_ok) {
72
+ return status;
73
+ }
74
+
75
+ infoStatus = napi_get_last_error_info(env, &errorInfo);
76
+ assert(infoStatus == napi_ok);
77
+ printf("NAPI error in file %s on line %i. Error %i: %s\n", file, line,
78
+ errorInfo->error_code, errorInfo->error_message);
79
+
80
+ if (status == napi_pending_exception) {
81
+ printf("NAPI pending exception. Engine error code: %i\n",
82
+ errorInfo->engine_error_code);
83
+ return status;
84
+ }
85
+
86
+ char errorCode[20];
87
+ throwStatus =
88
+ napi_throw_error(env, custom_itoa(errorInfo->error_code, errorCode, 10),
89
+ errorInfo->error_message);
90
+ assert(throwStatus == napi_ok);
91
+
92
+ return napi_pending_exception; // Expect to be cast to void
93
+ }
94
+
95
+ long long microTime(std::chrono::high_resolution_clock::time_point start) {
96
+ auto elapsed = std::chrono::high_resolution_clock::now() - start;
97
+ return std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
98
+ }
99
+
100
+ const char *getNapiTypeName(napi_valuetype t) {
101
+ switch (t) {
102
+ case napi_undefined:
103
+ return "undefined";
104
+ case napi_null:
105
+ return "null";
106
+ case napi_boolean:
107
+ return "boolean";
108
+ case napi_number:
109
+ return "number";
110
+ case napi_string:
111
+ return "string";
112
+ case napi_symbol:
113
+ return "symbol";
114
+ case napi_object:
115
+ return "object";
116
+ case napi_function:
117
+ return "function";
118
+ case napi_external:
119
+ return "external";
120
+ default:
121
+ return "unknown";
122
+ }
123
+ }
124
+
125
+ napi_status checkArgs(napi_env env, napi_callback_info info, char *methodName,
126
+ napi_value *args, size_t argc, napi_valuetype *types) {
127
+
128
+ napi_status status;
129
+
130
+ size_t realArgc = argc;
131
+ status = napi_get_cb_info(env, info, &realArgc, args, nullptr, nullptr);
132
+ if (status != napi_ok)
133
+ return status;
134
+
135
+ if (realArgc != argc) {
136
+ char errorMsg[100];
137
+ sprintf(errorMsg, "For method %s, expected %zi arguments and got %zi.",
138
+ methodName, argc, realArgc);
139
+ napi_throw_error(env, nullptr, errorMsg);
140
+ return napi_pending_exception;
141
+ }
142
+
143
+ napi_valuetype t;
144
+ for (int x = 0; x < (int)argc; x++) {
145
+ status = napi_typeof(env, args[x], &t);
146
+ if (status != napi_ok)
147
+ return status;
148
+ if (t != types[x]) {
149
+ char errorMsg[100];
150
+ sprintf(errorMsg,
151
+ "For method %s argument %i, expected type %s and got %s.",
152
+ methodName, x + 1, getNapiTypeName(types[x]), getNapiTypeName(t));
153
+ napi_throw_error(env, nullptr, errorMsg);
154
+ return napi_pending_exception;
155
+ }
156
+ }
157
+
158
+ return napi_ok;
159
+ };
160
+
161
+ void tidyCarrier(napi_env env, carrier *c) {
162
+ napi_status status;
163
+ if (c->passthru != nullptr) {
164
+ status = napi_delete_reference(env, c->passthru);
165
+ FLOATING_STATUS;
166
+ }
167
+ if (c->_request != nullptr) {
168
+ status = napi_delete_async_work(env, c->_request);
169
+ FLOATING_STATUS;
170
+ }
171
+ delete c;
172
+ }
173
+
174
+ int32_t rejectStatus(napi_env env, carrier *c, const char *file, int32_t line) {
175
+ if (c->status != GRANDI_SUCCESS) {
176
+ napi_value errorValue, errorCode, errorMsg;
177
+ napi_status status;
178
+ char errorChars[20];
179
+ if (c->status < GRANDI_ERROR_START) {
180
+ const napi_extended_error_info *errorInfo;
181
+ status = napi_get_last_error_info(env, &errorInfo);
182
+ FLOATING_STATUS;
183
+ c->errorMsg = std::string(errorInfo->error_message);
184
+ }
185
+ char *extMsg = (char *)malloc(sizeof(char) * c->errorMsg.length() + 200);
186
+ sprintf(extMsg, "In file %s on line %i, found error: %s", file, line,
187
+ c->errorMsg.c_str());
188
+ status =
189
+ napi_create_string_utf8(env, custom_itoa(c->status, errorChars, 10),
190
+ NAPI_AUTO_LENGTH, &errorCode);
191
+ FLOATING_STATUS;
192
+ status = napi_create_string_utf8(env, extMsg, NAPI_AUTO_LENGTH, &errorMsg);
193
+ FLOATING_STATUS;
194
+ status = napi_create_error(env, errorCode, errorMsg, &errorValue);
195
+ FLOATING_STATUS;
196
+ status = napi_reject_deferred(env, c->_deferred, errorValue);
197
+ FLOATING_STATUS;
198
+
199
+ // free(extMsg);
200
+ tidyCarrier(env, c);
201
+ }
202
+ return c->status;
203
+ }
204
+
205
+ bool validColorFormat(NDIlib_recv_color_format_e format) {
206
+ switch (format) {
207
+ case NDIlib_recv_color_format_BGRX_BGRA:
208
+ case NDIlib_recv_color_format_UYVY_BGRA:
209
+ case NDIlib_recv_color_format_RGBX_RGBA:
210
+ case NDIlib_recv_color_format_UYVY_RGBA:
211
+ case NDIlib_recv_color_format_fastest:
212
+ #ifdef _WIN32
213
+ case NDIlib_recv_color_format_BGRX_BGRA_flipped:
214
+ #endif
215
+ return true;
216
+ default:
217
+ return false;
218
+ }
219
+ }
220
+
221
+ bool validBandwidth(NDIlib_recv_bandwidth_e bandwidth) {
222
+ switch (bandwidth) {
223
+ case NDIlib_recv_bandwidth_metadata_only:
224
+ case NDIlib_recv_bandwidth_audio_only:
225
+ case NDIlib_recv_bandwidth_lowest:
226
+ case NDIlib_recv_bandwidth_highest:
227
+ return true;
228
+ default:
229
+ return false;
230
+ }
231
+ }
232
+
233
+ bool validFrameFormat(NDIlib_frame_format_type_e format) {
234
+ switch (format) {
235
+ case NDIlib_frame_format_type_progressive:
236
+ case NDIlib_frame_format_type_interleaved:
237
+ case NDIlib_frame_format_type_field_0:
238
+ case NDIlib_frame_format_type_field_1:
239
+ return true;
240
+ default:
241
+ return false;
242
+ }
243
+ }
244
+
245
+ bool validAudioFormat(Grandi_audio_format_e format) {
246
+ switch (format) {
247
+ case Grandi_audio_format_float_32_separate:
248
+ case Grandi_audio_format_int_16_interleaved:
249
+ case Grandi_audio_format_float_32_interleaved:
250
+ return true;
251
+ default:
252
+ return false;
253
+ }
254
+ }
255
+
256
+ // Make a native source object from components of a source object
257
+ napi_status makeNativeSource(napi_env env, napi_value source,
258
+ NDIlib_source_t *result) {
259
+ result->p_ndi_name = nullptr;
260
+ result->p_url_address = nullptr;
261
+
262
+ napi_status status;
263
+ napi_valuetype type;
264
+ napi_value namev, urlv;
265
+ size_t namel, urll;
266
+ char *name = nullptr;
267
+ char *url = nullptr;
268
+
269
+ status = napi_get_named_property(env, source, "name", &namev);
270
+ PASS_STATUS;
271
+ status = napi_get_named_property(env, source, "urlAddress", &urlv);
272
+ PASS_STATUS;
273
+
274
+ status = napi_typeof(env, namev, &type);
275
+ PASS_STATUS;
276
+ if (type == napi_string) {
277
+ status = napi_get_value_string_utf8(env, namev, nullptr, 0, &namel);
278
+ PASS_STATUS;
279
+ name = (char *)malloc(namel + 1);
280
+ if (!name)
281
+ return napi_generic_failure;
282
+ status = napi_get_value_string_utf8(env, namev, name, namel + 1, &namel);
283
+ if (status != napi_ok) {
284
+ free(name);
285
+ return status;
286
+ }
287
+ }
288
+
289
+ status = napi_typeof(env, urlv, &type);
290
+ PASS_STATUS;
291
+ if (type == napi_string) {
292
+ status = napi_get_value_string_utf8(env, urlv, nullptr, 0, &urll);
293
+ PASS_STATUS;
294
+ url = (char *)malloc(urll + 1);
295
+ if (!url) {
296
+ free(name);
297
+ return napi_generic_failure;
298
+ }
299
+ status = napi_get_value_string_utf8(env, urlv, url, urll + 1, &urll);
300
+ if (status != napi_ok) {
301
+ free(name);
302
+ free(url);
303
+ return status;
304
+ }
305
+ }
306
+
307
+ result->p_ndi_name = name;
308
+ result->p_url_address = url;
309
+ return napi_ok;
310
+ }
311
+
312
+ void freeNativeSource(NDIlib_source_t *result) {
313
+ if (result == nullptr)
314
+ return;
315
+ if (result->p_ndi_name != nullptr) {
316
+ free((void *)result->p_ndi_name);
317
+ result->p_ndi_name = nullptr;
318
+ }
319
+ if (result->p_url_address != nullptr) {
320
+ free((void *)result->p_url_address);
321
+ result->p_url_address = nullptr;
322
+ }
323
+ }
@@ -0,0 +1,137 @@
1
+ /* Copyright 2018 Streampunk Media Ltd.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
14
+ */
15
+
16
+ #ifndef GRANDI_UTIL_H
17
+ #define GRANDI_UTIL_H
18
+
19
+ #include <chrono>
20
+ #include <stdio.h>
21
+ #include <string>
22
+ #include <Processing.NDI.Lib.h>
23
+ #include "node_api.h"
24
+
25
+ // The three different formats of raw audio data supported by NDI utility
26
+ // functions
27
+ typedef enum Grandi_audio_format_e {
28
+ // Default NDI audio format
29
+ // Channels stored one after the other in each block - 32-bit floating point
30
+ // values
31
+ Grandi_audio_format_float_32_separate = 0,
32
+ // Alternative NDI audio foramt
33
+ // Channels stored as channel-interleaved 32-bit floating point values
34
+ Grandi_audio_format_float_32_interleaved = 1,
35
+ // Alternative NDI audio format
36
+ // Channels stored as channel-interleaved 16-bit integer values
37
+ Grandi_audio_format_int_16_interleaved = 2
38
+ } Grandi_audio_format_e;
39
+
40
+ #define DECLARE_NAPI_METHOD(name, func) \
41
+ {name, 0, func, 0, 0, 0, napi_default, 0}
42
+
43
+ // Handling NAPI errors - use "napi_status status;" where used
44
+ #define CHECK_STATUS \
45
+ if (checkStatus(env, status, __FILE__, __LINE__ - 1) != napi_ok) \
46
+ return nullptr
47
+ #define PASS_STATUS \
48
+ if (status != napi_ok) \
49
+ return status
50
+
51
+ napi_status checkStatus(napi_env env, napi_status status, const char *file,
52
+ uint32_t line);
53
+
54
+ // High resolution timing
55
+ #define HR_TIME_POINT std::chrono::high_resolution_clock::time_point
56
+ #define NOW std::chrono::high_resolution_clock::now()
57
+ long long microTime(std::chrono::high_resolution_clock::time_point start);
58
+
59
+ // Argument processing
60
+ napi_status checkArgs(napi_env env, napi_callback_info info, char *methodName,
61
+ napi_value *args, size_t argc, napi_valuetype *types);
62
+
63
+ // Async error handling
64
+ #define GRANDI_ERROR_START 4000
65
+ #define GRANDI_INVALID_ARGS 4001
66
+ #define GRANDI_OUT_OF_RANGE 4097
67
+ #define GRANDI_ASYNC_FAILURE 4098
68
+ #define GRANDI_BUILD_ERROR 4099
69
+ #define GRANDI_ALLOCATION_FAILURE 4100
70
+ #define GRANDI_RECEIVE_CREATE_FAIL 4101
71
+ #define GRANDI_SEND_CREATE_FAIL 4102
72
+ #define GRANDI_ROUTING_CREATE_FAIL 4103
73
+ #define GRANDI_FIND_CREATE_FAIL 4104
74
+ #define GRANDI_NOT_FOUND 4040
75
+ #define GRANDI_NOT_VIDEO 4140
76
+ #define GRANDI_NOT_AUDIO 4141
77
+ #define GRANDI_NOT_METADATA 4142
78
+ #define GRANDI_CONNECTION_LOST 4143
79
+ #define GRANDI_SUCCESS 0
80
+
81
+ struct carrier {
82
+ virtual ~carrier() {}
83
+ napi_ref passthru = nullptr;
84
+ int32_t status = GRANDI_SUCCESS;
85
+ std::string errorMsg;
86
+ long long totalTime;
87
+ napi_deferred _deferred;
88
+ napi_async_work _request = nullptr;
89
+ };
90
+
91
+ void tidyCarrier(napi_env env, carrier *c);
92
+ int32_t rejectStatus(napi_env env, carrier *c, const char *file, int32_t line);
93
+
94
+ #define REJECT_STATUS \
95
+ if (rejectStatus(env, c, __FILE__, __LINE__) != GRANDI_SUCCESS) \
96
+ return;
97
+ #define REJECT_RETURN \
98
+ if (rejectStatus(env, c, __FILE__, __LINE__) != GRANDI_SUCCESS) \
99
+ return promise;
100
+ #define FLOATING_STATUS \
101
+ if (status != napi_ok) { \
102
+ printf("Unexpected N-API status not OK in file %s at line %d value %i.\n", \
103
+ __FILE__, __LINE__ - 1, status); \
104
+ }
105
+
106
+ #define NAPI_THROW_ERROR(msg) \
107
+ { \
108
+ char errorMsg[100]; \
109
+ sprintf(errorMsg, msg); \
110
+ napi_throw_error(env, nullptr, errorMsg); \
111
+ return nullptr; \
112
+ }
113
+
114
+ #define REJECT_ERROR(msg, status) \
115
+ { \
116
+ c->errorMsg = msg; \
117
+ c->status = status; \
118
+ REJECT_STATUS; \
119
+ }
120
+
121
+ #define REJECT_ERROR_RETURN(msg, stat) \
122
+ { \
123
+ c->errorMsg = msg; \
124
+ c->status = stat; \
125
+ REJECT_RETURN; \
126
+ }
127
+
128
+ bool validColorFormat(NDIlib_recv_color_format_e format);
129
+ bool validBandwidth(NDIlib_recv_bandwidth_e bandwidth);
130
+ bool validFrameFormat(NDIlib_frame_format_type_e format);
131
+ bool validAudioFormat(Grandi_audio_format_e format);
132
+
133
+ napi_status makeNativeSource(napi_env env, napi_value source,
134
+ NDIlib_source_t *result);
135
+ void freeNativeSource(NDIlib_source_t *source);
136
+
137
+ #endif // GRANDI_UTIL_H
package/package.json ADDED
@@ -0,0 +1,84 @@
1
+ {
2
+ "name": "grandi",
3
+ "version": "0.1.0",
4
+ "description": "Node.JS native bindings to the Newtek NDI SDK.",
5
+ "homepage": "https://github.com/tux-tn/grandi#readme",
6
+ "keywords": [
7
+ "Newtek",
8
+ "NDI",
9
+ "network",
10
+ "device",
11
+ "interface"
12
+ ],
13
+ "author": "Sarhan Aissi <npm@tux.tn>",
14
+ "license": "Apache-2.0",
15
+ "files": [
16
+ "binding.gyp",
17
+ "dist",
18
+ "lib",
19
+ "src",
20
+ "scripts",
21
+ "LICENSE",
22
+ "NOTICE",
23
+ "README.md",
24
+ "prebuilds"
25
+ ],
26
+ "main": "./dist/index.mjs",
27
+ "types": "./dist/index.d.mts",
28
+ "exports": {
29
+ ".": "./dist/index.mjs",
30
+ "./package.json": "./package.json"
31
+ },
32
+ "gypfile": true,
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/tux-tn/grandi.git"
36
+ },
37
+ "bugs": {
38
+ "url": "https://github.com/tux-tn/grandi/issues"
39
+ },
40
+ "dependencies": {
41
+ "cross-spawn": "7.0.6",
42
+ "cross-zip": "4.0.1",
43
+ "execa": "9.6.0",
44
+ "got": "14.6.3",
45
+ "node-gyp-build": "^4.8.4",
46
+ "shelljs": "0.10.0",
47
+ "tmp": "0.2.5"
48
+ },
49
+ "devDependencies": {
50
+ "@biomejs/biome": "2.3.4",
51
+ "@types/bindings": "^1.5.5",
52
+ "@types/cross-zip": "^4.0.2",
53
+ "@types/node": "^24.10.0",
54
+ "@types/shelljs": "^0.8.17",
55
+ "@types/tmp": "^0.2.6",
56
+ "@vitest/coverage-v8": "^4.0.8",
57
+ "cross-env": "^10.1.0",
58
+ "prebuildify": "^6.0.1",
59
+ "shx": "0.4.0",
60
+ "standard-version": "^9.5.0",
61
+ "tsdown": "^0.16.1",
62
+ "typescript": "^5.9.3",
63
+ "vitest": "^4.0.8"
64
+ },
65
+ "scripts": {
66
+ "preinstall": "node scripts/preinstall.mjs",
67
+ "install": "node-gyp-build",
68
+ "prebuild:download": "cross-env NDI_FORCE=1 node scripts/preinstall.mjs",
69
+ "build": "tsdown",
70
+ "prebuild": "prebuildify --napi --strip --target=node@20.19.5",
71
+ "test": "vitest run",
72
+ "test:watch": "vitest",
73
+ "test:unit": "vitest run test/unit",
74
+ "test:integration": "cross-env RUN_NDI_TESTS=1 vitest run test/integration",
75
+ "test:coverage": "vitest run --coverage",
76
+ "format": "biome format",
77
+ "format:cpp": "clang-format -i lib/*.cc lib/*.h",
78
+ "lint": "biome lint",
79
+ "clean": "shx rm -rf ndi build"
80
+ },
81
+ "engines": {
82
+ "node": ">=20.19.5"
83
+ }
84
+ }
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file