-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
78 lines (67 loc) · 2.04 KB
/
script.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const video = document.getElementById('video');
const buttonActivation = document.getElementById('button-container');
const button = document.getElementById('button');
const buttonAction = {
show: 'SHOW',
stop: 'STOP'
};
const captureController = new CaptureController();
// Avoid to change tab to video source
// alt "focus-captured-surface"
captureController.setFocusBehavior("no-focus-change");
const displayMediaOptions = {
// video: {
// displaySurface: "browser",
// },
// audio: {
// suppressLocalAudioPlayback: true,
// },
// preferCurrentTab: false,
// selfBrowserSurface: Exclude the current tab from the list of tabs offered to the user.
selfBrowserSurface: "exclude",
// surfaceSwitching: Don't display Stop sharing button BUT don't work
surfaceSwitching: "exclude",
monitorTypeSurfaces: "exclude",
controller: captureController,
};
let pipWindow = null;
setButtonTextContent = (namedAction) => {
button.textContent = `${namedAction} PIP mode`;
button.dataset.action = namedAction;
};
setButtonStop = () => setButtonTextContent(buttonAction.stop);
setButtonShow = () => setButtonTextContent(buttonAction.show);
video.onloadeddata = (event) => {
video.play();
setButtonShow();
buttonActivation.hidden = false;
};
video.onleavepictureinpicture = () => {
console.log('Leaving Picture in picture...');
setButtonShow();
};
async function selectMediaStream() {
try {
video.srcObject = await navigator.mediaDevices.getDisplayMedia(displayMediaOptions);
} catch (error) {
console.error(`Failed to select media stream: ${error}`);
}
}
button.addEventListener('click', async () => {
button.disabled = true;
try {
if (video !== document.pictureInPictureElement) {
pipWindow = await video.requestPictureInPicture();
setButtonStop();
} else {
await document.exitPictureInPicture();
setButtonShow();
}
} catch (error) {
console.error(`Picture in picture error: ${error}`);
} finally {
button.disabled = false;
}
});
buttonActivation.hidden = true;
selectMediaStream();