Skip to content

Instantly share code, notes, and snippets.

@hubgit
Last active April 7, 2020 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hubgit/4d7557a38928255a4a8f655033eebbd4 to your computer and use it in GitHub Desktop.
Save hubgit/4d7557a38928255a4a8f655033eebbd4 to your computer and use it in GitHub Desktop.
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