SHARE
TWEET

WebRTC Demo

a guest Jun 24th, 2018 114 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Slide #1: Getting local video stream and initializing a peer connection with it (both caller and callee)
  2.  
  3. var localStream = null;
  4. var localVideo = document.getElementById('localVideo');
  5.  
  6. navigator
  7.   .mediaDevices
  8.   .getUserMedia({ audio: true, video: true })
  9.   .then(stream => {
  10.     localVideo.srcObject = stream;
  11.     localStream = stream;
  12.   });
  13.  
  14. var pc = new RTCPeerConnection({ iceServers: [...] });
  15.  
  16. localStream
  17.   .getTracks()
  18.   .forEach(track => pc.addTrack(track, localStream));
  19.  
  20. // Slide #2: Creating SDP offer (caller)
  21.  
  22. pc.createOffer({ offerToReceiveAudio: true, offerToReceiveVideo: true })
  23.   .then(offer => signaling.send('offer', offer));
  24.  
  25. // Slide #3: Handling SDP offer and sending SDP answer (callee)
  26.  
  27. signaling.on('offer', offer => {
  28.   pc.setRemoteDescription(offer)
  29.     .then(() => pc.createAnswer())
  30.     .then(answer => signaling.send('answer', answer))
  31. });
  32.  
  33. // Slide #4: Handling SDP answer (calleer)
  34.  
  35. signaling.on('answer', answer => pc.setRemoteDescription(answer));
  36.  
  37. // Slide #5: Exchanging ICE candidates
  38.  
  39. pc.onicecandidate = event => signaling.send('candidate', event.candidate);
  40.  
  41. signaling.on('candidate', candidate => pc.addIceCandidate(candidate));
  42.  
  43. // Slide #6: Getting remote video stream (both caller and callee)
  44.  
  45. var remoteVideo = document.getElementById('remoteVideo');
  46.  
  47. pc.onaddstream = event => remoteVideo.srcObject = event.streams[0];
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top