WebRTC: Is it possible to create a VideoStreamTrack from RGBA ArrayBuffer?

Viewed 507

I have a RGBA stream coming from a Redis callback inside an Electron application:

redis.on('messageBuffer', function(channel, frame) {
  if (channel.toString() !== REDIS_EVENT) return;

});

In order to deliver it as a video through WebRTC, I created a Canvas and used it as a source stream to my WebRTC connection, using HTMLCanvasElement.captureStream().

let canvasCtx = myCanvas.getContext('2d');
let stream = myCanvas.captureStream(10);

redis.on('messageBuffer', function(channel, frame) {
  if (channel.toString() !== REDIS_EVENT) return;

  let imageData = canvasCtx.createImageData(myCanvas.width, myCanvas.height);
  imageData.data.set(frame);
  canvasCtx.putImageData(imageData, 0, 0);
});

The stream is a CanvasCaptureMediaStream object, and here is how I'm using it as WebRTC stream source:

function createWebRTC(stream, clientId) {
  let pc = new RTCPeerConnection();

  // ...

  pc.addStream(stream);
}

This approach is working but it's definitely not efficient since I have to draw it in a canvas everytime to feed a MediaStream object.

Is there a better approach to convert RGBA stream into VideoStreamTrack?

0 Answers
Related