@stagetimerio/grandiose 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,47 @@
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 GRANDIOSE_SEND_H
17
+ #define GRANDIOSE_SEND_H
18
+
19
+ #include "node_api.h"
20
+ #include "grandiose_util.h"
21
+
22
+ napi_value send(napi_env env, napi_callback_info info);
23
+
24
+ struct sendCarrier : carrier {
25
+ char* name = nullptr;
26
+ char* groups = nullptr;
27
+ bool clockVideo = false;
28
+ bool clockAudio = false;
29
+ NDIlib_send_instance_t send;
30
+ ~sendCarrier() {
31
+ free(name);
32
+ }
33
+ };
34
+
35
+ struct sendDataCarrier : carrier {
36
+ NDIlib_send_instance_t send;
37
+ NDIlib_video_frame_v2_t videoFrame;
38
+ NDIlib_audio_frame_v3_t audioFrame;
39
+ NDIlib_metadata_frame_t metadataFrame;
40
+ napi_ref sourceBufferRef = nullptr;
41
+ ~sendDataCarrier() {
42
+ // TODO: free sourceBufferRef
43
+ }
44
+ };
45
+
46
+
47
+ #endif /* GRANDIOSE_SEND_H */
@@ -0,0 +1,282 @@
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 "grandiose_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
+ {
30
+ int i = 0;
31
+ bool isNegative = false;
32
+
33
+ /* Handle 0 explicitely, otherwise empty string is printed for 0 */
34
+ if (num == 0)
35
+ {
36
+ str[i++] = '0';
37
+ str[i] = '\0';
38
+ return str;
39
+ }
40
+
41
+ // In standard itoa(), negative numbers are handled only with
42
+ // base 10. Otherwise numbers are considered unsigned.
43
+ if (num < 0 && base == 10)
44
+ {
45
+ isNegative = true;
46
+ num = -num;
47
+ }
48
+
49
+ // Process individual digits
50
+ while (num != 0)
51
+ {
52
+ int rem = num % base;
53
+ str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
54
+ num = num/base;
55
+ }
56
+
57
+ // If number is negative, append '-'
58
+ if (isNegative)
59
+ str[i++] = '-';
60
+
61
+ str[i] = '\0'; // Append string terminator
62
+
63
+ // Reverse the string
64
+ std::reverse(std::string(str).begin(), std::string(str).end());
65
+
66
+ return str;
67
+ }
68
+
69
+ napi_status checkStatus(napi_env env, napi_status status,
70
+ const char* file, uint32_t line) {
71
+
72
+ napi_status infoStatus, throwStatus;
73
+ const napi_extended_error_info *errorInfo;
74
+
75
+ if (status == napi_ok) {
76
+ return status;
77
+ }
78
+
79
+ infoStatus = napi_get_last_error_info(env, &errorInfo);
80
+ assert(infoStatus == napi_ok);
81
+ printf("NAPI error in file %s on line %i. Error %i: %s\n", file, line,
82
+ errorInfo->error_code, errorInfo->error_message);
83
+
84
+ if (status == napi_pending_exception) {
85
+ printf("NAPI pending exception. Engine error code: %i\n", errorInfo->engine_error_code);
86
+ return status;
87
+ }
88
+
89
+ char errorCode[20];
90
+ throwStatus = napi_throw_error(env,
91
+ custom_itoa(errorInfo->error_code, errorCode, 10), errorInfo->error_message);
92
+ assert(throwStatus == napi_ok);
93
+
94
+ return napi_pending_exception; // Expect to be cast to void
95
+ }
96
+
97
+ long long microTime(std::chrono::high_resolution_clock::time_point start) {
98
+ auto elapsed = std::chrono::high_resolution_clock::now() - start;
99
+ return std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
100
+ }
101
+
102
+ const char* getNapiTypeName(napi_valuetype t) {
103
+ switch (t) {
104
+ case napi_undefined: return "undefined";
105
+ case napi_null: return "null";
106
+ case napi_boolean: return "boolean";
107
+ case napi_number: return "number";
108
+ case napi_string: return "string";
109
+ case napi_symbol: return "symbol";
110
+ case napi_object: return "object";
111
+ case napi_function: return "function";
112
+ case napi_external: return "external";
113
+ default: return "unknown";
114
+ }
115
+ }
116
+
117
+ napi_status checkArgs(napi_env env, napi_callback_info info, char* methodName,
118
+ napi_value* args, size_t argc, napi_valuetype* types) {
119
+
120
+ napi_status status;
121
+
122
+ size_t realArgc = argc;
123
+ status = napi_get_cb_info(env, info, &realArgc, args, nullptr, nullptr);
124
+ if (status != napi_ok) return status;
125
+
126
+ if (realArgc != argc) {
127
+ char errorMsg[100];
128
+ snprintf(errorMsg, sizeof(errorMsg), "For method %s, expected %zi arguments and got %zi.",
129
+ methodName, argc, realArgc);
130
+ napi_throw_error(env, nullptr, errorMsg);
131
+ return napi_pending_exception;
132
+ }
133
+
134
+ napi_valuetype t;
135
+ for ( int x = 0 ; x < (int)argc ; x++ ) {
136
+ status = napi_typeof(env, args[x], &t);
137
+ if (status != napi_ok) return status;
138
+ if (t != types[x]) {
139
+ char errorMsg[100];
140
+ snprintf(errorMsg, sizeof(errorMsg), "For method %s argument %i, expected type %s and got %s.",
141
+ methodName, x + 1, getNapiTypeName(types[x]), getNapiTypeName(t));
142
+ napi_throw_error(env, nullptr, errorMsg);
143
+ return napi_pending_exception;
144
+ }
145
+ }
146
+
147
+ return napi_ok;
148
+ };
149
+
150
+
151
+ void tidyCarrier(napi_env env, carrier* c) {
152
+ napi_status status;
153
+ if (c->passthru != nullptr) {
154
+ status = napi_delete_reference(env, c->passthru);
155
+ FLOATING_STATUS;
156
+ }
157
+ if (c->_request != nullptr) {
158
+ status = napi_delete_async_work(env, c->_request);
159
+ FLOATING_STATUS;
160
+ }
161
+ delete c;
162
+ }
163
+
164
+ int32_t rejectStatus(napi_env env, carrier* c, const char* file, int32_t line) {
165
+ if (c->status != GRANDIOSE_SUCCESS) {
166
+ napi_value errorValue, errorCode, errorMsg;
167
+ napi_status status;
168
+ char errorChars[20];
169
+ if (c->status < GRANDIOSE_ERROR_START) {
170
+ const napi_extended_error_info *errorInfo;
171
+ status = napi_get_last_error_info(env, &errorInfo);
172
+ FLOATING_STATUS;
173
+ c->errorMsg = std::string(errorInfo->error_message);
174
+ }
175
+ char* extMsg = (char *) malloc(sizeof(char) * c->errorMsg.length() + 200);
176
+ snprintf(extMsg, sizeof(char) * c->errorMsg.length() + 200, "In file %s on line %i, found error: %s", file, line, c->errorMsg.c_str());
177
+ status = napi_create_string_utf8(env, custom_itoa(c->status, errorChars, 10),
178
+ NAPI_AUTO_LENGTH, &errorCode);
179
+ FLOATING_STATUS;
180
+ status = napi_create_string_utf8(env, extMsg, NAPI_AUTO_LENGTH, &errorMsg);
181
+ FLOATING_STATUS;
182
+ status = napi_create_error(env, errorCode, errorMsg, &errorValue);
183
+ FLOATING_STATUS;
184
+ status = napi_reject_deferred(env, c->_deferred, errorValue);
185
+ FLOATING_STATUS;
186
+
187
+ //free(extMsg);
188
+ tidyCarrier(env, c);
189
+ }
190
+ return c->status;
191
+ }
192
+
193
+ bool validColorFormat(NDIlib_recv_color_format_e format) {
194
+ switch (format) {
195
+ case NDIlib_recv_color_format_BGRX_BGRA:
196
+ case NDIlib_recv_color_format_UYVY_BGRA:
197
+ case NDIlib_recv_color_format_RGBX_RGBA:
198
+ case NDIlib_recv_color_format_UYVY_RGBA:
199
+ case NDIlib_recv_color_format_fastest:
200
+ #ifdef _WIN32
201
+ case NDIlib_recv_color_format_BGRX_BGRA_flipped:
202
+ #endif
203
+ return true;
204
+ default:
205
+ return false;
206
+ }
207
+ }
208
+
209
+ bool validBandwidth(NDIlib_recv_bandwidth_e bandwidth) {
210
+ switch (bandwidth) {
211
+ case NDIlib_recv_bandwidth_metadata_only:
212
+ case NDIlib_recv_bandwidth_audio_only:
213
+ case NDIlib_recv_bandwidth_lowest:
214
+ case NDIlib_recv_bandwidth_highest:
215
+ return true;
216
+ default:
217
+ return false;
218
+ }
219
+ }
220
+
221
+ bool validFrameFormat(NDIlib_frame_format_type_e format) {
222
+ switch (format) {
223
+ case NDIlib_frame_format_type_progressive:
224
+ case NDIlib_frame_format_type_interleaved:
225
+ case NDIlib_frame_format_type_field_0:
226
+ case NDIlib_frame_format_type_field_1:
227
+ return true;
228
+ default:
229
+ return false;
230
+ }
231
+ }
232
+
233
+ bool validAudioFormat(Grandiose_audio_format_e format) {
234
+ switch (format) {
235
+ case Grandiose_audio_format_float_32_separate:
236
+ case Grandiose_audio_format_int_16_interleaved:
237
+ case Grandiose_audio_format_float_32_interleaved:
238
+ return true;
239
+ default:
240
+ return false;
241
+ }
242
+ }
243
+
244
+ // Make a native source object from components of a source object
245
+ napi_status makeNativeSource(napi_env env, napi_value source, NDIlib_source_t *result) {
246
+ const char* name = nullptr;
247
+ const char* url = nullptr;
248
+ napi_status status;
249
+ napi_valuetype type;
250
+ napi_value namev, urlv;
251
+ size_t namel, urll;
252
+
253
+ status = napi_get_named_property(env, source, "name", &namev);
254
+ PASS_STATUS;
255
+ status = napi_get_named_property(env, source, "urlAddress", &urlv);
256
+ PASS_STATUS;
257
+
258
+ status = napi_typeof(env, namev, &type);
259
+ PASS_STATUS;
260
+ if (type == napi_string) {
261
+ status = napi_get_value_string_utf8(env, namev, nullptr, 0, &namel);
262
+ PASS_STATUS;
263
+ name = (char *) malloc(namel + 1);
264
+ status = napi_get_value_string_utf8(env, namev, (char*) name, namel + 1, &namel);
265
+ PASS_STATUS;
266
+ }
267
+
268
+ status = napi_typeof(env, urlv, &type);
269
+ PASS_STATUS;
270
+ if (type == napi_string) {
271
+ status = napi_get_value_string_utf8(env, urlv, nullptr, 0, &urll);
272
+ PASS_STATUS;
273
+ url = (char *) malloc(urll + 1);
274
+ status = napi_get_value_string_utf8(env, urlv, (char*) url, urll + 1, &urll);
275
+ PASS_STATUS;
276
+ }
277
+
278
+ result->p_ndi_name = name;
279
+ result->p_url_address = url;
280
+ return napi_ok;
281
+ }
282
+
@@ -0,0 +1,120 @@
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 GRANDIOSE_UTIL_H
17
+ #define GRANDIOSE_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 functions
26
+ typedef enum Grandiose_audio_format_e {
27
+ // Default NDI audio format
28
+ // Channels stored one after the other in each block - 32-bit floating point values
29
+ Grandiose_audio_format_float_32_separate = 0,
30
+ // Alternative NDI audio foramt
31
+ // Channels stored as channel-interleaved 32-bit floating point values
32
+ Grandiose_audio_format_float_32_interleaved = 1,
33
+ // Alternative NDI audio format
34
+ // Channels stored as channel-interleaved 16-bit integer values
35
+ Grandiose_audio_format_int_16_interleaved = 2
36
+ } Grandiose_audio_format_e;
37
+
38
+ #define DECLARE_NAPI_METHOD(name, func) { name, 0, func, 0, 0, 0, napi_default, 0 }
39
+
40
+ // Handling NAPI errors - use "napi_status status;" where used
41
+ #define CHECK_STATUS if (checkStatus(env, status, __FILE__, __LINE__ - 1) != napi_ok) return nullptr
42
+ #define PASS_STATUS if (status != napi_ok) return status
43
+
44
+ napi_status checkStatus(napi_env env, napi_status status,
45
+ const char * file, uint32_t line);
46
+
47
+ // High resolution timing
48
+ #define HR_TIME_POINT std::chrono::high_resolution_clock::time_point
49
+ #define NOW std::chrono::high_resolution_clock::now()
50
+ long long microTime(std::chrono::high_resolution_clock::time_point start);
51
+
52
+ // Argument processing
53
+ napi_status checkArgs(napi_env env, napi_callback_info info, char* methodName,
54
+ napi_value* args, size_t argc, napi_valuetype* types);
55
+
56
+ // Async error handling
57
+ #define GRANDIOSE_ERROR_START 4000
58
+ #define GRANDIOSE_INVALID_ARGS 4001
59
+ #define GRANDIOSE_OUT_OF_RANGE 4097
60
+ #define GRANDIOSE_ASYNC_FAILURE 4098
61
+ #define GRANDIOSE_BUILD_ERROR 4099
62
+ #define GRANDIOSE_ALLOCATION_FAILURE 4100
63
+ #define GRANDIOSE_RECEIVE_CREATE_FAIL 4101
64
+ #define GRANDIOSE_SEND_CREATE_FAIL 4102
65
+ #define GRANDIOSE_ROUTING_CREATE_FAIL 4103
66
+ #define GRANDIOSE_FIND_CREATE_FAIL 4104
67
+ #define GRANDIOSE_NOT_FOUND 4040
68
+ #define GRANDIOSE_NOT_VIDEO 4140
69
+ #define GRANDIOSE_NOT_AUDIO 4141
70
+ #define GRANDIOSE_NOT_METADATA 4142
71
+ #define GRANDIOSE_CONNECTION_LOST 4143
72
+ #define GRANDIOSE_SUCCESS 0
73
+
74
+ struct carrier {
75
+ virtual ~carrier() {}
76
+ napi_ref passthru = nullptr;
77
+ int32_t status = GRANDIOSE_SUCCESS;
78
+ std::string errorMsg;
79
+ long long totalTime;
80
+ napi_deferred _deferred;
81
+ napi_async_work _request = nullptr;
82
+ };
83
+
84
+ void tidyCarrier(napi_env env, carrier* c);
85
+ int32_t rejectStatus(napi_env env, carrier* c, const char* file, int32_t line);
86
+
87
+ #define REJECT_STATUS if (rejectStatus(env, c, __FILE__, __LINE__) != GRANDIOSE_SUCCESS) return;
88
+ #define REJECT_RETURN if (rejectStatus(env, c, __FILE__, __LINE__) != GRANDIOSE_SUCCESS) return promise;
89
+ #define FLOATING_STATUS if (status != napi_ok) { \
90
+ printf("Unexpected N-API status not OK in file %s at line %d value %i.\n", \
91
+ __FILE__, __LINE__ - 1, status); \
92
+ }
93
+
94
+ #define NAPI_THROW_ERROR(msg) { \
95
+ char errorMsg[100]; \
96
+ snprintf(errorMsg, sizeof(errorMsg), msg); \
97
+ napi_throw_error(env, nullptr, errorMsg); \
98
+ return nullptr; \
99
+ }
100
+
101
+ #define REJECT_ERROR(msg, status) { \
102
+ c->errorMsg = msg; \
103
+ c->status = status; \
104
+ REJECT_STATUS; \
105
+ }
106
+
107
+ #define REJECT_ERROR_RETURN(msg, stat) { \
108
+ c->errorMsg = msg; \
109
+ c->status = stat; \
110
+ REJECT_RETURN; \
111
+ }
112
+
113
+ bool validColorFormat(NDIlib_recv_color_format_e format);
114
+ bool validBandwidth(NDIlib_recv_bandwidth_e bandwidth);
115
+ bool validFrameFormat(NDIlib_frame_format_type_e format);
116
+ bool validAudioFormat(Grandiose_audio_format_e format);
117
+
118
+ napi_status makeNativeSource(napi_env env, napi_value source, NDIlib_source_t *result);
119
+
120
+ #endif // GRANDIOSE_UTIL_H