Skip to content

Commit dc1ab91

Browse files
authored
fix(show-me/desktopcapturer): allow example to work for v17 and above (#1186)
1 parent 8f893ac commit dc1ab91

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

static/show-me/desktopcapturer/main.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// For more info, see:
55
// https://electronjs.org/docs/api/desktop-capturer
66

7-
const { app, BrowserWindow } = require('electron')
7+
const { app, BrowserWindow, desktopCapturer } = require('electron')
88
const path = require('path')
99

1010
app.whenReady().then(() => {
@@ -19,4 +19,14 @@ app.whenReady().then(() => {
1919
})
2020

2121
mainWindow.loadFile('index.html')
22+
if (parseInt(process.versions.electron) >= 17) {
23+
desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => {
24+
for (const source of sources) {
25+
if (source.id.startsWith('screen')) {
26+
mainWindow.webContents.send('SET_SOURCE', source.id)
27+
return
28+
}
29+
}
30+
})
31+
}
2232
})
Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { desktopCapturer } = require('electron')
2+
const { ipcRenderer } = require('electron')
23

34
// The following example shows how to capture video from
45
// the screen. It also grabs each window, so you could
@@ -12,31 +13,49 @@ function startCapture () {
1213
for (let i = 0; i < sources.length; ++i) {
1314
console.log(sources[i])
1415
if (sources[i].id.startsWith('screen')) {
15-
navigator.mediaDevices.getUserMedia({
16-
audio: false,
17-
video: {
18-
mandatory: {
19-
chromeMediaSource: 'desktop',
20-
chromeMediaSourceId: sources[i].id,
21-
minWidth: 1280,
22-
maxWidth: 1280,
23-
minHeight: 720,
24-
maxHeight: 720
25-
}
26-
}
27-
}).then((stream) => handleStream(stream))
28-
.catch((error) => console.log(error))
16+
showStream(sources[i].id)
2917
}
3018
}
3119
})
3220
}
3321

22+
async function showStream (sourceId) {
23+
try {
24+
const stream = await navigator.mediaDevices.getUserMedia({
25+
audio: false,
26+
video: {
27+
mandatory: {
28+
chromeMediaSource: 'desktop',
29+
chromeMediaSourceId: sourceId,
30+
minWidth: 1280,
31+
maxWidth: 1280,
32+
minHeight: 720,
33+
maxHeight: 720
34+
}
35+
}
36+
})
37+
handleStream(stream)
38+
} catch (e) {
39+
handleError(e)
40+
}
41+
}
42+
3443
function handleStream (stream) {
3544
const video = document.querySelector('video')
3645
video.srcObject = stream
3746
video.onloadedmetadata = (e) => video.play()
3847
}
3948

40-
window.addEventListener('DOMContentLoaded', () => {
41-
startCapture()
42-
})
49+
function handleError (e) {
50+
console.log(e)
51+
}
52+
53+
if (parseInt(process.versions.electron) >= 17) {
54+
ipcRenderer.on('SET_SOURCE', async (event, sourceId) => {
55+
showStream(sourceId)
56+
})
57+
} else {
58+
window.addEventListener('DOMContentLoaded', () => {
59+
startCapture()
60+
})
61+
}

0 commit comments

Comments
 (0)