-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.html
48 lines (41 loc) · 1.38 KB
/
stream.html
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
<html>
<head>
<title>Stream</title>
</head>
<body>
<button>Connect</button>
<script type="module">
const ws = import.meta.hot;
let stream;
async function connect() {
if (!stream) {
stream = await navigator.mediaDevices.getDisplayMedia({
video: { frameRate: 30, displaySurface: "monitor" },
});
}
const pc = new RTCPeerConnection();
stream.getTracks().forEach((track) => pc.addTrack(track, stream));
// Send an SDP offer
const offer = await pc.createOffer();
pc.setLocalDescription(offer);
ws.send("rtc:offer", JSON.stringify(offer));
// Recieve the SDP answer
ws?.on("rtc:answer", async (data) => {
await pc.setRemoteDescription(JSON.parse(data));
console.log("remote display stream started");
});
// Send ICE candidate messages
pc.addEventListener("icecandidate", (e) => {
if (e.candidate) ws.send("rtc:ice", JSON.stringify(e.candidate));
});
// Recieve ICE candidate messages
ws?.on("rtc:ice", async (data) => {
pc.addIceCandidate(new RTCIceCandidate(JSON.parse(data)));
});
}
// Listen for connect events
ws?.on("rtc:connect", connect);
document.querySelector("button").addEventListener("click", connect);
</script>
</body>
</html>