Skip to content

Call Emit inside a c++ Lambda fucnction #887

@shadow111

Description

@shadow111

Hello This actually not an issue but i'm seeking some recommendation
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

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

But i just get

Start Recording …

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions