Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

N-API Continuous callbacks for c++ media stream

Tags:

c++

node.js

I'm trying to create a node interface for a c++ media player. Upon decoding of a frame, there is an event which allows me to access the frame data, which I'm trying to funnel into node. But I can't seem to figure out how to get that kind of functionality to work with the functions available in the node api. My approach, for the time being, is to figure out a push mechanism to get the data from c++ to javascript where all i need is to initialize a callback in javascript, since it seems more elegant. If that fails I could create a polling loop in js to check if there is new frame data, but it seems less efficient.

I've tried with napi_create_async_work, by creating a lambda function in the execute parameter function, which would allow me to call napi_make_callback for every frame callback, but then I get the following error :

Fatal error in HandleScope::HandleScope
Entering the V8 API without proper locking in place

I'm likely approaching this incorrectly, its the first time I use n-api.

Any help is welcome, thank you!

like image 584
Victor.dMdB Avatar asked Mar 20 '26 21:03

Victor.dMdB


2 Answers

The issue is mainly pertaining to the fact you can’t access V8 (JavaScript) memory outside the event-loop’s main thread. If you're creating an async thread, by default you're also creating a new memory stack.

Fortunately, a fix is on the way which should allow thread safe access with napi_create_threadsafe_function (example here)

Until then There is a header only C++ package which integrates great with the C++ N-API wrapper

like image 118
Victor.dMdB Avatar answered Mar 23 '26 13:03

Victor.dMdB


Napi-addon-api is update. These is a good way that use the Napi::ThreadSafeFunction. Doc and example.

like image 28
spmno Avatar answered Mar 23 '26 13:03

spmno