Last active
August 10, 2023 17:51
-
-
Save sunknudsen/c4e0a8bf08f4fea0680abe0702e93a1c to your computer and use it in GitHub Desktop.
Screen sharing crashes app on Ubuntu 22.04.2 LTS
This file contains 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>Test</title> | |
</head> | |
<body> | |
<button id="button1">Step 1 (get source ID)</button> <span id="span"></span> | |
<br /> | |
<button id="button2">Step 2 (play video stream)</button> | |
<br /> | |
<video id="video" width="400px"></video> | |
<script src="./renderer.js"></script> | |
</body> | |
</html> |
This file contains 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
const { app, BrowserWindow, crashReporter, desktopCapturer, ipcMain } = require('electron') | |
const path = require('path') | |
console.log(app.getPath('crashDumps')) | |
crashReporter.start({ submitURL: '', uploadToServer: false }) | |
function createWindow () { | |
const mainWindow = new BrowserWindow({ | |
width: 800, | |
height: 600, | |
webPreferences: { | |
preload: path.join(__dirname, 'preload.js') | |
} | |
}) | |
mainWindow.loadFile('index.html') | |
} | |
app.whenReady().then(() => { | |
createWindow() | |
app.on('activate', function () { | |
if (BrowserWindow.getAllWindows().length === 0) createWindow() | |
}) | |
}) | |
app.on('window-all-closed', function () { | |
if (process.platform !== 'darwin') app.quit() | |
}) | |
ipcMain.handle("getSourceId", async () => { | |
let sourceId = null | |
const sources = await desktopCapturer.getSources({ types: ['window'] }) | |
for (const source of sources) { | |
sourceId = source.id | |
} | |
return sourceId | |
}) |
This file contains 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": "colossal-plot-frame-l8hjl", | |
"productName": "colossal-plot-frame-l8hjl", | |
"description": "My Electron application description", | |
"keywords": [], | |
"main": "./main.js", | |
"version": "1.0.0", | |
"author": "sunknudsen", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"dependencies": {}, | |
"devDependencies": { | |
"electron": "25.3.0" | |
} | |
} |
This file contains 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
const { contextBridge, ipcRenderer } = require("electron") | |
const api = { | |
getSourceId: async () => { | |
const sourceId = await ipcRenderer.invoke("getSourceId") | |
return sourceId | |
} | |
} | |
contextBridge.exposeInMainWorld("api", api) |
This file contains 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
let sourceId = null | |
const button1 = document.getElementById("button1") | |
button1.onclick = async () => { | |
sourceId = await window.api.getSourceId() | |
const span = document.getElementById("span") | |
span.innerText = sourceId | |
} | |
const button2 = document.getElementById("button2") | |
button2.onclick = async () => { | |
const constraints = { | |
audio: false, | |
video: { | |
mandatory: { | |
chromeMediaSource: "desktop", | |
chromeMediaSourceId: sourceId, | |
} | |
} | |
} | |
const userMedia = await navigator.mediaDevices.getUserMedia( | |
constraints | |
) | |
const video = document.getElementById("video") | |
video.srcObject = userMedia | |
video.onloadedmetadata = video.play | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment