-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshim.js
60 lines (51 loc) · 1.83 KB
/
shim.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Older browsers might not implement mediaDevices at all, so we set an empty object first
if (navigator.mediaDevices === undefined) {
navigator.mediaDevices = {};
}
// Some browsers partially implement mediaDevices. We can't just assign an object
// with getUserMedia as it would overwrite existing properties.
// Here, we will just add the getUserMedia property if it's missing.
if (navigator.mediaDevices.getUserMedia === undefined) {
navigator.mediaDevices.getUserMedia = function (constraints) {
// First get ahold of the legacy getUserMedia, if present
var getUserMedia =
navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
// Some browsers just don't implement it - return a rejected promise with an error
// to keep a consistent interface
if (!getUserMedia) {
return Promise.reject(
new Error("getUserMedia is not implemented in this browser")
);
}
// Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
return new Promise(function (resolve, reject) {
getUserMedia.call(navigator, constraints, resolve, reject);
});
};
}
function setVideoSrc(video, mediaStream) {
if ("srcObject" in video) {
video.srcObject = mediaStream;
} else {
// Avoid using this in new browsers, as it is going away.
video.src = window.URL.createObjectURL(mediaStream);
}
}
function showVideo(selector, mediaStream) {
const dom = document.querySelector(selector);
if (!dom) {
return;
}
dom.innerHTML = "";
const video = document.createElement("video");
const audio = document.createElement("audio");
audio.muted = true;
video.muted = true;
video.onloadedmetadata = function (e) {
video.play();
};
// video.srcObject = mediaStream;
setVideoSrc(video, mediaStream);
dom.appendChild(video);
dom.appendChild(audio);
}