JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
JavaScript
let handleStream = s => {
document.body.append(
Object.assign(document.createElement('video'), {
autoplay: true,
mozSrcObject: s,
srcObject: s
})
);
}
navigator.mediaDevices.enumerateDevices().then(
function(devices) {
let sourceId = null;
// enumerate all devices
for (var device of devices) {
// if there is still no video input, or if this is the rear camera
if (device.kind == 'videoinput' &&
(!sourceId || device.label.indexOf('back') !== -1)) {
sourceId = device.deviceId;
}
}
// we didn't find any video input
if (!sourceId) {
throw 'no video input';
}
let constraints = {
video: {
sourceId: sourceId
}
};
navigator.mediaDevices.getUserMedia(constraints)
.then(handleStream);
}
);