[英]N-API Continuous callbacks for c++ media stream
I'm trying to create a node interface for a c++ media player.我正在尝试为 C++ 媒体播放器创建一个节点接口。 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.
对帧进行解码后,有一个事件允许我访问帧数据,我正试图将其汇集到 node.js 中。 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 .
但我似乎无法弄清楚如何让这种功能与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.
目前,我的方法是找出一种将数据从 c++ 获取到 javascript 的推送机制,其中我需要的只是在 javascript 中初始化一个回调,因为它看起来更优雅。 If that fails I could create a polling loop in js to check if there is new frame data, but it seems less efficient.
如果失败,我可以在 js 中创建一个轮询循环来检查是否有新的帧数据,但它似乎效率较低。
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 :我已经尝试过napi_create_async_work ,通过在执行参数函数中创建一个 lambda 函数,这将允许我为每个帧回调调用napi_make_callback ,但随后出现以下错误:
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.我可能错误地处理了这个问题,这是我第一次使用 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.该问题主要与您无法访问事件循环主线程之外的 V8 (JavaScript) 内存有关。 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 )幸运的是,正在进行修复,应该允许使用napi_create_threadsafe_function进行线程安全访问(示例在这里)
Until then There is a header only C++ package which integrates great with the C++ N-API wrapper在此之前,只有头文件的 C++ 包与C++ N-API 包装器完美集成
Napi-addon-api is update. Napi-addon-api 正在更新。 These is a good way that use the Napi::ThreadSafeFunction.
这是使用 Napi::ThreadSafeFunction 的好方法。 Doc and example.
文档和示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.