Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetDisplayMedia: add Start/Stop switch #1616

Open
wants to merge 2 commits into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions src/content/getusermedia/getdisplaymedia/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
'use strict';

const preferredDisplaySurface = document.getElementById('displaySurface');
const startButton = document.getElementById('startButton');
const startStopButton = document.getElementById('startButton');
const videoElement = document.querySelector('video');
let displayMediaStarted = false;

if (adapter.browserDetails.browser === 'chrome' &&
adapter.browserDetails.version >= 107) {
Expand All @@ -21,16 +23,16 @@ if (adapter.browserDetails.browser === 'chrome' &&
}

function handleSuccess(stream) {
startButton.disabled = true;
displayMediaStarted = true;
startStopButton.textContent = 'Stop';
preferredDisplaySurface.disabled = true;
const video = document.querySelector('video');
video.srcObject = stream;
videoElement.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;
startStopButton.textContent = 'Start';
preferredDisplaySurface.disabled = false;
});
}
Expand All @@ -48,18 +50,28 @@ function errorMsg(msg, error) {
}


startButton.addEventListener('click', () => {
const options = {audio: true, video: true};
const displaySurface = preferredDisplaySurface.options[preferredDisplaySurface.selectedIndex].value;
if (displaySurface !== 'default') {
options.video = {displaySurface};
startStopButton.addEventListener('click', () => {
if (!displayMediaStarted) {
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);
}
else {
// demonstrates how to stop the stream from JavaScript
errorMsg('JavaScript has ended sharing the screen');
videoElement.srcObject.getTracks().forEach(track => track.stop());
videoElement.srcObject = null;
startStopButton.textContent = 'Start';
displayMediaStarted = false;
}
navigator.mediaDevices.getDisplayMedia(options)
.then(handleSuccess, handleError);
});

if ((navigator.mediaDevices && 'getDisplayMedia' in navigator.mediaDevices)) {
startButton.disabled = false;
startStopButton.disabled = false;
} else {
errorMsg('getDisplayMedia is not supported');
}