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