-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
77 lines (66 loc) · 2.29 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* This file is loaded via the <script> tag in the index.html file and will
* be executed in the renderer process for that window. No Node.js APIs are
* available in this process because `nodeIntegration` is turned off and
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
* to expose Node.js functionality from the main process.
*/
const both_btn = document.getElementById("both-btn")
both_btn.addEventListener('click', async () => {
displaySources(await window.electron.listAll())
})
const screens_btn = document.getElementById("screens-btn")
screens_btn.addEventListener('click', async () => {
displaySources(await window.electron.listScreens())
})
const windows_btn = document.getElementById("windows-btn")
windows_btn.addEventListener('click', async () => {
displaySources(await window.electron.listWindows())
})
async function displaySources(desktop_sources) {
const sources_element = document.getElementById("sources")
sources_element.replaceChildren()
for (i = 0; i < desktop_sources.length; i++) {
const source_div = document.createElement("div")
const source_thumb = document.createElement("img")
source_thumb.src = desktop_sources[i].image
source_div.appendChild(source_thumb)
const source_btn = document.createElement("button")
const source_id = desktop_sources[i].id
source_btn.innerHTML = source_id
source_btn.addEventListener('click', () => startDisplayMedia(source_id))
source_div.appendChild(source_btn)
sources_element.appendChild(source_div)
}
}
async function startDisplayMedia(source_id) {
try {
const stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: source_id,
}
}
})
handleStream(stream)
setupStopStream(stream)
} catch (e) {
handleError(e)
}
}
function setupStopStream(stream) {
const both_btn = document.getElementById("stop-stream")
both_btn.addEventListener('click', async () => {
stream.getTracks().forEach(track => track.stop())
})
}
function handleStream(stream) {
const video = document.querySelector('video')
video.srcObject = stream
video.onloadedmetadata = (e) => video.play()
}
function handleError(e) {
console.log(e)
}