HTML5 Getusermedia Record Webcam, Both Audio and Video

Audio and video recorder in html5 using getusermedia()

MediaStreamRecorder is a WebRTC API for recording getUserMedia() streams . It allows web apps to create a file from a live audio/video session.

MediaStreamRecorder is currently unimplemented. you should wait for implementing it in all browsers.

 <video autoplay></video>

<script language="javascript" type="text/javascript">
function onVideoFail(e) {
console.log('webcam fail!', e);
};

function hasGetUserMedia() {
// Note: Opera is unprefixed.
return !!(navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia);
}

if (hasGetUserMedia()) {
// Good to go!
} else {
alert('getUserMedia() is not supported in your browser');
}

window.URL = window.URL || window.webkitURL;
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;

var video = document.querySelector('video');
var streamRecorder;
var webcamstream;

if (navigator.getUserMedia) {
navigator.getUserMedia({audio: true, video: true}, function(stream) {
video.src = window.URL.createObjectURL(stream);
webcamstream = stream;
// streamrecorder = webcamstream.record();
}, onVideoFail);
} else {
alert ('failed');
}

function startRecording() {
streamRecorder = webcamstream.record();
setTimeout(stopRecording, 10000);
}
function stopRecording() {
streamRecorder.getRecordedData(postVideoToServer);
}
function postVideoToServer(videoblob) {

var data = {};
data.video = videoblob;
data.metadata = 'test metadata';
data.action = "upload_video";
jQuery.post("http://www.kongaraju.in/uploadvideo.php", data, onUploadSuccess);
}
function onUploadSuccess() {
alert ('video uploaded');
}

</script>

<div id="webcamcontrols">
<button class="recordbutton" onclick="startRecording();">RECORD</button>
</div>

http://www.w3.org/TR/mediastream-recording/

Record Video and Screen together and overlay with Javascript

This can be achieved in pure JS as follows -

  1. Fetch Webcam Stream via getUserMedia()
  2. Fetch Screen Share Stream via getDisplayMedia()
  3. Merge Both Stream using some math & canvas operations
  4. Use canvas.captureStream() to generate the composite video stream.
  5. Use AudioContext to merge audio clips (especially needed if using both microphone & system audio).
  6. Use MediaStream constructor to create a new stream using - the video from the new stream + audio from audioContext Destination Node as follows -

new MediaStream([...newStream.getVideoTracks(), ...audioDestination.stream.getTracks()]);


  1. Use newly generated MediaStream as required (i.e. replace in RTCPeerConnection, etc.).
  2. In this example - MediaRecorder API is used to record the resulting composite/picture-in-picture video.
  3. Recording begins when the "Record Resulting Stream" button is clicked. The final result can be downloaded upon clicking the "Stop Recording and Download Resulting Stream" button.

PS: The snippet won't be able to fetch camera/screen share + please use this CodePen link to see it in action.

let localCamStream,
localScreenStream,
localOverlayStream,
rafId,
cam,
screen,
mediaRecorder,
audioContext,
audioDestination;
let mediaWrapperDiv = document.getElementById("mediaWrapper");
let startWebcamBtn = document.getElementById("startWebcam");
let startScreenShareBtn = document.getElementById("startScreenShare");
let mergeStreamsBtn = document.getElementById("mergeStreams");
let startRecordingBtn = document.getElementById("startRecording");
let stopRecordingBtn = document.getElementById("stopRecording");
let stopAllStreamsBtn = document.getElementById("stopAllStreams");
let canvasElement = document.createElement("canvas");
let canvasCtx = canvasElement.getContext("2d");
let encoderOptions = {
mimeType: "video/webm; codecs=vp9"
};
let recordedChunks = [];
let audioTracks = [];

/**
* Internal Polyfill to simulate
* window.requestAnimationFrame
* since the browser will kill canvas
* drawing when tab is inactive
*/
const requestVideoFrame = function(callback) {
return window.setTimeout(function() {
callback(Date.now());
}, 1000 / 60); // 60 fps - just like requestAnimationFrame
};

/**
* Internal polyfill to simulate
* window.cancelAnimationFrame
*/
const cancelVideoFrame = function(id) {
clearTimeout(id);
};

async function startWebcamFn() {
localCamStream = await navigator.mediaDevices.getUserMedia({
video: true,
audio: {
deviceId: {
exact: "communications"
}
}
});
if (localCamStream) {
cam = await attachToDOM("justWebcam", localCamStream);
}
}

async function startScreenShareFn() {
localScreenStream = await navigator.mediaDevices.getDisplayMedia({
video: true,
audio: true
});
if (localScreenStream) {
screen = await attachToDOM("justScreenShare", localScreenStream);
}
}

async function stopAllStreamsFn() {
[
...(localCamStream ? localCamStream.getTracks() : []),
...(localScreenStream ? localScreenStream.getTracks() : []),
...(localOverlayStream ? localOverlayStream.getTracks() : [])
].map((track) => track.stop());
localCamStream = null;
localScreenStream = null;
localOverlayStream = null;
cancelVideoFrame(rafId);
mediaWrapperDiv.innerHTML = "";
document.getElementById("recordingState").innerHTML = "";
}

async function makeComposite() {
if (cam && screen) {
canvasCtx.save();
canvasElement.setAttribute("width", `${screen.videoWidth}px`);
canvasElement.setAttribute("height", `${screen.videoHeight}px`);
canvasCtx.clearRect(0, 0, screen.videoWidth, screen.videoHeight);
canvasCtx.drawImage(screen, 0, 0, screen.videoWidth, screen.videoHeight);
canvasCtx.drawImage(
cam,
0,
Math.floor(screen.videoHeight - screen.videoHeight / 4),
Math.floor(screen.videoWidth / 4),
Math.floor(screen.videoHeight / 4)
); // this is just a rough calculation to offset the webcam stream to bottom left
let imageData = canvasCtx.getImageData(
0,
0,
screen.videoWidth,
screen.videoHeight
); // this makes it work
canvasCtx.putImageData(imageData, 0, 0); // properly on safari/webkit browsers too
canvasCtx.restore();
rafId = requestVideoFrame(makeComposite);
}
}

async function mergeStreamsFn() {
document.getElementById("mutingStreams").style.display = "block";
await makeComposite();
audioContext = new AudioContext();
audioDestination = audioContext.createMediaStreamDestination();
let fullVideoStream = canvasElement.captureStream();
let existingAudioStreams = [
...(localCamStream ? localCamStream.getAudioTracks() : []),
...(localScreenStream ? localScreenStream.getAudioTracks() : [])
];
audioTracks.push(
audioContext.createMediaStreamSource(
new MediaStream([existingAudioStreams[0]])
)
);
if (existingAudioStreams.length > 1) {
audioTracks.push(
audioContext.createMediaStreamSource(
new MediaStream([existingAudioStreams[1]])
)
);
}
audioTracks.map((track) => track.connect(audioDestination));
console.log(audioDestination.stream);
localOverlayStream = new MediaStream([...fullVideoStream.getVideoTracks()]);
let fullOverlayStream = new MediaStream([
...fullVideoStream.getVideoTracks(),
...audioDestination.stream.getTracks()
]);
console.log(localOverlayStream, existingAudioStreams);
if (localOverlayStream) {
overlay = await attachToDOM("pipOverlayStream", localOverlayStream);
mediaRecorder = new MediaRecorder(fullOverlayStream, encoderOptions);
mediaRecorder.ondataavailable = handleDataAvailable;
overlay.volume = 0;
cam.volume = 0;
screen.volume = 0;
cam.style.display = "none";
// localCamStream.getAudioTracks().map(track => { track.enabled = false });
screen.style.display = "none";
// localScreenStream.getAudioTracks().map(track => { track.enabled = false });
}
}

async function startRecordingFn() {
mediaRecorder.start();
console.log(mediaRecorder.state);
console.log("recorder started");
document.getElementById("pipOverlayStream").style.border = "10px solid red";
document.getElementById(
"recordingState"
).innerHTML = `${mediaRecorder.state}...`;
}

async function attachToDOM(id, stream) {
let videoElem = document.createElement("video");
videoElem.id = id;
videoElem.width = 640;
videoElem.height = 360;
videoElem.autoplay = true;
videoElem.setAttribute("playsinline", true);
videoElem.srcObject = new MediaStream(stream.getTracks());
mediaWrapperDiv.appendChild(videoElem);
return videoElem;
}

function handleDataAvailable(event) {
console.log("data-available");
if (event.data.size > 0) {
recordedChunks.push(event.data);
console.log(recordedChunks);
download();
} else {}
}

function download() {
var blob = new Blob(recordedChunks, {
type: "video/webm"
});
var url = URL.createObjectURL(blob);
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = url;
a.download = "result.webm";
a.click();
window.URL.revokeObjectURL(url);
}

function stopRecordingFn() {
mediaRecorder.stop();
document.getElementById(
"recordingState"
).innerHTML = `${mediaRecorder.state}...`;
}

startWebcamBtn.addEventListener("click", startWebcamFn);
startScreenShareBtn.addEventListener("click", startScreenShareFn);
mergeStreamsBtn.addEventListener("click", mergeStreamsFn);
stopAllStreamsBtn.addEventListener("click", stopAllStreamsFn);
startRecordingBtn.addEventListener("click", startRecordingFn);
stopRecordingBtn.addEventListener("click", stopRecordingFn);
div#mediaWrapper,
div#buttonWrapper {
display: flex;
flex: 1 1 100%;
flex-basis: row nowrap;
}

div#mediaWrapper video {
border: 1px solid black;
margin: 1px;
max-width: 33%;
height: auto;
}

div#mediaWrapper video#pipOverlayStream {
max-width: 100% !important;
}

div#buttonWrapper button {
border-radius: 0.25rem;
color: #ffffff;
display: inline-block;
font-size: 1rem;
font-weight: bold;
line-height: 1.6;
padding: 0.375rem 0.75rem;
text-align: center;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
vertical-align: middle;
margin: 5px;
cursor: pointer;
}

div#buttonWrapper button#startWebcam {
background-color: #007bff;
border: 1px solid #007bff;
}

div#buttonWrapper button#startScreenShare {
background-color: #17a2b8;
border: 1px solid #17a2b8;
}

div#buttonWrapper button#mergeStreams {
background-color: #28a745;
border: 1px solid #28a745;
}

div#buttonWrapper button#startRecording {
background-color: #17a2b8;
border: 1px solid #17a2b8;
}

div#buttonWrapper button#stopRecording {
background-color: #000000;
border: 1px solid #000000;
}

div#buttonWrapper button#stopAllStreams {
background-color: #dc3545;
border: 1px solid #dc3545;
}
<main>
<p>This demo is a proof-of-concept solution for this <a href="https://stackoverflow.com/questions/71557879" target="_blank" rel="noopener noreferrer">StackOverflow question</a> and <a href="https://stackoverflow.com/questions/37404860" target="_blank"
rel="noopener noreferrer">also this one</a> - as long as you make the required changes<br>i.e. <b>mimeType: "video/mp4; codecs=h264"</b> instead of <b>mimeType: "video/webm; codecs=vp9"</b><br>AND<br><b>type: "video/mp4"</b> instead of <b>type: "video/webm"</b><br>AND<br><b>result.mp4</b> instead of <b>result.webm</b></p>
<h2>Click on "Start Webcam" to get started. </h2>
<h3>
Core Idea:<br>
<ol>
<li>Fetch Webcam Stream via getUserMedia()</li>
<li>Fetch Screen Share Stream via getDisplayMedia()</li>
<li>Merge Both Stream using some math & canvas operations</li>
<li>Use canvas.captureStream() to generate the composite video stream.</li>
<li>Use AudioContext to merge audio clips (especially needed if using both microphone & system audio).</li>
<li>Use MediaStream constructor to create a new stream using - the video from the new stream + audio from audioContext Destination Node as follows -<br><br>
<code>new MediaStream([...newStream.getVideoTracks(), ...audioDestination.stream.getTracks()]);</code>
</li><br>
<li>Use newly generated MediaStream as required (i.e. replace in RTCPeerConnection, etc.).</li>
<li>In this example - MediaRecorder API is used to record the resulting composite/picture-in-picture video. Recording begins when the "Record Resulting Stream" button is clicked. The final result can be downloaded upon clicking the "Stop Recording and
Download Resulting Stream" button</li>
</ol>
</h3>
<div id="mediaWrapper"></div>
<div id="buttonWrapper">
<button id="startWebcam" title="Start Webcam">Start Webcam</button>
<button id="startScreenShare" title="Start Screen Share">Start Screen Share</button>
<button id="mergeStreams" title="Merge Streams">Merge Streams</button>
<button id="startRecording" title="Record Resulting Stream">Record Resulting Stream</button>
<button id="stopRecording" title="Stop Recording and Download Resulting Stream">Stop Recording and Download Resulting Stream</button>
<button id="stopAllStreams" title="Stop All Streams">Stop All Streams</button>
</div>

<div id="helpText">
<h1 id="recordingState"></h1><br>
<h2 id="mutingStreams">
Note: In a WebRTC setting, you wouldn't be hearing your own voice or the screen share audio via the "video" tag. The same has been simulated here by ensuring that all video tags have a "volume = 0". Removing this will create a loopback hell which you
do not want.<br><br> Another way to avoid this issue is to ensure that the video tags are created with ONLY video stream tracks using <em style="color: blue;">new MediaStream([ source.getVideoTracks() ])</em> during the srcObject assignment.
</h2>
<h1>
Remember to send the correct stream (with both audio and video) to the rest of the peers though.
</h1>
</div>
</main>

WebRTC getUserMedia: Switching between Audio and Video+Audio Streams

It turns out my problem was NOT with WebRTC or media streams, but rather with how I was serving the stream to the <video> element. I got rid of the black stream by disposing and recreating the video element each time a stream is started rather than just assigning it's source to the stream.



Related Topics



Leave a reply



Submit