I am using Twilio for transfering audio calls to a meeting room on top of Mediasoup. Using Twilio websocket I get media stream encoded in base64 and push that to a NodeJS Readable stream. Then I will pipe that readable stream to a ffmpeg process I created before, using NodeJS spawn()
The problem is that only 92 bytes pipes into ffmpeg and so the output is not playable. I dont have any idea if the options of ffmpeg is wrong or I'm using Readable stream bad way.
const process = spawn('ffmpeg', [
'-loglevel',
'debug',
'-re',
'-protocol_whitelist',
'pipe,udp,rtp',
'-f',
'mulaw',
'-i',
'pipe:0',
'-map',
'0:a',
'-c:a',
'pcm_mulaw',
'output.wav'
]);
const rstream = new Readable({ encoding: 'binary' })
rstream._read = () => {};
rstream.resume();
rstream.pipe(process.stdin)
each time I get media from Twilio socket as base64, I push that chunk into rstream using:
const mediaBytes = Buffer.from(base64Chunk, "base64")
const count = mediaBytes.byteLength
rstream.push(Buffer.from([0x52, 0x49, ... /* PCM MU_LAW HEADER 54 Bytes */]))
rstream.push(Buffer.from([
count % 256,
(count >> 8) % 256,
(count >> 16) % 256,
(count >> 24) % 256,
]))
rstream.push(mediaBytes)