简体   繁体   中英

N-API Continuous callbacks for c++ media stream

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!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM