OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <head><title>Loopback test</title></head> |
| 4 <body> |
| 5 <video id="localVideo" width="1280" height="720" autoplay muted></video> |
| 6 <video id="remoteVideo" width="1280" height="720" autoplay muted></video> |
| 7 <script> |
| 8 |
| 9 "use strict"; |
| 10 |
| 11 var testDone = 0; |
| 12 var errors = ""; |
| 13 |
| 14 // Starts the test. |
| 15 function testCamera(resolution) { |
| 16 var test = new CameraTest(resolution); |
| 17 test.run(); |
| 18 } |
| 19 |
| 20 |
| 21 function CameraTest(resolutionArray) { |
| 22 this.resolution = resolutionArray; |
| 23 this.localStream = null; |
| 24 this.remoteStream = null; |
| 25 this.remoteVideo = document.getElementById("remoteVideo"); |
| 26 this.localVideo = document.getElementById("localVideo"); |
| 27 this.localVideo.width = this.resolution[0].toString(); |
| 28 this.localVideo.height = this.resolution[1].toString(); |
| 29 this.remoteVideo.width = this.resolution[0].toString(); |
| 30 this.remoteVideo.height = this.resolution[1].toString(); |
| 31 } |
| 32 |
| 33 |
| 34 CameraTest.prototype = { |
| 35 run: function() { |
| 36 this.triggerGetUserMedia(this.resolution); |
| 37 }, |
| 38 |
| 39 triggerGetUserMedia: function(resolution) { |
| 40 var constraints = { |
| 41 audio: false, |
| 42 video: { |
| 43 mandatory: { |
| 44 minWidth: resolution[0], |
| 45 minHeight: resolution[1], |
| 46 maxWidth: resolution[0], |
| 47 maxHeight: resolution[1] |
| 48 } |
| 49 } |
| 50 }; |
| 51 try { |
| 52 this.doGetUserMedia(constraints, this.gotLocalStream.bind(this), |
| 53 this.onGetUserMediaError.bind(this)); |
| 54 } catch (exception) { |
| 55 console.log('Unexpected exception: ', exception); |
| 56 this.reportError('gUM', 'doGetUserMedia failed: ' + exception); |
| 57 } |
| 58 }, |
| 59 |
| 60 reportError: function(errorType, message) { |
| 61 console.log(errorType, message); |
| 62 errors = message; |
| 63 }, |
| 64 |
| 65 doGetUserMedia: function(constraints, onSuccess, onFail) { |
| 66 navigator.getUserMedia = navigator.getUserMedia || |
| 67 navigator.webkitGetUserMedia; |
| 68 navigator.getUserMedia(constraints, onSuccess, onFail); |
| 69 }, |
| 70 |
| 71 gotLocalStream: function(stream) { |
| 72 this.localStream = stream; |
| 73 var servers = null; |
| 74 |
| 75 this.localPeerConnection = new webkitRTCPeerConnection(servers); |
| 76 this.localPeerConnection.onicecandidate = this.gotLocalIceCandidate.bind( |
| 77 this); |
| 78 |
| 79 this.remotePeerConnection = new webkitRTCPeerConnection(servers); |
| 80 this.remotePeerConnection.onicecandidate = this.gotRemoteIceCandidate.bind( |
| 81 this); |
| 82 this.remotePeerConnection.onaddstream = this.gotRemoteStream.bind(this); |
| 83 |
| 84 this.localPeerConnection.addStream(this.localStream); |
| 85 this.localPeerConnection.createOffer(this.gotLocalDescription.bind(this)); |
| 86 this.localVideo.src = URL.createObjectURL(stream); |
| 87 }, |
| 88 |
| 89 onGetUserMediaError: function(stream) { |
| 90 this.reportError('gUM', 'gUM call failed'); |
| 91 }, |
| 92 |
| 93 gotRemoteStream: function(event) { |
| 94 this.remoteVideo.src = URL.createObjectURL(event.stream); |
| 95 }, |
| 96 |
| 97 gotLocalDescription: function(description) { |
| 98 this.localPeerConnection.setLocalDescription(description); |
| 99 this.remotePeerConnection.setRemoteDescription(description); |
| 100 this.remotePeerConnection.createAnswer(this.gotRemoteDescription.bind( |
| 101 this)); |
| 102 }, |
| 103 |
| 104 gotRemoteDescription: function(description) { |
| 105 this.remotePeerConnection.setLocalDescription(description); |
| 106 this.localPeerConnection.setRemoteDescription(description); |
| 107 }, |
| 108 |
| 109 gotLocalIceCandidate: function(event) { |
| 110 if (event.candidate) |
| 111 this.remotePeerConnection.addIceCandidate( |
| 112 new RTCIceCandidate(event.candidate)); |
| 113 }, |
| 114 |
| 115 gotRemoteIceCandidate: function(event) { |
| 116 if (event.candidate) |
| 117 this.localPeerConnection.addIceCandidate( |
| 118 new RTCIceCandidate(event.candidate)); |
| 119 }, |
| 120 } |
| 121 |
| 122 window.onerror = function (message, filename, lineno, colno, error) { |
| 123 console.log("Something went wrong, here is the stack trace --> %s", |
| 124 error.stack); |
| 125 }; |
| 126 </script> |
| 127 </body> |
| 128 </html> |
OLD | NEW |