Saturday, 25 January 2020

How to use MediaRecorder as MediaSource

As an exercise in learning WebRTC I am trying to to show the local webcam and side by side with a delayed playback of the webcam. In order to achieve this I am trying to pass recorded blobs to a BufferSource and use the corresponding MediaSource as source for a video element.

// the ondataavailable callback for the MediaRecorder
async function handleDataAvailable(event) {
  // console.log("handleDataAvailable", event);
  if (event.data && event.data.size > 0) {
    recordedBlobs.push(event.data);
  }

  if (recordedBlobs.length > 5) {
    if (recordedBlobs.length === 5)
      console.log("buffered enough for delayed playback");
    if (!updatingBuffer) {
      updatingBuffer = true;
      const bufferedBlob = recordedBlobs.shift();
      const bufferedAsArrayBuffer = await bufferedBlob.arrayBuffer();
      if (!sourceBuffer.updating) {
        console.log("appending to buffer");
        sourceBuffer.appendBuffer(bufferedAsArrayBuffer);
      } else {
        console.warn("Buffer still updating... ");
        recordedBlobs.unshift(bufferedBlob);
      }
    }
  }
}
// connecting the media source to the video element
recordedVideo.src = null;
recordedVideo.srcObject = null;
recordedVideo.src = window.URL.createObjectURL(mediaSource);
recordedVideo.controls = true;
try {
  await recordedVideo.play();
} catch (e) {
  console.error(`Play failed: ${e}`);
}

All code: https://jsfiddle.net/43rm7258/1/

When I run this in Chromium 78 I get an NotSupportedError: Failed to load because no supported source was found. from the play element of the video element.

I have no clue what I am doing wrong or how to proceed at this point.

This is about something similar, but does not help me: MediaSource randomly stops video

This example was my starting point: https://webrtc.github.io/samples/src/content/getusermedia/record/



from How to use MediaRecorder as MediaSource

2 comments:

  1. Did you hear there's a 12 word sentence you can say to your crush... that will induce deep feelings of love and instinctual attractiveness to you deep inside his heart?

    That's because deep inside these 12 words is a "secret signal" that fuels a man's instinct to love, treasure and guard you with his entire heart...

    ===> 12 Words That Trigger A Man's Desire Response

    This instinct is so hardwired into a man's brain that it will make him work better than before to build your relationship stronger.

    In fact, fueling this mighty instinct is absolutely mandatory to getting the best possible relationship with your man that as soon as you send your man one of these "Secret Signals"...

    ...You will instantly notice him expose his soul and mind for you in such a way he never experienced before and he'll perceive you as the one and only woman in the universe who has ever truly appealed to him.

    ReplyDelete