Fabiano Taioli
Fabiano Taioli

Reputation: 5540

Calling callback inside callback in native Node

I'm a node & C++ newbie so be clement.

I'm writing a native node addon.

My addon start a webcam streaming (using UVC lib) and I want every frame to be available to node.

My CC addon do something like

uvc_start_streaming(devh, &ctrl, frameProcess, (void *) &args, 0)

Where:

The c++ callback is called every new frame and I want to simple print something like "new frame received" so my C++ is like:

void frameProcess(uvc_frame_t *frame, void *ptr) {
  const FunctionCallbackInfo<Value> args = *((const FunctionCallbackInfo<Value>*)(ptr));
  Isolate* isolate = args.GetIsolate();

  Local<Function> cb = Local<Function>::Cast(args[0]);  
  const unsigned argc = 1;
  Local<Value> argv[argc] = { String::NewFromUtf8(isolate, "new frame received") };
  cb->Call(Null(isolate), argc, argv);

}

void testStreaming (const FunctionCallbackInfo<Value>& args) {
    ...
    res = uvc_start_streaming(devh, &ctrl, frameProcess, (void *) &args, 0);
    puts("Streaming...");
    Sleep(10000); /* stream for 10 seconds */
    uvc_stop_streaming(devh);
    puts("Done streaming.");
    ...
}

...
NODE_SET_METHOD(exports, "testStreaming", testDevice);

My js is something like:

'use strict';
var uvc = require('../build/Release/binding')
uvc.testStreaming(
  function (x) {
    console.log(x)
  }
)

The problem is that node exit without any message or error when program reach cb->Call.

If I comment cb->Call row the program run for 10 seconds (continuosly calling the ) as programmed and then exit.

But if I uncomment cb->Call the program exit immediatly.

Upvotes: 0

Views: 120

Answers (1)

pmed
pmed

Reputation: 1556

Your frameProcess() function should call v8::Function callback in the Node.js thread, see https://stackoverflow.com/a/28116160/1355844

Upvotes: 1

Related Questions