I have implemented a node.js Transform stream class that spawns ffmpeg and streams out the transformed stream at a controlled realtime rate. Below is my _transform() method.
this.rcvd += chunk.length
console.log('received bytes %d', this.rcvd)
const ready = this.ffmpeg.stdin.write(chunk, encoding, err => err
? cb(err)
: ready
? cb
: this.ffmpeg.stdin.once('drain', cb))
I want to write to ffmpeg's stdin stream till it returns false, at which point I wait for the drain event to write more data.
Concurrently, I have a timer that fires every 40 milliseconds that reads ffmpeg's stdout and pushes it to the Transform stream's output (the readable side). Code not complete, but the folllowing describes it well.
const now = Date.now()
const bytesToTransmit = (now - this.lastTx) * 32
const buf = this.ffmpeg.stdout.read()
if (buf == null) return
if (buf.length <= bytesToTransmit) {
this.push(buf)
this.lastTx += buf.length * 32
return
}
this.push(buf.slice(0, bytesToTransmit))
this.lastTx = now
// todo(handle pending buffer)
this.pending = buf.slice(bytesToTransmit)
The issue I am facing is that the first write (of 65k bytes) returns false, and after that I never receive the drain event. FFMpeg doesn't start processing the data until certain amount of data (256k bytes in my case) has been written, and once I start waiting for the drain event, I never recover control.
I've tried a few ffmpeg options like nobuffer but to no avail. What am I doing wrong? Any ideas would be super helpful.
Thanks!