loled/static/js/grab-canvas.js

77 lines
2.3 KiB
JavaScript
Raw Normal View History

2023-09-14 11:43:36 +02:00
(async () => {
const globalConnnection = Symbol("grab-canvas-connection")
const currentScriptSrc = document.currentScript.src;
2023-09-21 11:52:30 +02:00
const sourceElt = document.querySelector("canvas,video");
2023-09-21 12:04:11 +02:00
console.log("Grabbing", sourceElt)
2023-09-14 11:43:36 +02:00
2023-09-21 11:52:30 +02:00
if(!sourceElt){
2023-09-14 11:43:36 +02:00
console.error("No canvas found on this page")
}
const conn = new RTCPeerConnection({
iceServers: [
{urls: ["stun:stun.nextcloud.com:443"]}
]
});
const iceCandidates = []
conn.addEventListener("icecandidate", e => {
iceCandidates.push(e.candidate)
})
const allCandidatesCollected = new Promise(res =>
conn.addEventListener("icecandidate", e => e.candidate == null && res() ))
2023-09-21 11:52:30 +02:00
let captureFn = sourceElt.captureStream;
if(!captureFn){
captureFn = sourceElt.mozCaptureStream;
}
2023-09-21 11:56:20 +02:00
if(sourceElt instanceof HTMLCanvasElement){
captureFn = sourceElt.captureStream.bind(sourceElt)
} else {
if(sourceElt.mozCaptureStream){
captureFn = sourceElt.mozCaptureStream.bind(sourceElt)
} else {
captureFn = sourceElt.captureStream.bind(sourceElt)
}
}
2023-09-21 11:52:30 +02:00
const canvasStream = captureFn()
2023-09-14 11:43:36 +02:00
const canvasStreamTracks = canvasStream.getVideoTracks()
if(canvasStreamTracks.length > 0){
conn.addTrack(canvasStreamTracks[0], canvasStream)
2023-09-21 12:04:11 +02:00
} else {
throw new Error("Element don't habe video track")
2023-09-14 11:43:36 +02:00
}
2023-09-21 12:31:40 +02:00
try {
const AudioContext = window.AudioContext || window.webkitAudioContext;
if(AudioContext){
const audioContext = new AudioContext();
audioContext.createMediaStreamSource(canvasStream)
.connect(audioContext.destination)
}
} catch(e){
console.warn(e)
2023-09-21 12:16:12 +02:00
}
2023-09-14 11:43:36 +02:00
const offer = await conn.createOffer();
await conn.setLocalDescription(offer);
await allCandidatesCollected
const res = await fetch(new URL("/_loled/grab-display", currentScriptSrc), {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
offer,
iceCandidates
})
})
const response = new RTCSessionDescription(await res.json());
conn.setRemoteDescription(response);
window[globalConnnection] = conn
})()