Created
April 30, 2020 04:18
-
-
Save matheuschimelli/82a346fe5bdd1f38186c5a6dece2184f to your computer and use it in GitHub Desktop.
webrtc simple example
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
(async () => { | |
if (!("mediaDevices" in navigator) || !("RTCPeerConnection" in window)) { | |
alert("Sorry, your browser does not support WebRTC."); | |
return; | |
} | |
const stream = await navigator.mediaDevices.getUserMedia({video:true, audio:true}); | |
yourVideo.srcObject = stream; | |
const configuration = { | |
iceServers: [{urls: "stun:stun.1.google.com:19302"}] | |
}; | |
const yours = new RTCPeerConnection(configuration); | |
const theirs = new RTCPeerConnection(configuration); | |
for (const track of stream.getTracks()) { | |
yours.addTrack(track, stream); | |
} | |
theirs.ontrack = e => theirVideo.srcObject = e.streams[0]; | |
yours.onicecandidate = e => theirs.addIceCandidate(e.candidate); | |
theirs.onicecandidate = e => yours.addIceCandidate(e.candidate); | |
const offer = await yours.createOffer(); | |
await yours.setLocalDescription(offer); | |
await theirs.setRemoteDescription(offer); | |
const answer = await theirs.createAnswer(); | |
await theirs.setLocalDescription(answer); | |
await yours.setRemoteDescription(answer); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment