Skip to content

Commit 20d96db

Browse files
fix broken node buffers in node 14+ - related to #62
1 parent d7cfacd commit 20d96db

2 files changed

Lines changed: 50 additions & 32 deletions

File tree

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kinect2",
3-
"version": "0.2.3",
3+
"version": "0.2.4",
44
"description": "Nodejs library to access the kinect 2 data from the official MS SDK",
55
"repository": "https://github.com/wouterverweirder/kinect2",
66
"keywords": [
@@ -16,15 +16,18 @@
1616
"main": "lib/kinect2.js",
1717
"dependencies": {
1818
"node-addon-api": "^1.0.0",
19-
"prebuild-install": "^6.1.3"
19+
"prebuild-install": "^6.1.3",
20+
"prebuild": "^10.0.1"
2021
},
2122
"engines": {
2223
"node": ">= 10.16.0"
2324
},
2425
"scripts": {
25-
"install": "prebuild-install -t 4 --runtime napi || prebuild -t 4 --runtime napi || node-gyp rebuild",
26-
"prebuild-install": "prebuild-install -t 4 --runtime napi --verbose",
27-
"prebuild": "prebuild -t 4 --runtime napi",
26+
"test": "npm run clean && npm run prebuild && node test.js",
27+
"install": "prebuild-install -t 4 --runtime napi || prebuild -t 4 --runtime napi",
28+
"prebuild-install": "prebuild-install -t 4 --runtime napi --verbose",
29+
"prebuild": "prebuild -t 4 --runtime napi",
30+
"clean": "rimraf ./lib/binding && rimraf ./build && rimraf ./prebuilds",
2831
"package": "prebuild -t 4 --runtime napi -u",
2932
"start": "cd ./examples/electron && npm start",
3033
"prepublishOnly": "rimraf ./examples/**/node_modules"
@@ -41,7 +44,6 @@
4144
]
4245
},
4346
"devDependencies": {
44-
"rimraf": "^3.0.0",
45-
"prebuild": "^10.0.1"
47+
"rimraf": "^3.0.0"
4648
}
4749
}

src/kinect2.cc

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ IDepthFrameReader* m_pRawDepthFrameReader = NULL;
1616
IBodyFrameReader* m_pBodyFrameReader = NULL;
1717
IMultiSourceFrameReader* m_pMultiSourceFrameReader = NULL;
1818

19+
Napi::ObjectReference m_v8ObjectReference;
20+
1921
RGBQUAD* m_pColorPixels = new RGBQUAD[cColorWidth * cColorHeight];
2022
RGBQUAD* m_pColorPixelsV8 = new RGBQUAD[cColorWidth * cColorHeight];
2123
char* m_pInfraredPixels = new char[cInfraredWidth * cInfraredHeight];
@@ -756,8 +758,7 @@ Napi::Value MethodOpenColorReader(const Napi::CallbackInfo& info) {
756758
return;
757759
}
758760
m_mColorReaderMutex.lock();
759-
Napi::Buffer<RGBQUAD> imageData = Napi::Buffer<RGBQUAD>::New(env, pixelsRef, cColorWidth * cColorHeight);
760-
jsCallback.Call( { imageData } );
761+
jsCallback.Call( { m_v8ObjectReference.Get("colorBuffer") } );
761762
m_mColorReaderMutex.unlock();
762763
};
763764

@@ -859,8 +860,7 @@ Napi::Value MethodOpenInfraredReader(const Napi::CallbackInfo& info) {
859860
return;
860861
}
861862
m_mInfraredReaderMutex.lock();
862-
Napi::Buffer<char> imageData = Napi::Buffer<char>::New(env, pixelsRef, cInfraredWidth * cInfraredHeight);
863-
jsCallback.Call( { imageData } );
863+
jsCallback.Call( { m_v8ObjectReference.Get("infraredBuffer") } );
864864
m_mInfraredReaderMutex.unlock();
865865
};
866866

@@ -962,8 +962,7 @@ Napi::Value MethodOpenLongExposureInfraredReader(const Napi::CallbackInfo& info)
962962
return;
963963
}
964964
m_mLongExposureInfraredReaderMutex.lock();
965-
Napi::Buffer<char> imageData = Napi::Buffer<char>::New(env, pixelsRef, cLongExposureInfraredWidth * cLongExposureInfraredHeight);
966-
jsCallback.Call( { imageData } );
965+
jsCallback.Call( { m_v8ObjectReference.Get("longExposureInfraredBuffer") } );
967966
m_mLongExposureInfraredReaderMutex.unlock();
968967
};
969968

@@ -1065,8 +1064,7 @@ Napi::Value MethodOpenDepthReader(const Napi::CallbackInfo& info) {
10651064
return;
10661065
}
10671066
m_mDepthReaderMutex.lock();
1068-
Napi::Buffer<char> imageData = Napi::Buffer<char>::New(env, pixelsRef, cDepthWidth * cDepthHeight);
1069-
jsCallback.Call( { imageData } );
1067+
jsCallback.Call( { m_v8ObjectReference.Get("depthPixelsBuffer") } );
10701068
m_mDepthReaderMutex.unlock();
10711069
};
10721070

@@ -1168,8 +1166,7 @@ Napi::Value MethodOpenRawDepthReader(const Napi::CallbackInfo& info) {
11681166
return;
11691167
}
11701168
m_mRawDepthReaderMutex.lock();
1171-
Napi::Buffer<UINT16> imageData = Napi::Buffer<UINT16>::New(env, pixelsRef, cDepthWidth * cDepthHeight);
1172-
jsCallback.Call( { imageData } );
1169+
jsCallback.Call( { m_v8ObjectReference.Get("rawDepthValuesBuffer") } );
11731170
m_mRawDepthReaderMutex.unlock();
11741171
};
11751172

@@ -1444,10 +1441,8 @@ Napi::Value MethodOpenMultiSourceReader(const Napi::CallbackInfo& info) {
14441441

14451442
if(NodeKinect2FrameTypes::FrameTypes_Color & m_enabledFrameTypes)
14461443
{
1447-
Napi::Buffer<RGBQUAD> v8ColorPixels = Napi::Buffer<RGBQUAD>::New(env, m_pColorPixelsV8, cColorWidth * cColorHeight);
1448-
14491444
Napi::Object v8ColorResult = Napi::Object::New(env);
1450-
v8ColorResult.Set(Napi::String::New(env, "buffer"), v8ColorPixels);
1445+
v8ColorResult.Set(Napi::String::New(env, "buffer"), m_v8ObjectReference.Get("colorBuffer"));
14511446

14521447
//field of view
14531448
v8ColorResult.Set(Napi::String::New(env, "horizontalFieldOfView"), Napi::Number::New(env, m_fColorHorizontalFieldOfViewV8));
@@ -1459,11 +1454,8 @@ Napi::Value MethodOpenMultiSourceReader(const Napi::CallbackInfo& info) {
14591454

14601455
if(NodeKinect2FrameTypes::FrameTypes_Depth & m_enabledFrameTypes)
14611456
{
1462-
//reuse the existing buffer
1463-
Napi::Buffer<char> v8DepthPixels = Napi::Buffer<char>::New(env, m_pDepthPixelsV8, cDepthWidth * cDepthHeight);
1464-
14651457
Napi::Object v8DepthResult = Napi::Object::New(env);
1466-
v8DepthResult.Set(Napi::String::New(env, "buffer"), v8DepthPixels);
1458+
v8DepthResult.Set(Napi::String::New(env, "buffer"), m_v8ObjectReference.Get("depthPixelsBuffer"));
14671459

14681460
//field of view
14691461
v8DepthResult.Set(Napi::String::New(env, "horizontalFieldOfView"), Napi::Number::New(env, m_fDepthHorizontalFieldOfViewV8));
@@ -1475,10 +1467,8 @@ Napi::Value MethodOpenMultiSourceReader(const Napi::CallbackInfo& info) {
14751467

14761468
if(NodeKinect2FrameTypes::FrameTypes_RawDepth & m_enabledFrameTypes)
14771469
{
1478-
Napi::Buffer<UINT16> v8RawDepthValues = Napi::Buffer<UINT16>::New(env, m_pRawDepthValuesV8, cDepthWidth * cDepthHeight);
1479-
14801470
Napi::Object v8RawDepthResult = Napi::Object::New(env);
1481-
v8RawDepthResult.Set(Napi::String::New(env, "buffer"), v8RawDepthValues);
1471+
v8RawDepthResult.Set(Napi::String::New(env, "buffer"), m_v8ObjectReference.Get("rawDepthBuffer"));
14821472

14831473
//field of view
14841474
v8RawDepthResult.Set(Napi::String::New(env, "horizontalFieldOfView"), Napi::Number::New(env, m_fDepthHorizontalFieldOfViewV8));
@@ -1490,10 +1480,8 @@ Napi::Value MethodOpenMultiSourceReader(const Napi::CallbackInfo& info) {
14901480

14911481
if(NodeKinect2FrameTypes::FrameTypes_DepthColor & m_enabledFrameTypes)
14921482
{
1493-
Napi::Buffer<char> v8DepthColorPixels = Napi::Buffer<char>::New(env, (char *)m_pDepthColorPixelsV8, cDepthWidth * cDepthHeight * sizeof(RGBQUAD));
1494-
14951483
Napi::Object v8DepthColorResult = Napi::Object::New(env);
1496-
v8DepthColorResult.Set(Napi::String::New(env, "buffer"), v8DepthColorPixels);
1484+
v8DepthColorResult.Set(Napi::String::New(env, "buffer"), m_v8ObjectReference.Get("depthColorPixelsBuffer"));
14971485

14981486
v8Result.Set(Napi::String::New(env, "depthColor"), v8DepthColorResult);
14991487
}
@@ -1510,13 +1498,13 @@ Napi::Value MethodOpenMultiSourceReader(const Napi::CallbackInfo& info) {
15101498
Napi::Object v8BodyIndexColorResult = Napi::Object::New(env);
15111499

15121500
Napi::Array v8bodies = Napi::Array::New(env, BODY_COUNT);
1501+
Napi::Array bodyIndexColorPixelsBuffers = m_v8ObjectReference.Get("bodyIndexColorPixelsBuffers").As<Napi::Array>();
15131502
for(int i = 0; i < BODY_COUNT; i++)
15141503
{
15151504
Napi::Object v8body = Napi::Object::New(env);
15161505
v8body.Set(Napi::String::New(env, "bodyIndex"), Napi::Number::New(env, i));
15171506
if(m_jsBodyFrameV8.bodies[i].hasPixels && m_trackPixelsForBodyIndexV8[i]) {
1518-
Napi::Buffer<RGBQUAD> v8ColorPixels = Napi::Buffer<RGBQUAD>::New(env, m_pBodyIndexColorPixelsV8[i], cColorWidth * cColorHeight);
1519-
v8body.Set(Napi::String::New(env, "buffer"), v8ColorPixels);
1507+
v8body.Set(Napi::String::New(env, "buffer"), bodyIndexColorPixelsBuffers.Get(i));
15201508
}
15211509
v8bodies.Set(i, v8body);
15221510
}
@@ -1890,6 +1878,34 @@ Napi::Value MethodTrackPixelsForBodyIndices(const Napi::CallbackInfo& info) {
18901878
Napi::Object Init(Napi::Env env, Napi::Object exports) {
18911879
printf("[kinect2.cc] Init\n");
18921880

1881+
m_v8ObjectReference = Napi::Reference<Napi::Object>::New(Napi::Object::New(env), 1);
1882+
1883+
Napi::Buffer<RGBQUAD> colorBuffer = Napi::Buffer<RGBQUAD>::New(env, m_pColorPixelsV8, cColorWidth * cColorHeight);
1884+
m_v8ObjectReference.Set("colorBuffer", colorBuffer);
1885+
1886+
Napi::Buffer<char> infraredBuffer = Napi::Buffer<char>::New(env, m_pInfraredPixelsV8, cInfraredWidth * cInfraredHeight);
1887+
m_v8ObjectReference.Set("infraredBuffer", infraredBuffer);
1888+
1889+
Napi::Buffer<char> longExposureInfraredBuffer = Napi::Buffer<char>::New(env, m_pLongExposureInfraredPixelsV8, cLongExposureInfraredWidth * cLongExposureInfraredHeight);
1890+
m_v8ObjectReference.Set("longExposureInfraredBuffer", longExposureInfraredBuffer);
1891+
1892+
Napi::Buffer<char> depthPixelsBuffer = Napi::Buffer<char>::New(env, m_pDepthPixelsV8, cDepthWidth * cDepthHeight);
1893+
m_v8ObjectReference.Set("depthPixelsBuffer", depthPixelsBuffer);
1894+
1895+
Napi::Buffer<UINT16> rawDepthValuesBuffer = Napi::Buffer<UINT16>::New(env, m_pRawDepthValuesV8, cDepthWidth * cDepthHeight);
1896+
m_v8ObjectReference.Set("rawDepthValuesBuffer", rawDepthValuesBuffer);
1897+
1898+
Napi::Buffer<char> depthColorPixelsBuffer = Napi::Buffer<char>::New(env, (char *)m_pDepthColorPixelsV8, cDepthWidth * cDepthHeight * sizeof(RGBQUAD));
1899+
m_v8ObjectReference.Set("depthColorPixelsBuffer", depthColorPixelsBuffer);
1900+
1901+
Napi::Array bodyIndexColorPixelsBuffers = Napi::Array::New(env, BODY_COUNT);
1902+
for(int i = 0; i < BODY_COUNT; i++)
1903+
{
1904+
Napi::Buffer<RGBQUAD> bodyIndexColorPixelsBuffer = Napi::Buffer<RGBQUAD>::New(env, m_pBodyIndexColorPixelsV8[i], cColorWidth * cColorHeight);
1905+
bodyIndexColorPixelsBuffers.Set(i, bodyIndexColorPixelsBuffer);
1906+
}
1907+
m_v8ObjectReference.Set(Napi::String::New(env, "bodyIndexColorPixelsBuffers"), bodyIndexColorPixelsBuffers);
1908+
18931909
exports.Set(Napi::String::New(env, "open"), Napi::Function::New(env, MethodOpen));
18941910
exports.Set(Napi::String::New(env, "close"), Napi::Function::New(env, MethodClose));
18951911

0 commit comments

Comments
 (0)