Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

javascript - NAPI Call Emit inside a c++ Lambda fucnction

I'm working on a N-API addon to capture video frame using windows graphic capture API , extract frame bytes and send it back to JavaScript. I have tried the event emitter but I can't get the data.

Here is my C++ code:

#include <napi.h>
// my other include

Napi::Value startCapture(const Napi::CallbackInfo &info){
    Napi::Env env = info.Env();
    Napi::Function emit = info[0].As<Napi::Function>();
    emit.Call({Napi::String::New(env, "start")});
    auto framePool = winrt::Direct3D11CaptureFramePool::Create(
    device, //d3d device
    winrt::DirectXPixelFormat::B8G8R8A8UIntNormalized,
    2,
    itemSize);
    // capture session
    auto session = framePool.CreateCaptureSession(item);

    // Lambda Function 
    framePool.FrameArrived([session, d3dDevice, d3dContext, &emit, &env](auto &framePool, auto &) {
        auto frame = framePool.TryGetNextFrame();
        auto frameTexture = GetDXGIInterfaceFromObject<ID3D11Texture2D>(frame.Surface());
        // Extraction the byte and so on ...
        emit.Call({Napi::String::New(env, "data"), Napi::String::New(env, "data ...")});
    }
    
    session.StartCapture();
    emit.Call({Napi::String::New(env, "end")});
    return Napi::String::New(env, "OK");
}

here my JavaScript code calling the start capture function

<!-- language-all: js -->
const EventEmitter = require('events').EventEmitter
const addon = require('./build/myaddon.node')
const emitter = new EventEmitter()

emitter.on('start', () => {
    console.log('Start Recording ...')
})
emitter.on('data', (evt) => {
    console.log(evt);
})

emitter.on('end', () => {
    console.log('Stop Recording ...')
})

addon.startCapture(emitter.emit.bind(emitter))

Normally my output should be an infinite loop of data messages until I stop it

Start Recording … data data . . . data

After looking to the lambda function framePool.FrameArrived it seems that it's running on a different thread than the startCapture function if I understand the lambda function concept correctly, I just wanna to found a way on how I can stream those messages to JavaScript using event Emitter or any other recommendation is well welcoming.

question from:https://stackoverflow.com/questions/65853118/napi-call-emit-inside-a-c-lambda-fucnction

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...