Skip to content

Instantly share code, notes, and snippets.

@VerteDinde
Created August 21, 2024 19:56
Show Gist options
  • Save VerteDinde/ecd068bd4ca9e2e5b92a45a79d34eb81 to your computer and use it in GitHub Desktop.
Save VerteDinde/ecd068bd4ca9e2e5b92a45a79d34eb81 to your computer and use it in GitHub Desktop.
setDisplayMediaRequestHandler example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using Node.js <span id="node-version"></span>,
Chromium <span id="chrome-version"></span>,
and Electron <span id="electron-version"></span>.
<!-- You can also require other files to run in this process -->
<button id="startButton" class="button">Start</button>
<button id="stopButton" class="button">Stop</button>
<button id="pauseButton" class="button">Pause</button>
<video width="320" height="240" autoplay></video>
<script src="./renderer.js"></script>
</body>
</html>
// Modules to control application life and create native browser window
const { app, BrowserWindow, desktopCapturer, session } = require('electron')
const path = require('node:path')
function createWindow () {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadFile('index.html')
// mainWindow.webContents.openDevTools()
}
app.whenReady().then(() => {
createWindow()
session.defaultSession.setDisplayMediaRequestHandler((request, callback) => {
// If we should use the system picker
// Note: this is currently experimental
// if (desktopCapturer.isDisplayMediaSystemPickerAvailable()) {
// callback({ video: desktopCapturer.systemPickerVideoSource })
// return;
// }
desktopCapturer.getSources({ types: ['screen'] }).then((sources) => {
console.log('Request: ', request)
// Grant access to the first screen found.
callback({ video: sources[0] })
}).catch((err) => {
console.log('Error: ', err);
})
})
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
{
"name": "getDisplayMedia",
"productName": "getDisplayMedia",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "khammond",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "999.999.999"
}
}
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const type of ['chrome', 'node', 'electron']) {
replaceText(`${type}-version`, process.versions[type])
}
})
// renderer.js
const startButton = document.getElementById('startButton')
const stopButton = document.getElementById('stopButton')
const video = document.querySelector('video')
startButton.addEventListener('click', () => {
navigator.mediaDevices.getDisplayMedia({
audio: true,
video: {
width: 320,
height: 240,
frameRate: 30
}
}).then(stream => {
video.srcObject = stream;
video.onloadedmetadata = (e) => video.play();
}).catch(e => console.log("Failed in getDisplayMedia", e))
})
stopButton.addEventListener('click', () => {
video.close()
})
pauseButton.addEventListener('click', () => {
video.pause()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment