wip v2
This commit is contained in:
205
static/webRTC.js
205
static/webRTC.js
@@ -1,12 +1,66 @@
|
||||
"use strict";
|
||||
class PeerManager {
|
||||
// import { wsConnection } from "./main";
|
||||
export class PeerManager {
|
||||
connect(peerID) {
|
||||
// Connect to the peer that has the peer id peerID
|
||||
}
|
||||
disconnect(peerID) {
|
||||
}
|
||||
}
|
||||
class PeerConnection {
|
||||
export class PeerConnection {
|
||||
constructor(remotePeerID, signaler) {
|
||||
this.makingOffer = false;
|
||||
this.ignoreOffer = false;
|
||||
this.onSignallerMessage = async ({ data: { description, candidate } }) => {
|
||||
try {
|
||||
if (description) {
|
||||
// const offerCollision =
|
||||
// description.type === "offer" &&
|
||||
// (this.makingOffer || this.rtcPeer.signalingState !== "stable");
|
||||
// this.ignoreOffer = !polite && offerCollision;
|
||||
if (this.ignoreOffer) {
|
||||
return;
|
||||
}
|
||||
await this.rtcPeer.setRemoteDescription(description);
|
||||
if (description.type === "offer") {
|
||||
await this.rtcPeer.setLocalDescription();
|
||||
this.signaler.send(JSON.stringify({ description: this.rtcPeer.localDescription }));
|
||||
}
|
||||
}
|
||||
else if (candidate) {
|
||||
try {
|
||||
await this.rtcPeer.addIceCandidate(candidate);
|
||||
}
|
||||
catch (err) {
|
||||
if (!this.ignoreOffer) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
};
|
||||
this.id = remotePeerID;
|
||||
this.rtcPeer = new RTCPeerConnection(PeerConnection.config);
|
||||
this.signaler = signaler;
|
||||
;
|
||||
this.rtcPeer.onnegotiationneeded = async () => {
|
||||
try {
|
||||
this.makingOffer = true;
|
||||
await this.rtcPeer.setLocalDescription();
|
||||
signaler.send(JSON.stringify({ description: this.rtcPeer.localDescription }));
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
finally {
|
||||
this.makingOffer = false;
|
||||
}
|
||||
};
|
||||
this.rtcPeer.onicecandidate = ({ candidate }) => signaler.send(JSON.stringify({ candidate }));
|
||||
this.ignoreOffer = false;
|
||||
}
|
||||
}
|
||||
PeerConnection.config = {
|
||||
iceServers: [
|
||||
@@ -17,80 +71,75 @@ PeerConnection.config = {
|
||||
{ urls: "stun:stun4.l.google.com" },
|
||||
],
|
||||
};
|
||||
const config = {
|
||||
iceServers: [{ urls: "stun:stun.mystunserver.tld" }],
|
||||
};
|
||||
let polite = true;
|
||||
// const config = {
|
||||
// iceServers: [{ urls: "stun:stun.mystunserver.tld" }],
|
||||
// };
|
||||
// let polite = true;
|
||||
// const signaler = new SignalingChannel();
|
||||
const signaler = {};
|
||||
const pc = new RTCPeerConnection(config);
|
||||
const constraints = { audio: true, video: true };
|
||||
const selfVideo = document.querySelector("video.selfview");
|
||||
const remoteVideo = document.querySelector("video.remoteview");
|
||||
async function start() {
|
||||
try {
|
||||
const stream = await navigator.mediaDevices.getUserMedia(constraints);
|
||||
for (const track of stream.getTracks()) {
|
||||
pc.addTrack(track, stream);
|
||||
}
|
||||
// selfVideo.srcObject = stream;
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
pc.ontrack = ({ track, streams }) => {
|
||||
track.onunmute = () => {
|
||||
// if (remoteVideo.srcObject) {
|
||||
// return;
|
||||
// }
|
||||
// remoteVideo.srcObject = streams[0];
|
||||
};
|
||||
};
|
||||
let makingOffer = false;
|
||||
pc.onnegotiationneeded = async () => {
|
||||
try {
|
||||
makingOffer = true;
|
||||
await pc.setLocalDescription();
|
||||
signaler.send({ description: pc.localDescription });
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
finally {
|
||||
makingOffer = false;
|
||||
}
|
||||
};
|
||||
pc.onicecandidate = ({ candidate }) => signaler.send({ candidate });
|
||||
let ignoreOffer = false;
|
||||
signaler.onmessage = async ({ data: { description, candidate } }) => {
|
||||
try {
|
||||
if (description) {
|
||||
const offerCollision = description.type === "offer" &&
|
||||
(makingOffer || pc.signalingState !== "stable");
|
||||
ignoreOffer = !polite && offerCollision;
|
||||
if (ignoreOffer) {
|
||||
return;
|
||||
}
|
||||
await pc.setRemoteDescription(description);
|
||||
if (description.type === "offer") {
|
||||
await pc.setLocalDescription();
|
||||
signaler.send({ description: pc.localDescription });
|
||||
}
|
||||
}
|
||||
else if (candidate) {
|
||||
try {
|
||||
await pc.addIceCandidate(candidate);
|
||||
}
|
||||
catch (err) {
|
||||
if (!ignoreOffer) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
};
|
||||
// const signaler: any = {}
|
||||
// const rtcPeer = new RTCPeerConnection(config);
|
||||
// // const constraints = { audio: true, video: true };
|
||||
// const selfVideo = document.querySelector("video.selfview");
|
||||
// const remoteVideo = document.querySelector("video.remoteview");
|
||||
// async function start() {
|
||||
// try {
|
||||
// const stream = await navigator.mediaDevices.getUserMedia(constraints);
|
||||
// for (const track of stream.getTracks()) {
|
||||
// pc.addTrack(track, stream);
|
||||
// }
|
||||
// selfVideo.srcObject = stream;
|
||||
// } catch (err) {
|
||||
// console.error(err);
|
||||
// }
|
||||
// }
|
||||
// rtcPeer.ontrack = ({ track, streams }) => {
|
||||
// track.onunmute = () => {
|
||||
// if (remoteVideo.srcObject) {
|
||||
// return;
|
||||
// }
|
||||
// remoteVideo.srcObject = streams[0];
|
||||
// };
|
||||
// };
|
||||
// makingOffer = false;
|
||||
// rtcPeer.onnegotiationneeded = async () => {
|
||||
// try {
|
||||
// // makingOffer = true;
|
||||
// await rtcPeer.setLocalDescription();
|
||||
// signaler.send({ description: rtcPeer.localDescription });
|
||||
// } catch (err) {
|
||||
// console.error(err);
|
||||
// } finally {
|
||||
// makingOffer = false;
|
||||
// }
|
||||
// };
|
||||
// rtcPeer.onicecandidate = ({ candidate }) => signaler.send({ candidate });
|
||||
// let ignoreOffer = false;
|
||||
// signaler.onmessage = async ({ data: { description, candidate } }: MessageEvent) => {
|
||||
// try {
|
||||
// if (description) {
|
||||
// const offerCollision =
|
||||
// description.type === "offer" &&
|
||||
// // (makingOffer || rtcPeer.signalingState !== "stable");
|
||||
// ignoreOffer = !polite && offerCollision;
|
||||
// if (ignoreOffer) {
|
||||
// return;
|
||||
// }
|
||||
// await rtcPeer.setRemoteDescription(description);
|
||||
// if (description.type === "offer") {
|
||||
// await rtcPeer.setLocalDescription();
|
||||
// signaler.send({ description: rtcPeer.localDescription });
|
||||
// }
|
||||
// } else if (candidate) {
|
||||
// try {
|
||||
// await rtcPeer.addIceCandidate(candidate);
|
||||
// } catch (err) {
|
||||
// if (!ignoreOffer) {
|
||||
// throw err;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } catch (err) {
|
||||
// console.error(err);
|
||||
// }
|
||||
// };
|
||||
//# sourceMappingURL=webRTC.js.map
|
||||
Reference in New Issue
Block a user