/
main.js
65 lines (56 loc) · 2.13 KB
/
main.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
/*
* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree.
*/
'use strict';
const preferredDisplaySurface = document.getElementById('displaySurface');
const startButton = document.getElementById('startButton');
if (adapter.browserDetails.browser === 'chrome' &&
adapter.browserDetails.version >= 107) {
// See https://developer.chrome.com/docs/web-platform/screen-sharing-controls/
document.getElementById('options').style.display = 'block';
} else if (adapter.browserDetails.browser === 'firefox') {
// Polyfill in Firefox.
// See https://blog.mozilla.org/webrtc/getdisplaymedia-now-available-in-adapter-js/
adapter.browserShim.shimGetDisplayMedia(window, 'screen');
}
function handleSuccess(stream) {
startButton.disabled = true;
preferredDisplaySurface.disabled = true;
const video = document.querySelector('video');
video.srcObject = stream;
// demonstrates how to detect that the user has stopped
// sharing the screen via the browser UI.
stream.getVideoTracks()[0].addEventListener('ended', () => {
errorMsg('The user has ended sharing the screen');
startButton.disabled = false;
preferredDisplaySurface.disabled = false;
});
}
function handleError(error) {
errorMsg(`getDisplayMedia error: ${error.name}`, error);
}
function errorMsg(msg, error) {
const errorElement = document.querySelector('#errorMsg');
errorElement.innerHTML += `<p>${msg}</p>`;
if (typeof error !== 'undefined') {
console.error(error);
}
}
startButton.addEventListener('click', () => {
const options = {audio: true, video: true};
const displaySurface = preferredDisplaySurface.options[preferredDisplaySurface.selectedIndex].value;
if (displaySurface !== 'default') {
options.video = {displaySurface};
}
navigator.mediaDevices.getDisplayMedia(options)
.then(handleSuccess, handleError);
});
if ((navigator.mediaDevices && 'getDisplayMedia' in navigator.mediaDevices)) {
startButton.disabled = false;
} else {
errorMsg('getDisplayMedia is not supported');
}