Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a stream of bytes from navigator.mediaDevices.getUserMedia()?

I am aware of how to retrieve eg. a video stream from a webcam source:

const constraints = { video: true };

navigator.mediaDevices

    .getUserMedia(constraints)

    .then(mediaStream => {

        // ** But how to get the a stream of bytes from here??? **

    });

I could not find any proper documentation of how to retrieve a stream of bytes from the mediaStream object.

How to do this? Because, lets say I want to stream the bytes to the server.

like image 784
Dachstein Avatar asked Jul 26 '18 16:07

Dachstein


People also ask

What is navigator MediaDevices getUserMedia?

The MediaDevices . getUserMedia() method prompts the user for permission to use a media input which produces a MediaStream with tracks containing the requested types of media.

Is getUserMedia deprecated?

getUserMedia() Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes.

Is getUserMedia a part of webRTC?

getUserMedia() is a part of the webRTC media capture API and is used to get access to the camera and the microphone connected to the user device (user computer, smartphone, etc.) from the browser.

Which browsers support getUserMedia?

getUserMedia one. Get User Media/Stream API is supported by Mozilla Firefox browser version 36 to 61.


1 Answers

MediaStream Recording API

By further investigating into MDN and the HTML 5 APIs related to Audio and Video I have found the MediaStream Recording API.

So, to get the byte stream (or chunks as soon as some are available) we can do this:

const constraints = { video: true };

navigator.mediaDevices

    .getUserMedia(constraints)

    .then(mediaStream => {

        // use MediaStream Recording API
        const recorder = new MediaRecorder(stream);

        // fires every one second and passes an BlobEvent
        recorder.ondataavailable = event => {

            // get the Blob from the event
            const blob = event.data;

            // and send that blob to the server...
        };

        // make data available event fire every one second
        recorder.start(1000);
    });

Browser Support:

The MediaStream Recording API is still in Working Draft status (March 2018). Currently only natively supported in Chrome and Firefox.

Polyfill: streamproc/MediaStreamRecorder

Further reading: Record to an audio file

like image 121
Dachstein Avatar answered Sep 23 '22 00:09

Dachstein