|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | + * |
| 4 | + * Use of this source code is governed by a BSD-style license |
| 5 | + * that can be found in the LICENSE file in the root of the source |
| 6 | + * tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/* globals StreamVisualizer */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +var startButton = document.getElementById('startButton'); |
| 14 | +var callButton = document.getElementById('callButton'); |
| 15 | +var hangupButton = document.getElementById('hangupButton'); |
| 16 | +callButton.disabled = true; |
| 17 | +hangupButton.disabled = true; |
| 18 | +startButton.onclick = start; |
| 19 | +callButton.onclick = call; |
| 20 | +hangupButton.onclick = hangup; |
| 21 | + |
| 22 | +var canvas = document.querySelector('canvas'); |
| 23 | + |
| 24 | +var startTime; |
| 25 | +var localVideo = document.getElementById('localVideo'); |
| 26 | +var remoteVideo = document.getElementById('remoteVideo'); |
| 27 | + |
| 28 | +localVideo.addEventListener('loadedmetadata', function() { |
| 29 | + trace('Local video videoWidth: ' + this.videoWidth + |
| 30 | + 'px, videoHeight: ' + this.videoHeight + 'px'); |
| 31 | +}); |
| 32 | + |
| 33 | +remoteVideo.addEventListener('loadedmetadata', function() { |
| 34 | + trace('Remote video videoWidth: ' + this.videoWidth + |
| 35 | + 'px, videoHeight: ' + this.videoHeight + 'px'); |
| 36 | +}); |
| 37 | + |
| 38 | +remoteVideo.addEventListener('resize', function() { |
| 39 | + trace('Remote video size changed to ' + |
| 40 | + remoteVideo.videoWidth + 'x' + remoteVideo.videoHeight); |
| 41 | + // We'll use the first onsize callback as an indication that video has started |
| 42 | + // playing out. |
| 43 | + if (startTime) { |
| 44 | + var elapsedTime = window.performance.now() - startTime; |
| 45 | + trace('Setup time: ' + elapsedTime.toFixed(3) + 'ms'); |
| 46 | + startTime = null; |
| 47 | + } |
| 48 | +}); |
| 49 | + |
| 50 | +var localStream; |
| 51 | +var pc1; |
| 52 | +var pc2; |
| 53 | +var offerOptions = { |
| 54 | + offerToReceiveAudio: 1, |
| 55 | + offerToReceiveVideo: 1 |
| 56 | +}; |
| 57 | + |
| 58 | +function getName(pc) { |
| 59 | + return (pc === pc1) ? 'pc1' : 'pc2'; |
| 60 | +} |
| 61 | + |
| 62 | +function getOtherPc(pc) { |
| 63 | + return (pc === pc1) ? pc2 : pc1; |
| 64 | +} |
| 65 | + |
| 66 | +function gotStream(stream) { |
| 67 | + trace('Received local stream'); |
| 68 | + localVideo.srcObject = stream; |
| 69 | + localStream = stream; |
| 70 | + callButton.disabled = false; |
| 71 | +} |
| 72 | + |
| 73 | +function start() { |
| 74 | + trace('Requesting local stream'); |
| 75 | + startButton.disabled = true; |
| 76 | + navigator.mediaDevices.getUserMedia({ |
| 77 | + audio: true, |
| 78 | + video: true |
| 79 | + }) |
| 80 | + .then(gotStream) |
| 81 | + .catch(function(e) { |
| 82 | + alert('getUserMedia() error: ' + e.name); |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +function call() { |
| 87 | + callButton.disabled = true; |
| 88 | + hangupButton.disabled = false; |
| 89 | + trace('Starting call'); |
| 90 | + startTime = window.performance.now(); |
| 91 | + var videoTracks = localStream.getVideoTracks(); |
| 92 | + var audioTracks = localStream.getAudioTracks(); |
| 93 | + if (videoTracks.length > 0) { |
| 94 | + trace('Using video device: ' + videoTracks[0].label); |
| 95 | + } |
| 96 | + if (audioTracks.length > 0) { |
| 97 | + trace('Using audio device: ' + audioTracks[0].label); |
| 98 | + } |
| 99 | + var servers = null; |
| 100 | + pc1 = new RTCPeerConnection(servers); |
| 101 | + trace('Created local peer connection object pc1'); |
| 102 | + pc1.onicecandidate = function(e) { |
| 103 | + onIceCandidate(pc1, e); |
| 104 | + }; |
| 105 | + pc2 = new RTCPeerConnection(servers); |
| 106 | + trace('Created remote peer connection object pc2'); |
| 107 | + pc2.onicecandidate = function(e) { |
| 108 | + onIceCandidate(pc2, e); |
| 109 | + }; |
| 110 | + pc1.oniceconnectionstatechange = function(e) { |
| 111 | + onIceStateChange(pc1, e); |
| 112 | + }; |
| 113 | + pc2.oniceconnectionstatechange = function(e) { |
| 114 | + onIceStateChange(pc2, e); |
| 115 | + }; |
| 116 | + pc2.onaddstream = gotRemoteStream; |
| 117 | + |
| 118 | + pc1.addStream(localStream); |
| 119 | + trace('Added local stream to pc1'); |
| 120 | + |
| 121 | + trace('pc1 createOffer start'); |
| 122 | + pc1.createOffer(onCreateOfferSuccess, onCreateSessionDescriptionError, |
| 123 | + offerOptions); |
| 124 | +} |
| 125 | + |
| 126 | +function onCreateSessionDescriptionError(error) { |
| 127 | + trace('Failed to create session description: ' + error.toString()); |
| 128 | +} |
| 129 | + |
| 130 | +function onCreateOfferSuccess(desc) { |
| 131 | + trace('Offer from pc1\n' + desc.sdp); |
| 132 | + trace('pc1 setLocalDescription start'); |
| 133 | + pc1.setLocalDescription(desc, function() { |
| 134 | + onSetLocalSuccess(pc1); |
| 135 | + }, onSetSessionDescriptionError); |
| 136 | + trace('pc2 setRemoteDescription start'); |
| 137 | + pc2.setRemoteDescription(desc, function() { |
| 138 | + onSetRemoteSuccess(pc2); |
| 139 | + }, onSetSessionDescriptionError); |
| 140 | + trace('pc2 createAnswer start'); |
| 141 | + // Since the 'remote' side has no media stream we need |
| 142 | + // to pass in the right constraints in order for it to |
| 143 | + // accept the incoming offer of audio and video. |
| 144 | + pc2.createAnswer(onCreateAnswerSuccess, onCreateSessionDescriptionError); |
| 145 | +} |
| 146 | + |
| 147 | +function onSetLocalSuccess(pc) { |
| 148 | + trace(getName(pc) + ' setLocalDescription complete'); |
| 149 | +} |
| 150 | + |
| 151 | +function onSetRemoteSuccess(pc) { |
| 152 | + trace(getName(pc) + ' setRemoteDescription complete'); |
| 153 | +} |
| 154 | + |
| 155 | +function onSetSessionDescriptionError(error) { |
| 156 | + trace('Failed to set session description: ' + error.toString()); |
| 157 | +} |
| 158 | + |
| 159 | +function gotRemoteStream(e) { |
| 160 | + remoteVideo.srcObject = e.stream; |
| 161 | + trace('pc2 received remote stream'); |
| 162 | + var streamVisualizer = new StreamVisualizer(e.stream, canvas); |
| 163 | + streamVisualizer.start(); |
| 164 | +} |
| 165 | + |
| 166 | + |
| 167 | +function onCreateAnswerSuccess(desc) { |
| 168 | + trace('Answer from pc2:\n' + desc.sdp); |
| 169 | + trace('pc2 setLocalDescription start'); |
| 170 | + pc2.setLocalDescription(desc, function() { |
| 171 | + onSetLocalSuccess(pc2); |
| 172 | + }, onSetSessionDescriptionError); |
| 173 | + trace('pc1 setRemoteDescription start'); |
| 174 | + pc1.setRemoteDescription(desc, function() { |
| 175 | + onSetRemoteSuccess(pc1); |
| 176 | + }, onSetSessionDescriptionError); |
| 177 | +} |
| 178 | + |
| 179 | +function onIceCandidate(pc, event) { |
| 180 | + if (event.candidate) { |
| 181 | + getOtherPc(pc).addIceCandidate(new RTCIceCandidate(event.candidate), |
| 182 | + function() { |
| 183 | + onAddIceCandidateSuccess(pc); |
| 184 | + }, |
| 185 | + function(err) { |
| 186 | + onAddIceCandidateError(pc, err); |
| 187 | + } |
| 188 | + ); |
| 189 | + trace(getName(pc) + ' ICE candidate: \n' + event.candidate.candidate); |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +function onAddIceCandidateSuccess(pc) { |
| 194 | + trace(getName(pc) + ' addIceCandidate success'); |
| 195 | +} |
| 196 | + |
| 197 | +function onAddIceCandidateError(pc, error) { |
| 198 | + trace(getName(pc) + ' failed to add ICE Candidate: ' + error.toString()); |
| 199 | +} |
| 200 | + |
| 201 | +function onIceStateChange(pc, event) { |
| 202 | + if (pc) { |
| 203 | + trace(getName(pc) + ' ICE state: ' + pc.iceConnectionState); |
| 204 | + console.log('ICE state change event: ', event); |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +function hangup() { |
| 209 | + trace('Ending call'); |
| 210 | + pc1.close(); |
| 211 | + pc2.close(); |
| 212 | + pc1 = null; |
| 213 | + pc2 = null; |
| 214 | + hangupButton.disabled = true; |
| 215 | + callButton.disabled = false; |
| 216 | +} |
0 commit comments