wip v2
This commit is contained in:
163
webRTC.js
163
webRTC.js
@@ -1,96 +1,83 @@
|
||||
"use strict";
|
||||
class PeerManager {
|
||||
connect(peerID) {
|
||||
// Connect to the peer that has the peer id peerID
|
||||
}
|
||||
disconnect(peerID) {
|
||||
// https://web.dev/articles/webrtc-basics
|
||||
|
||||
// handles JSON.stringify/parse
|
||||
|
||||
class SignalingChannel {
|
||||
send(message) {
|
||||
console.log(message);
|
||||
}
|
||||
}
|
||||
class PeerConnection {
|
||||
}
|
||||
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 = {};
|
||||
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);
|
||||
|
||||
|
||||
function createPeerConnection(targetPeer) {
|
||||
|
||||
const signaling = new SignalingChannel();
|
||||
|
||||
// const constraints = {audio: true, video: true};
|
||||
const configuration = { iceServers: [{ urls: 'stun:stunserver2024.stunprotocol.org' }] };
|
||||
const peerConnection = new RTCPeerConnection(configuration);
|
||||
const sendChannel = peerConnection.createDataChannel('default');
|
||||
|
||||
|
||||
// Send any ice candidates to the other peer.
|
||||
peerConnection.onicecandidate = ({ candidate }) => signaling.send({ candidate });
|
||||
|
||||
// Let the "negotiationneeded" event trigger offer generation.
|
||||
peerConnection.onnegotiationneeded = async () => {
|
||||
try {
|
||||
await peerConnection.setLocalDescription(await peerConnection.createOffer());
|
||||
// Send the offer to the other peer.
|
||||
signaling.send({ desc: peerConnection.localDescription });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
// 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 });
|
||||
|
||||
// // Once remote track media arrives, show it in remote video element.
|
||||
// peerConnection.ontrack = (event) => {
|
||||
// // Don't set srcObject again if it is already set.
|
||||
// if (remoteView.srcObject) return;
|
||||
// remoteView.srcObject = event.streams[0];
|
||||
// };
|
||||
|
||||
// Call start() to initiate.
|
||||
async function start() {
|
||||
// try {
|
||||
// Get local stream, show it in self-view, and add it to be sent.
|
||||
// const stream =
|
||||
// await navigator.mediaDevices.getUserMedia(constraints);
|
||||
// stream.getTracks().forEach((track) =>
|
||||
// peerConnection.addTrack(track, stream));
|
||||
// selfView.srcObject = stream;
|
||||
// } catch (err) {
|
||||
// console.error(err);
|
||||
// }
|
||||
}
|
||||
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;
|
||||
|
||||
signaling.onmessage = async ({ desc, candidate }) => {
|
||||
try {
|
||||
if (desc) {
|
||||
// If you get an offer, you need to reply with an answer.
|
||||
if (desc.type === 'offer') {
|
||||
await peerConnection.setRemoteDescription(desc);
|
||||
// const stream =
|
||||
// await navigator.mediaDevices.getUserMedia(constraints);
|
||||
// stream.getTracks().forEach((track) =>
|
||||
// peerConnection.addTrack(track, stream));
|
||||
await peerConnection.setLocalDescription(await peerConnection.createAnswer());
|
||||
signaling.send({ desc: peerConnection.localDescription });
|
||||
} else if (desc.type === 'answer') {
|
||||
await peerConnection.setRemoteDescription(desc);
|
||||
} else {
|
||||
console.log('Unsupported SDP type.');
|
||||
}
|
||||
} else if (candidate) {
|
||||
await peerConnection.addIceCandidate(candidate);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
};
|
||||
//# sourceMappingURL=webRTC.js.map
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user