|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 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 | +'use strict'; |
| 10 | + |
| 11 | +const localVideo = document.getElementById('localVideo'); |
| 12 | +const remoteVideo = document.getElementById('remoteVideo'); |
| 13 | +const startButton = document.getElementById('startButton'); |
| 14 | +const callButton = document.getElementById('callButton'); |
| 15 | +const renegotiateButton = document.getElementById('renegotiateButton'); |
| 16 | +const hangupButton = document.getElementById('hangupButton'); |
| 17 | +const negotiateCheckbox = document.getElementById('negotiateDataChannel'); |
| 18 | +startButton.onclick = start; |
| 19 | +callButton.onclick = call; |
| 20 | +renegotiateButton.onclick = renegotiate; |
| 21 | +hangupButton.onclick = hangup; |
| 22 | + |
| 23 | +let localStream; |
| 24 | +let pc1; |
| 25 | +let pc2; |
| 26 | + |
| 27 | +function log(text) { |
| 28 | + document.getElementById('log').innerText += text + '\n'; |
| 29 | +} |
| 30 | +function getName(pc) { |
| 31 | + return (pc === pc1) ? 'pc1' : 'pc2'; |
| 32 | +} |
| 33 | + |
| 34 | +function getOtherPc(pc) { |
| 35 | + return (pc === pc1) ? pc2 : pc1; |
| 36 | +} |
| 37 | + |
| 38 | +async function start() { |
| 39 | + startButton.disabled = true; |
| 40 | + negotiateCheckbox.disabled = true; |
| 41 | + const stream = await navigator.mediaDevices.getUserMedia({ |
| 42 | + audio: false, |
| 43 | + video: true, |
| 44 | + }); |
| 45 | + localVideo.srcObject = stream; |
| 46 | + localStream = stream; |
| 47 | + callButton.disabled = false; |
| 48 | + negotiateCheckbox.disabled = false; |
| 49 | +} |
| 50 | + |
| 51 | +async function call() { |
| 52 | + callButton.disabled = true; |
| 53 | + negotiateCheckbox.disabled = true; |
| 54 | + renegotiateButton.disabled = false; |
| 55 | + hangupButton.disabled = false; |
| 56 | + console.log('Starting call'); |
| 57 | + const audioTracks = localStream.getAudioTracks(); |
| 58 | + if (audioTracks.length > 0) { |
| 59 | + console.log(`Using audio device: ${audioTracks[0].label}`); |
| 60 | + } |
| 61 | + const config = { |
| 62 | + alwaysNegotiateDataChannels: negotiateCheckbox.checked, |
| 63 | + }; |
| 64 | + pc1 = new RTCPeerConnection(config); |
| 65 | + // Use pc.getConfiguration to detect whether the feature is supported. |
| 66 | + if (pc1.getConfiguration().alwaysNegotiateDataChannels === undefined) { |
| 67 | + log('RTCConfiguration.alwaysNegotiateDataChannel is not supported in this browser (' + adapter.browserDetails.browser + '/' + adapter.browserDetails.version + ')'); |
| 68 | + } else { |
| 69 | + log('RTCConfiguration.alwaysNegotiateDataChannel is supported in this browser (' + adapter.browserDetails.browser + '/' + adapter.browserDetails.version + ')'); |
| 70 | + } |
| 71 | + pc1.onicecandidate = e => onIceCandidate(pc1, e); |
| 72 | + pc2 = new RTCPeerConnection(); |
| 73 | + pc2.onicecandidate = e => onIceCandidate(pc2, e); |
| 74 | + pc2.ontrack = gotRemoteStream; |
| 75 | + |
| 76 | + // Create an audio transceiver which serves as transport. |
| 77 | + pc1.addTransceiver('audio'); |
| 78 | + localStream.getTracks().forEach(track => pc1.addTrack(track, localStream)); |
| 79 | + |
| 80 | + await pc1.setLocalDescription(); |
| 81 | + await pc2.setRemoteDescription(pc1.localDescription); |
| 82 | + await pc2.setLocalDescription(); |
| 83 | + await pc1.setRemoteDescription(pc2.localDescription); |
| 84 | + renegotiateButton.disabled = false; |
| 85 | +} |
| 86 | + |
| 87 | +function gotRemoteStream(e) { |
| 88 | + remoteVideo.srcObject = e.streams[0]; |
| 89 | +} |
| 90 | + |
| 91 | +function onIceCandidate(pc, event) { |
| 92 | + getOtherPc(pc) |
| 93 | + .addIceCandidate(event.candidate) |
| 94 | + .catch(err => onAddIceCandidateError(pc, err)); |
| 95 | +} |
| 96 | + |
| 97 | +function onAddIceCandidateError(pc, error) { |
| 98 | + console.log(`${getName(pc)} failed to add ICE Candidate: ${error.toString()}`); |
| 99 | +} |
| 100 | + |
| 101 | +async function renegotiate() { |
| 102 | + renegotiateButton.disabled = true; |
| 103 | + hangupButton.disabled = true; |
| 104 | + pc1.getTransceivers()[0].stop(); // stops the first transceiver. |
| 105 | + if (negotiateCheckbox.checked) { |
| 106 | + log('Stopped transceiver, the right video should not freeze (if always negotiating data channels is supported)'); |
| 107 | + } else { |
| 108 | + log('Stopped transceiver, the right video will freeze for ~4 seconds'); |
| 109 | + } |
| 110 | + await pc1.setLocalDescription(); |
| 111 | + await pc2.setRemoteDescription(pc1.localDescription); |
| 112 | + await new Promise(r => setTimeout(r, 4000)); // wait 4 seconds. |
| 113 | + console.log('renegotiated'); |
| 114 | + await pc2.setLocalDescription(); |
| 115 | + await pc1.setRemoteDescription(pc2.localDescription); |
| 116 | + await new Promise(r => setTimeout(r, 1000)); // wait 1 seconds. |
| 117 | + const stats = await pc2.getStats(); |
| 118 | + const freezes = [...stats.values()].filter(s => s.type === 'inbound-rtp' && s.kind === 'video').map(s => s.totalFreezesDuration); |
| 119 | + log('Renegotiation done, the getStats() API detected a freeze lasting ' + freezes + ' seconds'); |
| 120 | + log('Flip the "always negotiate data channels" box and try again'); |
| 121 | + hangupButton.disabled = false; |
| 122 | +} |
| 123 | + |
| 124 | +function hangup() { |
| 125 | + console.log('Ending call'); |
| 126 | + pc1.close(); |
| 127 | + pc2.close(); |
| 128 | + pc1 = null; |
| 129 | + pc2 = null; |
| 130 | + |
| 131 | + hangupButton.disabled = true; |
| 132 | + callButton.disabled = false; |
| 133 | + negotiateCheckbox.disabled = false; |
| 134 | +} |
0 commit comments