|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const createExample = require('../../lib/browser/example'); |
| 4 | + |
| 5 | +const description = 'This example sends a given amount of data from the client \ |
| 6 | +over an RTCDataChannel. Upon receipt, node-webrtc responds by sending the data \ |
| 7 | +back to the client. \ |
| 8 | +Data is chunked into pieces, you can adjust both the chunk size and global \ |
| 9 | +size to test the outbound buffer limits of the RTCDataChannel.'; |
| 10 | + |
| 11 | +const dataSize = document.createElement('input'); |
| 12 | +dataSize.type = 'number'; |
| 13 | +dataSize.value = 10; |
| 14 | +dataSize.min = 1; |
| 15 | +dataSize.max = 1024; |
| 16 | + |
| 17 | +const chunkSize = document.createElement('input'); |
| 18 | +chunkSize.type = 'number'; |
| 19 | +chunkSize.value = 64; |
| 20 | +chunkSize.step = 16; |
| 21 | +chunkSize.min = 16; |
| 22 | +chunkSize.max = 512; |
| 23 | + |
| 24 | +function beforeAnswer(peerConnection) { |
| 25 | + let dataChannel = null; |
| 26 | + let downloadStartTime = 0; |
| 27 | + let downLoadDuration = 0; |
| 28 | + let downloadedBytes = 0; |
| 29 | + const uploadedBytes = (dataSize.value)*1024*1024; |
| 30 | + |
| 31 | + function closeDatachannel() { |
| 32 | + if (dataChannel) { |
| 33 | + dataChannel.removeEventListener('message', onMessage); |
| 34 | + dataChannel.close(); |
| 35 | + dataChannel = null; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + function resetButtons() { |
| 40 | + document.querySelectorAll('button').forEach((button) => { |
| 41 | + if (button.innerText === 'Start') { |
| 42 | + button.disabled = false; |
| 43 | + } |
| 44 | + |
| 45 | + if (button.innerText === 'Stop') { |
| 46 | + button.disabled = true; |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + function onMessage({ data }) { |
| 52 | + if (/^#START/.test(data)) { |
| 53 | + downloadStartTime = Date.now(); |
| 54 | + const uploadDuration = data.split(' ')[1]; |
| 55 | + const uploadBitRate = uploadedBytes*8/(uploadDuration/1000)/1000000; |
| 56 | + const text = `Upload   -- total : ${uploadedBytes}, duration : ${uploadDuration} ms, bitrate : ~${uploadBitRate.toFixed(2)} Mbits/s`; |
| 57 | + uploadLabel.innerHTML = text; |
| 58 | + console.log(text); |
| 59 | + |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if (/^#STOP/.test(data)) { |
| 64 | + downLoadDuration = Date.now() - downloadStartTime; |
| 65 | + const downloadBitRate = downloadedBytes*8/(downLoadDuration/1000)/1000000; |
| 66 | + const text = `Download -- total : ${downloadedBytes}, duration : ${downLoadDuration} ms, bitrate : ~${downloadBitRate.toFixed(2)} Mbits/s`; |
| 67 | + downloadLabel.innerHTML = text; |
| 68 | + console.log(text); |
| 69 | + |
| 70 | + peerConnection.close(); |
| 71 | + closeDatachannel(); |
| 72 | + resetButtons(); |
| 73 | + |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + downloadedBytes += data.length; |
| 78 | + } |
| 79 | + |
| 80 | + function onDataChannel({ channel }) { |
| 81 | + if (channel.label !== 'datachannel-buffer-limits') { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + uploadLabel.innerHTML = 'Upload   -- ...'; |
| 86 | + downloadLabel.innerHTML = 'Download -- ...'; |
| 87 | + |
| 88 | + // Slightly delaying everything because Firefox needs it |
| 89 | + setTimeout(() => { |
| 90 | + dataChannel = channel; |
| 91 | + dataChannel.addEventListener('message', onMessage); |
| 92 | + |
| 93 | + const queueStartTime = Date.now(); |
| 94 | + const chunkSizeInBytes = (chunkSize.value)*1024; |
| 95 | + const loops = uploadedBytes / chunkSizeInBytes; |
| 96 | + const rem = uploadedBytes % chunkSizeInBytes; |
| 97 | + |
| 98 | + try { |
| 99 | + dataChannel.send(`#START ${chunkSize.value}`); |
| 100 | + |
| 101 | + var data = new Array(chunkSizeInBytes + 1).join('.'); |
| 102 | + for (let i = 0; i < loops; i++) { |
| 103 | + dataChannel.send(data); |
| 104 | + } |
| 105 | + |
| 106 | + if (rem) { |
| 107 | + dataChannel.send(data); |
| 108 | + } |
| 109 | + |
| 110 | + dataChannel.send('#STOP'); |
| 111 | + const queueDuration = Date.now() - queueStartTime; |
| 112 | + console.log(`Queued ${uploadedBytes} bytes in ${queueDuration} ms`); |
| 113 | + } catch(e) { |
| 114 | + console.log('Failed to send data over dataChannel :', e); |
| 115 | + peerConnection.close(); |
| 116 | + closeDatachannel(); |
| 117 | + resetButtons(); |
| 118 | + alert(e); |
| 119 | + } |
| 120 | + }, 200); |
| 121 | + } |
| 122 | + |
| 123 | + function onConnectionStateChange(event) { |
| 124 | + switch(peerConnection.connectionState) { |
| 125 | + case "disconnected": |
| 126 | + case "failed": |
| 127 | + case "closed": |
| 128 | + console.log('Received close event'); |
| 129 | + closeDatachannel(); |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + peerConnection.addEventListener('connectionstatechange', onConnectionStateChange); |
| 135 | + peerConnection.addEventListener('datachannel', onDataChannel); |
| 136 | +} |
| 137 | + |
| 138 | +createExample('datachannel-buffer-limits', description, { beforeAnswer, RTCPeerConnection: window.RTCPeerConnection }); |
| 139 | + |
| 140 | +const dataSizeLabel = document.createElement('label'); |
| 141 | +dataSizeLabel.innerText = 'Data size to send (MBytes):'; |
| 142 | +dataSizeLabel.appendChild(dataSize); |
| 143 | + |
| 144 | +const chunkSizeLabel = document.createElement('label'); |
| 145 | +chunkSizeLabel.innerText = 'Chunk size (Kbytes):'; |
| 146 | +chunkSizeLabel.appendChild(chunkSize); |
| 147 | + |
| 148 | +const uploadLabel = document.createElement('label'); |
| 149 | +uploadLabel.innerHTML = 'Upload   -- '; |
| 150 | + |
| 151 | +const downloadLabel = document.createElement('label'); |
| 152 | +downloadLabel.innerHTML = 'Download -- '; |
| 153 | + |
| 154 | +document.body.appendChild(dataSizeLabel); |
| 155 | +document.body.appendChild(chunkSizeLabel); |
| 156 | +document.body.appendChild(uploadLabel); |
| 157 | +document.body.appendChild(downloadLabel); |
0 commit comments