Added tools
This commit is contained in:
42
static/js/rtc.js
Normal file
42
static/js/rtc.js
Normal file
@ -0,0 +1,42 @@
|
||||
export async function connectSomeStream(canvasStream){
|
||||
const globalConnnection = Symbol("grab-canvas-connection")
|
||||
|
||||
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() ))
|
||||
|
||||
const canvasStreamTracks = canvasStream.getVideoTracks()
|
||||
if(canvasStreamTracks.length > 0){
|
||||
conn.addTrack(canvasStreamTracks[0], canvasStream)
|
||||
} else {
|
||||
throw new Error("Stream don't have video track")
|
||||
}
|
||||
|
||||
const offer = await conn.createOffer();
|
||||
await conn.setLocalDescription(offer);
|
||||
|
||||
await allCandidatesCollected
|
||||
|
||||
const res = await fetch(new URL("/_loled/grab-display", window.location), {
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user