145 lines
4.7 KiB
JavaScript
145 lines
4.7 KiB
JavaScript
// import { wsConnection } from "./main";
|
|
export class PeerManager {
|
|
connect(peerID) {
|
|
// Connect to the peer that has the peer id peerID
|
|
}
|
|
disconnect(peerID) {
|
|
}
|
|
}
|
|
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: [
|
|
{ urls: "stun:stun.l.google.com" },
|
|
{ urls: "stun:stun1.l.google.com" },
|
|
{ urls: "stun:stun2.l.google.com" },
|
|
{ urls: "stun:stun3.l.google.com" },
|
|
{ urls: "stun:stun4.l.google.com" },
|
|
],
|
|
};
|
|
// const config = {
|
|
// iceServers: [{ urls: "stun:stun.mystunserver.tld" }],
|
|
// };
|
|
// let polite = true;
|
|
// const signaler = new SignalingChannel();
|
|
// 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
|