Last active
April 7, 2020 09:47
-
-
Save hubgit/4d7557a38928255a4a8f655033eebbd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const config = { iceServers: [ { url: 'stun:stun.l.google.com:19302' } ] } | |
const [localConnection, remoteConnection] = [new RTCPeerConnection(config), new RTCPeerConnection(config)] | |
const handleIceCandidate = connection => event => event.candidate && connection.addIceCandidate(new RTCIceCandidate(event.candidate)) | |
localConnection.onicecandidate = handleIceCandidate(remoteConnection) | |
remoteConnection.onicecandidate = handleIceCandidate(localConnection) | |
const [localVideo, remoteVideo] = [document.createElement('video'), document.createElement('video')] | |
const handleTrack = video => ({ streams: [stream] }) => { | |
video.srcObject = stream | |
video.play() | |
} | |
localConnection.ontrack = handleTrack(localVideo) | |
remoteConnection.ontrack = handleTrack(remoteVideo) | |
localConnection.createOffer(description => { | |
localConnection.setLocalDescription(description) | |
remoteConnection.setRemoteDescription(description) | |
remoteConnection.createAnswer(description => { | |
remoteConnection.setLocalDescription(description) | |
localConnection.setRemoteDescription(description) | |
}) | |
}) | |
document.body.appendChild(localVideo) | |
document.body.appendChild(remoteVideo) | |
navigator.mediaDevices.getUserMedia({audio: true, video: true}).then(stream => { | |
const tracks = stream.getTracks() | |
for (const track of tracks) { | |
localConnection.addTrack(track, stream) | |
} | |
for (const track of tracks) { | |
remoteConnection.addTrack(track, stream) | |
} | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment