Created
August 27, 2024 15:38
-
-
Save VerteDinde/8e4e41c6606efea081add0487dd2cb6d to your computer and use it in GitHub Desktop.
desktopCapturer API Sequioa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'"> | |
<title>Hello World!</title> | |
</head> | |
<body> | |
<h1>Hello World!</h1> | |
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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((req, callback) => { | |
// If we should use the system picker | |
// Note: this is currently experimental | |
if (desktopCapturer.isDisplayMediaSystemPickerAvailable()) { | |
desktopCapturer.getNativePickerSource().then((sources) => { | |
console.log('Source: ', sources); | |
callback({ video: sources[0] }) | |
}); | |
return; | |
} | |
desktopCapturer.getSources({ types: ['screen', 'window'], thumbnailSize: { width: 0, height: 0 } }).then((sources) => { | |
console.log('Request: ', req) | |
console.log('Sources: ', sources) | |
// 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() | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// renderer.js | |
const startButton = document.getElementById('startButton') | |
const stopButton = document.getElementById('stopButton') | |
const video = document.querySelector('video') | |
startButton.addEventListener('click', () => { | |
console.log('inside displayMedia') | |
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)) | |
}) | |
pauseButton.addEventListener('click', () => { | |
video.pause() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment