Streampunk / grandiose

Node.JS native bindings to Newtek NDI(tm).
Apache License 2.0
184 stars 47 forks source link

Unable to make this work in a worker thread #27

Open grrrwaaa opened 1 year ago

grrrwaaa commented 1 year ago

I have a basic test of the library working, it's great.

I wanted to move the receiver to a worker thread to free up resources on the main thread, but when I do I either get "No data received" timeouts on the receiver.video() call. The exact same code works fine and I receive streams when in the main thread.

Here's the code for "ndi_worker.js"

const { workerData, parentPort } = require('worker_threads')

const grandiose = require('grandiose');
console.log(grandiose.version(), grandiose.isSupportedCPU())

async function run() {
    let sources = await grandiose.find()
    console.log(sources);
    if (!sources.length) return

    let receiver = await grandiose.receive({ source: sources[0], });
    console.log(receiver)

    let timeout = 1000; // Optional timeout, default is 10000ms
    while (true) {
        try {
            let videoFrame = await receiver.video(timeout);
            //console.log(videoFrame);
        } catch (e) { console.error(e); }
    }
}

run()

And to launch it:

const { Worker } = require('worker_threads')
let ndi_worker = new Worker("./ndi_worker.js")

Output:

NewTek NDI Copyright (C)2015-2018 NewTek, inc. v3.5.9.0
NDI SDK WIN64 00:29:47 Jun 26 2018 3.5.9.0 true
Wait is 10000.
Find status is 1.
Destroying find carrier.
[
  { name: 'DESKTOP-P5UKCTA (OBS)', urlAddress: '130.63.209.196:5961' }
]
Completing some receive creation work.
{
  embedded: [External: 17124ce13d0],    
  video: [Function: video],
  audio: [Function: audio],
  metadata: [Function: metadata],       
  data: [Function: data],
  source: { name: 'DESKTOP-P5UKCTA (OBS)', urlAddress: '130.63.209.196:5961' }, 
  colorFormat: 100,
  bandwidth: 100,
  allowVideoFields: true
}
No data received.
[Error: In file ...  found error: No video data received 
in the requested time interval.] {      
  code: '4040'
}

It's baffling why it would work on the main thread but not on a worker thread. Could there be something in the implementation that assumes main thread somehow?

dortanes commented 11 months ago

Are you tried to use data() instead of video() or audio() ?

grrrwaaa commented 11 months ago

Yes, strangely using .data() does work, but .video() does not.