Is this working?
For developers, it is an API.
POST https://webrtc.dothemagic/create-video-call
Um, not that sort. It's an API the browser provides (or whichever environment you're coding on.s)
Long story, I won't tell you because
BUT
Use Google Meet. It's too complicated to build your own.
No, seriously.
Thanks for coming to my TED talk.
Jk. Of course I will tell you how webrtc works.
Let me refer my notes.
Let's just go to https://webrtc.org
┌──────┐ ┌──────┐
│ Src │ │ Node │
└──┬───┘ └──┬───┘
│ │
│ │
┌──▼───┐ ┌──▼───┐
│ Node │───────│ Node │
└──┬───┘ └──┬───┘
│ │
│ │
┌──▼───┐ ┌──▼───┐
│ Node │ │ Dest │
└──────┘ └──────┘
This is what ChatGPT things a P2P network looks like.
I am not gonna explain P2P. You use torrents frequently.
To send a live video of my face to you, here's what is needed
The other person will do the same (hopefully they follow step 1 & 2).
const configuration = {
'iceServers': [{'urls': 'stun:stun.l.google.com:19302'}]
};
const peerConnection = new RTCPeerConnection(configuration);const offer = await peerConnection.createOffer();
await peerConnection.setLocalDescription(offer);
// {"type":"offer","sdp":"v=0\r\no=- 1887124698810677396 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\na=extmap-allow-mixed\r\na=msid-semantic: WMS\r\n"}const peerConnection = new RTCPeerConnection(configuration);
const message = getMessageFromCarrierPigeon()
if (message.offer) {
peerConnection.setRemoteDescription(new RTCSessionDescription(message.offer));
const answer = await peerConnection.createAnswer();
await peerConnection.setLocalDescription(answer);
sendMessageThroughCarrierPigeon({'answer': answer});
}Code is self-explanatory (you should have attended my talk).
That's a nice song.
// Listen for local ICE candidates on the local RTCPeerConnection
peerConnection.addEventListener('icecandidate', event => {
if (event.candidate) {
signalingChannel.send({'new-ice-candidate': event.candidate});
}
});
// Listen for remote ICE candidates and add them to the local RTCPeerConnection
signalingChannel.addEventListener('message', async message => {
if (message.iceCandidate) {
try {
await peerConnection.addIceCandidate(message.iceCandidate);
} catch (e) {
console.error('Error adding received ice candidate', e);
}
}
});// Listen for connectionstatechange on the local RTCPeerConnection
peerConnection.addEventListener('connectionstatechange', event => {
if (peerConnection.connectionState === 'connected') {
// Peers connected!
}
});We're finally connected. It fails 90% of the times btw.
const localStream = await getUserMedia({vide: true, audio: true});
const peerConnection = new RTCPeerConnection(iceConfig);
localStream.getTracks().forEach(track => {
peerConnection.addTrack(track, localStream);
});What's that getUserMedia black magic thing?
navigator.mediaDevices.getUserMedia(constraints);const remoteVideo = document.querySelector('#remoteVideo');
peerConnection.addEventListener('track', async (event) => {
const [remoteStream] = event.streams;
remoteVideo.srcObject = remoteStream;
});addTrack on the peerConection, the other party receives a track event.If you have any questions ask ChatGPT me.