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.
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.
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.
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.
getUserMedia one. Get User Media/Stream API is supported by Mozilla Firefox browser version 36 to 61.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With