Skip to content

Commit d814936

Browse files
committed
Moving first methods over to N-API.
1 parent fc22561 commit d814936

18 files changed

+479
-72
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ node_modules
2828

2929
# Gyp project - ignore build folder
3030
build
31+
32+
.DS_Store

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,6 @@ Contributions can be made via pull requests and will be considered by the author
131131

132132
## License
133133

134-
This software is released under the Apache 2.0 license. Copyright 2017 Streampunk Media Ltd.
134+
This software is released under the Apache 2.0 license. Copyright 2018 Streampunk Media Ltd.
135135

136136
The software links to the BlackMagic Desktop Video libraries. Include files and examples from which this code is derived include the BlackMagic License in their respective header files. The BlackMagic DeckLink SDK can be downloaded from https://www.blackmagicdesign.com/support.

binding.gyp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
],
77
"conditions": [
88
['OS=="mac"', {
9-
'sources' : [ "src/macadam.cc", "src/Capture.cc", "src/Playback.cc" ],
9+
'sources' : [ "src/macadam_util.cc", "src/macadam.cc" ],
1010
'xcode_settings': {
1111
'GCC_ENABLE_CPP_RTTI': 'YES',
1212
'MACOSX_DEPLOYMENT_TARGET': '10.7',
@@ -25,7 +25,7 @@
2525
]
2626
}],
2727
['OS=="linux"', {
28-
'sources' : [ "src/macadam.cc", "src/Capture.cc", "src/Playback.cc" ],
28+
'sources' : [ "src/macadam_util.cc", "src/macadam.cc" ],
2929
'link_settings' : {
3030
"libraries": [
3131
"/usr/lib/libDeckLinkAPI.so"
@@ -39,7 +39,7 @@
3939
]
4040
}],
4141
['OS=="win"', {
42-
"sources" : [ "src/macadam.cc", "src/Capture.cc", "src/Playback.cc",
42+
"sources" : [ "src/macadam_util.cc", "src/macadam.cc",
4343
"decklink/Win/include/DeckLinkAPI_i.c" ],
4444
"configurations": {
4545
"Release": {

index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2017 Streampunk Media Ltd.
1+
/* Copyright 2018 Streampunk Media Ltd.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -46,12 +46,11 @@ var isWinOrMac = (os.platform() === 'win32') || (os.platform() === 'darwin');
4646
// if (!isWinOrMac)
4747
// throw('Macadam is not currently supported on this platform');
4848

49-
var bindings = require('bindings');
50-
var macadamNative = bindings('macadam');
49+
const macadamNative = require('bindings')('macadam');
5150
const util = require('util');
5251
const EventEmitter = require('events');
5352

54-
// var SegfaultHandler = require('../node-segfault-handler');
53+
// var SegfaultHandler = require('node-segfault-handler');
5554
// SegfaultHandler.registerHandler("crash.log");
5655

5756
function Capture (deviceIndex, displayMode, pixelFormat) {

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@
2222
"highland": "^2.11.1",
2323
"nan": "^2.6.2"
2424
},
25+
"devDependencies": {
26+
"segfault-handler": "^1.0.0"
27+
},
2528
"gypfile": true
2629
}

scratch/convertEBU.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2017 Streampunk Media Ltd.
1+
/* Copyright 2018 Streampunk Media Ltd.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

scratch/readEBU.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2017 Streampunk Media Ltd.
1+
/* Copyright 2018 Streampunk Media Ltd.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

scratch/readEBU_safe.js

0 Bytes
Binary file not shown.

scratch/record.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2017 Streampunk Media Ltd.
1+
/* Copyright 2018 Streampunk Media Ltd.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

scratch/sigint_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2017 Streampunk Media Ltd.
1+
/* Copyright 2018 Streampunk Media Ltd.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

src/Capture.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2017 Streampunk Media Ltd.
1+
/* Copyright 2018 Streampunk Media Ltd.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

src/Capture.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2017 Streampunk Media Ltd.
1+
/* Copyright 2018 Streampunk Media Ltd.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

src/Playback.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2017 Streampunk Media Ltd.
1+
/* Copyright 2018 Streampunk Media Ltd.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

src/Playback.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2017 Streampunk Media Ltd.
1+
/* Copyright 2018 Streampunk Media Ltd.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

src/macadam.cc

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2017 Streampunk Media Ltd.
1+
/* Copyright 2018 Streampunk Media Ltd.
22
33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.
@@ -42,11 +42,8 @@
4242

4343
#define _WINSOCKAPI_
4444

45-
#include <node.h>
46-
#include "node_buffer.h"
4745
#include "DeckLinkAPI.h"
4846
#include <stdio.h>
49-
#include <nan.h>
5047

5148
#ifdef WIN32
5249
#include <tchar.h>
@@ -55,25 +52,24 @@
5552
#include <comdef.h>
5653
#endif
5754

58-
#include "Capture.h"
59-
#include "Playback.h"
55+
#include "macadam_util.h"
6056
#include "node_api.h"
6157

62-
using namespace v8;
58+
napi_value deckLinkVersion(napi_env env, napi_callback_info info) {
59+
napi_status status;
60+
napi_value result;
6361

64-
NAN_METHOD(DeckLinkVersion) {
6562
IDeckLinkIterator* deckLinkIterator;
66-
HRESULT result;
63+
HRESULT hresult;
6764
IDeckLinkAPIInformation* deckLinkAPIInformation;
6865
#ifdef WIN32
6966
CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL, IID_IDeckLinkIterator, (void**)&deckLinkIterator);
7067
#else
7168
deckLinkIterator = CreateDeckLinkIteratorInstance();
7269
#endif
73-
result = deckLinkIterator->QueryInterface(IID_IDeckLinkAPIInformation, (void**)&deckLinkAPIInformation);
74-
if (result != S_OK) {
75-
Nan::ThrowError("Error connecting to DeckLinkAPI.");
76-
}
70+
71+
hresult = deckLinkIterator->QueryInterface(IID_IDeckLinkAPIInformation, (void**)&deckLinkAPIInformation);
72+
if (hresult != S_OK) NAPI_THROW_ERROR("Error connecting to DeckLinkAPI.");
7773

7874
char deckVer [80];
7975
int64_t deckLinkVersion;
@@ -89,74 +85,76 @@ NAN_METHOD(DeckLinkVersion) {
8985
sprintf(deckVer, "DeckLinkAPI version: %d.%d.%d", dlVerMajor, dlVerMinor, dlVerPoint);
9086

9187
deckLinkAPIInformation->Release();
88+
deckLinkIterator->Release();
9289

93-
info.GetReturnValue().Set(Nan::New(deckVer).ToLocalChecked());
90+
status = napi_create_string_utf8(env, deckVer, NAPI_AUTO_LENGTH, &result);
91+
CHECK_STATUS;
92+
return result;
9493
}
9594

96-
NAN_METHOD(GetFirstDevice) {
95+
napi_value getFirstDevice(napi_env env, napi_callback_info info) {
96+
napi_status status;
97+
napi_value result;
98+
99+
status = napi_get_undefined(env, &result);
100+
CHECK_STATUS;
101+
97102
IDeckLinkIterator* deckLinkIterator;
98-
HRESULT result;
99-
IDeckLinkAPIInformation *deckLinkAPIInformation;
103+
HRESULT hresult;
100104
IDeckLink* deckLink;
101105
#ifdef WIN32
102106
CoCreateInstance(CLSID_CDeckLinkIterator, NULL, CLSCTX_ALL, IID_IDeckLinkIterator, (void**)&deckLinkIterator);
103107
#else
104108
deckLinkIterator = CreateDeckLinkIteratorInstance();
105109
#endif
106-
result = deckLinkIterator->QueryInterface(IID_IDeckLinkAPIInformation, (void**)&deckLinkAPIInformation);
107-
if (result != S_OK) {
108-
Nan::ThrowError("Error connecting to DeckLinkAPI.");
109-
}
110110
if (deckLinkIterator->Next(&deckLink) != S_OK) {
111-
info.GetReturnValue().SetUndefined();
112-
return;
111+
status = napi_get_undefined(env, &result);
112+
CHECK_STATUS;
113+
deckLinkIterator->Release();
114+
return result;
113115
}
116+
114117
#ifdef WIN32
115118
BSTR deviceNameBSTR = NULL;
116-
result = deckLink->GetModelName(&deviceNameBSTR);
117-
if (result == S_OK) {
119+
hresult = deckLink->GetModelName(&deviceNameBSTR);
120+
if (hresult == S_OK) {
118121
_bstr_t deviceName(deviceNameBSTR, false);
119-
info.GetReturnValue().Set(Nan::New((char*) deviceName).ToLocalChecked());
120-
return;
122+
status = napi_create_string_utf8(env, (char*) deviceName, NAPI_AUTO_LENGTH, &result);
123+
CHECK_STATUS;
121124
}
122125
#elif __APPLE__
123126
CFStringRef deviceNameCFString = NULL;
124-
result = deckLink->GetModelName(&deviceNameCFString);
125-
if (result == S_OK) {
127+
hresult = deckLink->GetModelName(&deviceNameCFString);
128+
if (hresult == S_OK) {
126129
char deviceName [64];
127130
CFStringGetCString(deviceNameCFString, deviceName, sizeof(deviceName), kCFStringEncodingMacRoman);
128-
info.GetReturnValue().Set(Nan::New(deviceName).ToLocalChecked());
129-
return;
131+
status = napi_create_string_utf8(env, deviceName, NAPI_AUTO_LENGTH, &result);
132+
CHECK_STATUS;
133+
}
134+
#else
135+
const char* deviceName;
136+
hresult = deckLink->GetModelName(&deviceName);
137+
if (hresult == S_OK) {
138+
status = napi_create_string_utf8(env, deviceName, NAPI_AUTO_LENGTH, &result);
139+
CHECK_STATUS;
130140
}
131141
#endif
132-
info.GetReturnValue().SetUndefined();
133-
}
134-
135142

143+
deckLink->Release();
144+
deckLinkIterator->Release();
136145

137-
/* static Local<Object> makeBuffer(char* data, size_t size) {
138-
HandleScope scope;
139-
140-
// It ends up being kind of a pain to convert a slow buffer into a fast
141-
// one since the fast part is implemented in JavaScript.
142-
Local<Buffer> slowBuffer = Buffer::New(data, size);
143-
// First get the Buffer from global scope...
144-
Local<Object> global = Context::GetCurrent()->Global();
145-
Local<Value> bv = global->Get(String::NewSymbol("Buffer"));
146-
assert(bv->IsFunction());
147-
Local<Function> b = Local<Function>::Cast(bv);
148-
// ...call Buffer() with the slow buffer and get a fast buffer back...
149-
Handle<Value> argv[3] = { slowBuffer->handle_, Integer::New(size), Integer::New(0) };
150-
Local<Object> fastBuffer = b->NewInstance(3, argv);
146+
return result;
147+
}
151148

152-
return scope.Close(fastBuffer);
153-
} */
149+
napi_value Init(napi_env env, napi_value exports) {
150+
napi_status status;
151+
napi_property_descriptor desc[] = {
152+
DECLARE_NAPI_METHOD("deckLinkVersion", deckLinkVersion),
153+
DECLARE_NAPI_METHOD("getFirstDevice", getFirstDevice),
154+
};
155+
status = napi_define_properties(env, exports, 2, desc);
156+
CHECK_STATUS;
154157

155-
NAN_MODULE_INIT(Init) {
156-
Nan::Export(target, "deckLinkVersion", DeckLinkVersion);
157-
Nan::Export(target, "getFirstDevice", GetFirstDevice);
158-
streampunk::Capture::Init(target);
159-
streampunk::Playback::Init(target);
160158
#ifdef WIN32
161159
HRESULT result;
162160
result = CoInitialize(NULL);
@@ -165,6 +163,8 @@ NAN_MODULE_INIT(Init) {
165163
fprintf(stderr, "Initialization of COM failed - result = %08x.\n", result);
166164
}
167165
#endif
166+
167+
return exports;
168168
}
169169

170-
NODE_MODULE(macadam, Init);
170+
NAPI_MODULE(nodencl, Init)

0 commit comments

Comments
 (0)