1 <!DOCTYPE html> 2 <html> 3 <head><title>Loopback test</title></head> 4 <body> 5 <video id="remoteVideo" width="1280" height="720" autoplay muted></video> 6 <script> 7 var localStream, localPeerConnection, remotePeerConnection; 8 var remoteVideo = document.getElementById("remoteVideo"); 9 10 function start() { 11 navigator.getUserMedia = navigator.getUserMedia || 12 navigator.webkitGetUserMedia; 13 navigator.getUserMedia( 14 { 15 audio:false, 16 video:{ 17 "mandatory": { 18 "minWidth": "1280", 19 "minHeight": "720", 20 } 21 } 22 }, 23 gotLocalStream, 24 gotError); 25 } 26 27 function gotLocalStream(stream) { 28 localStream = stream; 29 var servers = null; 30 31 localPeerConnection = new webkitRTCPeerConnection(servers); 32 localPeerConnection.onicecandidate = gotLocalIceCandidate; 33 34 remotePeerConnection = new webkitRTCPeerConnection(servers); 35 remotePeerConnection.onicecandidate = gotRemoteIceCandidate; 36 remotePeerConnection.onaddstream = gotRemoteStream; 37 38 localPeerConnection.addStream(localStream); 39 localPeerConnection.createOffer(gotLocalDescription); 40 } 41 42 function gotError(error) { 43 console.log("navigator.getUserMedia error: ", error); 44 } 45 46 function gotRemoteStream(event) { 47 remoteVideo.src = URL.createObjectURL(event.stream); 48 } 49 50 function gotLocalDescription(description) { 51 localPeerConnection.setLocalDescription(description); 52 remotePeerConnection.setRemoteDescription(description); 53 remotePeerConnection.createAnswer(gotRemoteDescription); 54 } 55 56 function gotRemoteDescription(description) { 57 remotePeerConnection.setLocalDescription(description); 58 localPeerConnection.setRemoteDescription(description); 59 } 60 61 function gotLocalIceCandidate(event) { 62 if (event.candidate) 63 remotePeerConnection.addIceCandidate(new RTCIceCandidate(event.candidate)); 64 } 65 66 function gotRemoteIceCandidate(event) { 67 if (event.candidate) 68 localPeerConnection.addIceCandidate(new RTCIceCandidate(event.candidate)); 69 } 70 71 window.onload=start; 72 </script> 73 </body> 74 </html> 75