Skip to content Skip to sidebar Skip to footer

How Can We Mix Canvas Stream With Audio Stream Using Mediarecorder

I have a canvas stream using canvas.captureStream(). I have another video stream from webrtc video call. Now i want to mix canvas stream with audio tracks of the video stream.How c

Solution 1:

Use the MediaStream constructor available in Firefox and Chrome 56, to combine tracks into a new stream:

let stream = new MediaStream([videoTrack, audioTrack]);

The following works for me in Firefox (Use https fiddle in Chrome, though it errors on recording):

navigator.mediaDevices.getUserMedia({audio: true})
  .then(stream =>record(newMediaStream([stream.getTracks()[0],
                                          whiteNoise().getTracks()[0]]), 5000)
    .then(recording => {
      stop(stream);
      video.src = link.href = URL.createObjectURL(newBlob(recording));
      link.download = "recording.webm";
      link.innerHTML = "Download recording";
      log("Playing "+ recording[0].type +" recording:");
    })
    .catch(log))
  .catch(log);

varwhiteNoise = () => {
  let ctx = canvas.getContext('2d');
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  let p = ctx.getImageData(0, 0, canvas.width, canvas.height);
  requestAnimationFrame(functiondraw(){
    for (var i = 0; i < p.data.length; i++) {
      p.data[i++] = p.data[i++] = p.data[i++] = Math.random() * 255;
    }
    ctx.putImageData(p, 0, 0);
    requestAnimationFrame(draw);
  });
  return canvas.captureStream(60);
}

varrecord = (stream, ms) => {
  var rec = newMediaRecorder(stream), data = [];
  rec.ondataavailable = e => data.push(e.data);
  rec.start();
  log(rec.state + " for "+ (ms / 1000) +" seconds...");
  var stopped = newPromise((y, n) =>
      (rec.onstop = y, rec.onerror = e =>n(e.error || e.name)));
  returnPromise.all([stopped, wait(ms).then(_ => rec.stop())]).then(_ => data);
};

varstop = stream => stream.getTracks().forEach(track => track.stop());
varwait = ms => newPromise(resolve =>setTimeout(resolve, ms));
varlog = msg => div.innerHTML += "<br>" + msg;
<divid="div"></div><br><canvasid="canvas"width="160"height="120"hidden></canvas><videoid="video"width="160"height="120"autoplay></video><aid="link"></a>

Post a Comment for "How Can We Mix Canvas Stream With Audio Stream Using Mediarecorder"