|
| 1 | +<!DOCTYPE html> |
| 2 | +<head> |
| 3 | + <meta charset="UTF-8"> |
| 4 | + |
| 5 | + <script type="text/javascript"> |
| 6 | + |
| 7 | + "use strict"; |
| 8 | + |
| 9 | + function RestSignaler(signalUrl) { |
| 10 | + |
| 11 | + this.sendOffer = async function (offer) { |
| 12 | + return (await fetch(signalUrl, { |
| 13 | + method: 'POST', |
| 14 | + body: JSON.stringify(offer), |
| 15 | + headers: { 'Content-Type': 'application/json' } |
| 16 | + })).json(); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + </script> |
| 21 | + |
| 22 | + <script type="text/javascript"> |
| 23 | + |
| 24 | + "use strict"; |
| 25 | + |
| 26 | + const STUN_SERVER = "stun:stun.sipsorcery.com"; |
| 27 | + const NOISE_FRAME_WIDTH = 80; |
| 28 | + const NOISE_FRAME_HEIGHT = 60; |
| 29 | + const ICE_GATHERING_TIMEOUT = 3000; |
| 30 | + |
| 31 | + var pc; |
| 32 | + var signaler; |
| 33 | + var isClosed = true; |
| 34 | + var isOfferSent = false; |
| 35 | + |
| 36 | + window.onload = () => { |
| 37 | + let host = window.location.hostname.concat(window.location.port ? `:${window.location.port}` : ""); |
| 38 | + document.getElementById('signalingUrl').value = `${window.location.protocol}//${host}/offer`; |
| 39 | + }; |
| 40 | + |
| 41 | + async function start() { |
| 42 | + |
| 43 | + if (!isClosed) { |
| 44 | + // Close any existing peer connection. |
| 45 | + await closePeer(); |
| 46 | + } |
| 47 | + |
| 48 | + isOfferSent = false; |
| 49 | + |
| 50 | + let signalingUrl = document.getElementById('signalingUrl').value; |
| 51 | + |
| 52 | + // Create the noise. |
| 53 | + let noiseStm = whiteNoise(NOISE_FRAME_WIDTH, NOISE_FRAME_HEIGHT); |
| 54 | + document.getElementById("localVideoCtl").srcObject = noiseStm; |
| 55 | + |
| 56 | + signaler = new RestSignaler(signalingUrl); |
| 57 | + |
| 58 | + // Create the peer connections. |
| 59 | + pc = createPeer("echoVideoCtl", noiseStm); |
| 60 | + |
| 61 | + pc.onicegatheringstatechange = async () => { |
| 62 | + console.log(`onicegatheringstatechange: ${pc.iceGatheringState}.`); |
| 63 | + |
| 64 | + if (pc.iceGatheringState === 'complete') { |
| 65 | + sendOffer(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + let offer = await pc.createOffer(); |
| 70 | + await pc.setLocalDescription(offer); |
| 71 | + |
| 72 | + setTimeout(sendOffer, ICE_GATHERING_TIMEOUT); |
| 73 | + } |
| 74 | + |
| 75 | + async function sendOffer() { |
| 76 | + |
| 77 | + if (!isOfferSent) { |
| 78 | + isOfferSent = true; |
| 79 | + |
| 80 | + var offerWithIce = pc.localDescription; |
| 81 | + console.log(offerWithIce); |
| 82 | + var answer = await signaler.sendOffer(offerWithIce); |
| 83 | + |
| 84 | + if (answer != null) { |
| 85 | + console.log(answer.sdp) |
| 86 | + await pc.setRemoteDescription(answer); |
| 87 | + } |
| 88 | + else { |
| 89 | + console.log("Failed to get an answer from the Echo Test server.") |
| 90 | + pc.close(); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + function createPeer(videoCtlID, noiseStm) { |
| 96 | + |
| 97 | + console.log("Creating peer ..."); |
| 98 | + isClosed = false; |
| 99 | + |
| 100 | + let pc = new RTCPeerConnection({ iceServers: [{ urls: STUN_SERVER }] }); |
| 101 | + noiseStm.getTracks().forEach(track => pc.addTrack(track, noiseStm)); |
| 102 | + pc.ontrack = evt => document.getElementById(videoCtlID).srcObject = evt.streams[0]; |
| 103 | + pc.onicecandidate = evt => evt.candidate && console.log(evt.candidate); |
| 104 | + |
| 105 | + // Diagnostics. |
| 106 | + pc.onicegatheringstatechange = () => console.log(`onicegatheringstatechange: ${pc.iceGatheringState}.`); |
| 107 | + pc.oniceconnectionstatechange = () => console.log(`oniceconnectionstatechange: ${pc.iceConnectionState}.`); |
| 108 | + pc.onsignalingstatechange = () => console.log(`onsignalingstatechange: ${pc.signalingState}.`); |
| 109 | + pc.onconnectionstatechange = () => console.log(`onconnectionstatechange: ${pc.connectionState}.`); |
| 110 | + |
| 111 | + return pc; |
| 112 | + } |
| 113 | + |
| 114 | + async function closePeer() { |
| 115 | + console.log("Closing...") |
| 116 | + isClosed = true; |
| 117 | + await pc?.close(); |
| 118 | + }; |
| 119 | + |
| 120 | + function whiteNoise(width, height) { |
| 121 | + const canvas = Object.assign(document.createElement("canvas"), { width, height }); |
| 122 | + const ctx = canvas.getContext('2d'); |
| 123 | + ctx.fillRect(0, 0, width, height); |
| 124 | + const p = ctx.getImageData(0, 0, width, height); |
| 125 | + requestAnimationFrame(function draw() { |
| 126 | + if (!isClosed) { |
| 127 | + for (var i = 0; i < p.data.length; i++) { |
| 128 | + p.data[i++] = Math.random() * 255; |
| 129 | + p.data[i++] = Math.random() * 255; |
| 130 | + p.data[i++] = Math.random() * 255; |
| 131 | + } |
| 132 | + ctx.putImageData(p, 0, 0); |
| 133 | + requestAnimationFrame(draw); |
| 134 | + } |
| 135 | + }); |
| 136 | + return canvas.captureStream(); |
| 137 | + } |
| 138 | + |
| 139 | + </script> |
| 140 | +</head> |
| 141 | +<body> |
| 142 | + |
| 143 | + <table> |
| 144 | + <thead> |
| 145 | + <tr> |
| 146 | + <th>Source</th> |
| 147 | + <th>Echo</th> |
| 148 | + </tr> |
| 149 | + </thead> |
| 150 | + <tr> |
| 151 | + <td> |
| 152 | + <video controls autoplay="autoplay" id="localVideoCtl" width="320" height="240"></video> |
| 153 | + </td> |
| 154 | + <td> |
| 155 | + <video controls autoplay="autoplay" id="echoVideoCtl" width="320" height="240"></video> |
| 156 | + </td> |
| 157 | + |
| 158 | + </tr> |
| 159 | + </table> |
| 160 | + |
| 161 | + <div> |
| 162 | + <label>Signaling URL:</label> <input type="text" id="signalingUrl" size="40" /><br /> |
| 163 | + <button type="button" class="btn btn-success" onclick="start();">Start</button> |
| 164 | + <button type="button" class="btn btn-success" onclick="closePeer();">Close</button> |
| 165 | + </div> |
| 166 | + |
| 167 | +</body> |
0 commit comments